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

@@ -21,7 +21,7 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.lifecycle;
//~--- non-JDK imports --------------------------------------------------------
@@ -103,7 +103,7 @@ public class BootstrapContextFilter extends GuiceFilter {
}
}
private class GuiceInjectionContext implements RestartStrategy.InjectionContext {
private class GuiceInjectionContext implements RestartStrategy.InternalInjectionContext {
@Override
public void initialize() {

View File

@@ -35,7 +35,7 @@ import java.util.function.IntConsumer;
* <p>
* 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);
@@ -45,6 +45,8 @@ class ExitRestartStrategy implements RestartStrategy {
private IntConsumer exiter = System::exit;
private int exitCode;
ExitRestartStrategy() {
}
@@ -54,12 +56,12 @@ class ExitRestartStrategy implements RestartStrategy {
}
@Override
public void restart(InjectionContext context) {
int exitCode = determineExitCode();
LOG.warn("destroy injection context");
context.destroy();
public void prepareRestart(InjectionContext context) {
exitCode = determineExitCode();
}
@Override
protected void executeRestart(InjectionContext context) {
LOG.warn("exit scm-manager with exit code {}", 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.
*/
class PosixRestartStrategy implements RestartStrategy {
class PosixRestartStrategy extends RestartStrategy {
private static final Logger LOG = LoggerFactory.getLogger(PosixRestartStrategy.class);
@@ -41,10 +41,7 @@ class PosixRestartStrategy implements RestartStrategy {
}
@Override
public void restart(InjectionContext context) {
LOG.warn("destroy injection context");
context.destroy();
protected void executeRestart(InjectionContext context) {
LOG.warn("restart scm-manager jvm process");
try {
restart();

View File

@@ -24,23 +24,20 @@
package sonia.scm.lifecycle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
/**
* 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.
*/
public interface RestartStrategy {
public abstract class RestartStrategy {
/**
* Context for Injection in SCM-Manager.
*/
interface InjectionContext {
/**
* Initialize the injection context.
*/
void initialize();
private static final Logger LOG = LoggerFactory.getLogger(RestartStrategy.class);
interface InternalInjectionContext extends InjectionContext {
/**
* 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
*/
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.