implemented restarter to move control over the restart process to the core

This commit is contained in:
Sebastian Sdorra
2020-02-12 14:45:13 +01:00
parent bca34b829d
commit de3db6252e
12 changed files with 201 additions and 69 deletions

View File

@@ -0,0 +1,41 @@
package sonia.scm.lifecycle;
import com.google.common.annotations.VisibleForTesting;
import sonia.scm.event.ScmEventBus;
import javax.inject.Inject;
import javax.inject.Singleton;
@Singleton
public class DefaultRestarter implements Restarter {
private ScmEventBus eventBus;
private RestartStrategy strategy;
@Inject
public DefaultRestarter() {
this(
ScmEventBus.getInstance(),
RestartStrategy.get(Thread.currentThread().getContextClassLoader()).orElse(null)
);
}
@VisibleForTesting
DefaultRestarter(ScmEventBus eventBus, RestartStrategy strategy) {
this.eventBus = eventBus;
this.strategy = strategy;
}
@Override
public boolean isSupported() {
return strategy != null;
}
@Override
public void restart(Class<?> cause, String reason) {
if (!isSupported()) {
throw new RestartNotSupportedException("restarting is not supported");
}
eventBus.post(new RestartEvent(cause, reason));
}
}

View File

@@ -1,10 +0,0 @@
package sonia.scm.lifecycle;
/**
* Exception is thrown if a restart is not supported or a restart strategy is misconfigured.
*/
public class RestartNotSupportedException extends RuntimeException {
RestartNotSupportedException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -9,6 +9,8 @@ import sonia.scm.SCMContext;
import sonia.scm.SCMContextProvider;
import sonia.scm.io.DefaultFileSystem;
import sonia.scm.io.FileSystem;
import sonia.scm.lifecycle.DefaultRestarter;
import sonia.scm.lifecycle.Restarter;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.repository.RepositoryLocationResolver;
import sonia.scm.repository.xml.MetadataStore;
@@ -61,6 +63,8 @@ public class BootstrapModule extends AbstractModule {
bind(FileSystem.class, DefaultFileSystem.class);
bind(Restarter.class, DefaultRestarter.class);
// note CipherUtil uses an other generator
bind(CipherHandler.class).toInstance(CipherUtil.getInstance().getCipherHandler());

View File

@@ -41,6 +41,7 @@ import org.slf4j.LoggerFactory;
import sonia.scm.NotFoundException;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.RestartEvent;
import sonia.scm.lifecycle.Restarter;
import sonia.scm.version.Version;
import javax.inject.Inject;
@@ -68,20 +69,21 @@ public class DefaultPluginManager implements PluginManager {
private static final Logger LOG = LoggerFactory.getLogger(DefaultPluginManager.class);
private final ScmEventBus eventBus;
private final PluginLoader loader;
private final PluginCenter center;
private final PluginInstaller installer;
private final Restarter restarter;
private final Collection<PendingPluginInstallation> pendingInstallQueue = new ArrayList<>();
private final Collection<PendingPluginUninstallation> pendingUninstallQueue = new ArrayList<>();
private final PluginDependencyTracker dependencyTracker = new PluginDependencyTracker();
@Inject
public DefaultPluginManager(ScmEventBus eventBus, PluginLoader loader, PluginCenter center, PluginInstaller installer) {
this.eventBus = eventBus;
public DefaultPluginManager(PluginLoader loader, PluginCenter center, PluginInstaller installer, Restarter restarter) {
this.loader = loader;
this.center = center;
this.installer = installer;
this.restarter = restarter;
this.computeInstallationDependencies();
}
@@ -242,16 +244,8 @@ public class DefaultPluginManager implements PluginManager {
}
}
@VisibleForTesting
void triggerRestart(String cause) {
new Thread(() -> {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
eventBus.post(new RestartEvent(PluginManager.class, cause));
}).start();
private void triggerRestart(String cause) {
restarter.restart(PluginManager.class, cause);
}
private void cancelPending(List<PendingPluginInstallation> pendingInstallations) {

View File

@@ -12,6 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.event.ScmEventBus;
import sonia.scm.lifecycle.RestartEvent;
import sonia.scm.lifecycle.Restarter;
import sonia.scm.update.repository.DefaultMigrationStrategyDAO;
import sonia.scm.update.repository.MigrationStrategy;
import sonia.scm.update.repository.V1Repository;
@@ -41,11 +42,13 @@ class MigrationWizardServlet extends HttpServlet {
private final XmlRepositoryV1UpdateStep repositoryV1UpdateStep;
private final DefaultMigrationStrategyDAO migrationStrategyDao;
private final Restarter restarter;
@Inject
MigrationWizardServlet(XmlRepositoryV1UpdateStep repositoryV1UpdateStep, DefaultMigrationStrategyDAO migrationStrategyDao) {
MigrationWizardServlet(XmlRepositoryV1UpdateStep repositoryV1UpdateStep, DefaultMigrationStrategyDAO migrationStrategyDao, Restarter restarter) {
this.repositoryV1UpdateStep = repositoryV1UpdateStep;
this.migrationStrategyDao = migrationStrategyDao;
this.restarter = restarter;
}
@Override
@@ -121,7 +124,12 @@ class MigrationWizardServlet extends HttpServlet {
ThreadContext.bind(new Subject.Builder(new DefaultSecurityManager()).authenticated(false).buildSubject());
ScmEventBus.getInstance().post(new RestartEvent(MigrationWizardServlet.class, "wrote migration data"));
if (restarter.isSupported()) {
restarter.restart(MigrationWizardServlet.class, "wrote migration data");
} else {
LOG.error("Restarting is not supported on this platform.");
LOG.error("Please do a manual restart");
}
}
private List<RepositoryLineEntry> getRepositoryLineEntries() {