Ensure that context is destroyed on restart

This commit is contained in:
René Pfeuffer
2020-03-25 16:52:53 +01:00
parent 15aab3059d
commit 24c7cb0b67
6 changed files with 57 additions and 31 deletions

View File

@@ -103,7 +103,7 @@ public class BootstrapContextFilter extends GuiceFilter {
} }
} }
private class GuiceInjectionContext implements RestartStrategy.InjectionContext { private class GuiceInjectionContext implements RestartStrategy.InternalInjectionContext {
@Override @Override
public void initialize() { public void initialize() {

View File

@@ -35,7 +35,7 @@ import java.util.function.IntConsumer;
* <p> * <p>
* This is useful if an external mechanism is able to restart the process after it has exited. * This is useful if an external mechanism is able to restart the process after it has exited.
*/ */
class ExitRestartStrategy implements RestartStrategy { class ExitRestartStrategy extends RestartStrategy {
private static final Logger LOG = LoggerFactory.getLogger(ExitRestartStrategy.class); private static final Logger LOG = LoggerFactory.getLogger(ExitRestartStrategy.class);
@@ -45,6 +45,8 @@ class ExitRestartStrategy implements RestartStrategy {
private IntConsumer exiter = System::exit; private IntConsumer exiter = System::exit;
private int exitCode;
ExitRestartStrategy() { ExitRestartStrategy() {
} }
@@ -54,12 +56,12 @@ class ExitRestartStrategy implements RestartStrategy {
} }
@Override @Override
public void restart(InjectionContext context) { public void prepareRestart(InjectionContext context) {
int exitCode = determineExitCode(); exitCode = determineExitCode();
}
LOG.warn("destroy injection context");
context.destroy();
@Override
protected void executeRestart(InjectionContext context) {
LOG.warn("exit scm-manager with exit code {}", exitCode); LOG.warn("exit scm-manager with exit code {}", exitCode);
exiter.accept(exitCode); exiter.accept(exitCode);
} }

View File

@@ -33,7 +33,7 @@ import static sonia.scm.lifecycle.CLibrary.*;
/** /**
* Restart strategy which uses execvp from libc. This strategy is only supported on posix base operating systems. * Restart strategy which uses execvp from libc. This strategy is only supported on posix base operating systems.
*/ */
class PosixRestartStrategy implements RestartStrategy { class PosixRestartStrategy extends RestartStrategy {
private static final Logger LOG = LoggerFactory.getLogger(PosixRestartStrategy.class); private static final Logger LOG = LoggerFactory.getLogger(PosixRestartStrategy.class);
@@ -41,10 +41,7 @@ class PosixRestartStrategy implements RestartStrategy {
} }
@Override @Override
public void restart(InjectionContext context) { protected void executeRestart(InjectionContext context) {
LOG.warn("destroy injection context");
context.destroy();
LOG.warn("restart scm-manager jvm process"); LOG.warn("restart scm-manager jvm process");
try { try {
restart(); restart();

View File

@@ -24,23 +24,20 @@
package sonia.scm.lifecycle; package sonia.scm.lifecycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional; import java.util.Optional;
/** /**
* Strategy for restarting SCM-Manager. Implementations must either have a default constructor or one taking the * Strategy for restarting SCM-Manager. Implementations must either have a default constructor or one taking the
* class loader for the current context as a single argument. * class loader for the current context as a single argument.
*/ */
public interface RestartStrategy { public abstract class RestartStrategy {
/** private static final Logger LOG = LoggerFactory.getLogger(RestartStrategy.class);
* Context for Injection in SCM-Manager.
*/
interface InjectionContext {
/**
* Initialize the injection context.
*/
void initialize();
interface InternalInjectionContext extends InjectionContext {
/** /**
* Destroys the injection context. * Destroys the injection context.
*/ */
@@ -48,11 +45,43 @@ public interface RestartStrategy {
} }
/** /**
* Restart SCM-Manager. * Context for Injection in SCM-Manager.
*/
public interface InjectionContext {
/**
* Initialize the injection context.
*/
void initialize();
}
/**
* Restart SCM-Manager by first calling {@link #prepareRestart(InjectionContext)}, destroying the
* current context, and finally calling {@link #executeRestart(InjectionContext)}.
* *
* @param context injection context * @param context injection context
*/ */
void restart(InjectionContext context); public final void restart(InternalInjectionContext context) {
prepareRestart(context);
LOG.warn("destroy injection context");
context.destroy();
executeRestart(context);
}
/**
* Prepare the restart of SCM-Manager. Here you can check whether restart is possible and,
* if necessary, throw a {@link RestartNotSupportedException} to abort the restart.
*
* @param context injection context
*/
protected void prepareRestart(InjectionContext context) {
}
/**
* Actually restart SCM-Manager.
*
* @param context injection context
*/
protected abstract void executeRestart(InjectionContext context);
/** /**
* Returns the configured strategy or empty if restart is not supported by the underlying platform. * Returns the configured strategy or empty if restart is not supported by the underlying platform.

View File

@@ -39,7 +39,7 @@ import static org.mockito.Mockito.verify;
class ExitRestartStrategyTest { class ExitRestartStrategyTest {
@Mock @Mock
private RestartStrategy.InjectionContext context; private RestartStrategy.InternalInjectionContext context;
private ExitRestartStrategy strategy; private ExitRestartStrategy strategy;
private CapturingExiter exiter; private CapturingExiter exiter;

View File

@@ -117,14 +117,13 @@ class RestartStrategyTest {
} }
} }
public static class TestingRestartStrategy implements RestartStrategy { public static class TestingRestartStrategy extends RestartStrategy {
@Override @Override
public void restart(InjectionContext context) { protected void executeRestart(InjectionContext context) {
} }
} }
public static class ComplexRestartStrategy implements RestartStrategy { public static class ComplexRestartStrategy extends RestartStrategy {
private final ClassLoader classLoader; private final ClassLoader classLoader;
@@ -133,8 +132,7 @@ class RestartStrategyTest {
} }
@Override @Override
public void restart(InjectionContext context) { protected void executeRestart(InjectionContext context) {
} }
} }