mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-13 08:55:44 +01:00
Reimplement restarting of scm-manager
SCM-Manager tries now to figure out which is the best strategy for the restart. It chooses from one of the following strategies: * PosixRestartStrategy which uses native LibC * ExitRestartStrategy uses System.exit and relies on external mechanism to start again * InjectionContextRestartStrategy destroys and re initializes the injection context
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
package sonia.scm.lifecycle;
|
||||
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.function.IntConsumer;
|
||||
|
||||
/**
|
||||
* {@link RestartStrategy} which tears down the scm-manager context and
|
||||
* then exists the java process with {@link System#exit(int)}.
|
||||
* <p>
|
||||
* This is useful if an external mechanism is able to restart the process after it has exited.
|
||||
*/
|
||||
class ExitRestartStrategy implements RestartStrategy {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ExitRestartStrategy.class);
|
||||
|
||||
static final String NAME = "exit";
|
||||
|
||||
static final String PROPERTY_EXIT_CODE = "sonia.scm.restart.exit-code";
|
||||
|
||||
private IntConsumer exiter = System::exit;
|
||||
|
||||
ExitRestartStrategy() {
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
void setExiter(IntConsumer exiter) {
|
||||
this.exiter = exiter;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restart(InjectionContext context) {
|
||||
int exitCode = determineExitCode();
|
||||
|
||||
LOG.warn("destroy injection context");
|
||||
context.destroy();
|
||||
|
||||
LOG.warn("exit scm-manager with exit code {}", exitCode);
|
||||
exiter.accept(exitCode);
|
||||
}
|
||||
|
||||
private int determineExitCode() {
|
||||
String exitCodeAsString = System.getProperty(PROPERTY_EXIT_CODE, "0");
|
||||
try {
|
||||
return Integer.parseInt(exitCodeAsString);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw new RestartNotSupportedException("invalid exit code " + exitCodeAsString, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user