mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-02 02:10:53 +01:00
Re-add exit code restart strategy
This commit is contained in:
@@ -25,6 +25,7 @@ package sonia.scm.lifecycle;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.config.WebappConfigProvider;
|
||||
|
||||
/**
|
||||
* {@link RestartStrategy} which tears down the scm-manager context and
|
||||
@@ -49,7 +50,8 @@ class ExitRestartStrategy extends RestartStrategy {
|
||||
|
||||
@Override
|
||||
protected void executeRestart(InjectionContext context) {
|
||||
LOG.warn("exit scm-manager with exit code {}", 0);
|
||||
System.exit(0);
|
||||
Integer exitCode = WebappConfigProvider.resolveAsInteger("restart.exitCode").orElse(0);
|
||||
LOG.warn("exit scm-manager with exit code {}", exitCode);
|
||||
System.exit(exitCode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,18 +26,18 @@ package sonia.scm.lifecycle;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.google.common.base.Strings;
|
||||
import sonia.scm.Platform;
|
||||
import sonia.scm.config.WebappConfigProvider;
|
||||
import sonia.scm.util.SystemUtil;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
final class RestartStrategyFactory {
|
||||
|
||||
/**
|
||||
* System property to load a specific restart strategy.
|
||||
*/
|
||||
static final String PROPERTY_STRATEGY = "sonia.scm.lifecycle.restart-strategy";
|
||||
static final String RESTART_STRATEGY = "restart.strategy";
|
||||
|
||||
/**
|
||||
* No restart supported.
|
||||
@@ -46,13 +46,11 @@ final class RestartStrategyFactory {
|
||||
|
||||
private final Platform platform;
|
||||
private final Map<String, String> environment;
|
||||
private final Properties systemProperties;
|
||||
|
||||
@VisibleForTesting
|
||||
RestartStrategyFactory(Platform platform, Map<String, String> environment, Properties systemProperties) {
|
||||
RestartStrategyFactory(Platform platform, Map<String, String> environment) {
|
||||
this.platform = platform;
|
||||
this.environment = environment;
|
||||
this.systemProperties = systemProperties;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -64,28 +62,27 @@ final class RestartStrategyFactory {
|
||||
static RestartStrategy create(ClassLoader webAppClassLoader) {
|
||||
RestartStrategyFactory factory = new RestartStrategyFactory(
|
||||
SystemUtil.getPlatform(),
|
||||
System.getenv(),
|
||||
System.getProperties()
|
||||
System.getenv()
|
||||
);
|
||||
return factory.fromClassLoader(webAppClassLoader);
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
RestartStrategy fromClassLoader(ClassLoader webAppClassLoader) {
|
||||
String property = systemProperties.getProperty(PROPERTY_STRATEGY);
|
||||
if (Strings.isNullOrEmpty(property)) {
|
||||
String strategy = WebappConfigProvider.resolveAsString(RESTART_STRATEGY).orElse(null);
|
||||
if (Strings.isNullOrEmpty(strategy)) {
|
||||
return forPlatform();
|
||||
}
|
||||
return fromProperty(webAppClassLoader, property);
|
||||
return forStrategy(webAppClassLoader, strategy);
|
||||
}
|
||||
|
||||
private RestartStrategy fromProperty(ClassLoader webAppClassLoader, String property) {
|
||||
if (STRATEGY_NONE.equalsIgnoreCase(property)) {
|
||||
private RestartStrategy forStrategy(ClassLoader webAppClassLoader, String strategy) {
|
||||
if (STRATEGY_NONE.equalsIgnoreCase(strategy)) {
|
||||
return null;
|
||||
} else if (ExitRestartStrategy.NAME.equalsIgnoreCase(property)) {
|
||||
} else if (ExitRestartStrategy.NAME.equalsIgnoreCase(strategy)) {
|
||||
return new ExitRestartStrategy();
|
||||
} else {
|
||||
return fromClassName(property, webAppClassLoader);
|
||||
return fromClassName(strategy, webAppClassLoader);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -48,12 +48,12 @@ class DefaultRestarterTest {
|
||||
|
||||
@Test
|
||||
void shouldLoadStrategyOnCreation() {
|
||||
System.setProperty(RestartStrategyFactory.PROPERTY_STRATEGY, ExitRestartStrategy.NAME);
|
||||
System.setProperty(RestartStrategyFactory.RESTART_STRATEGY, ExitRestartStrategy.NAME);
|
||||
try {
|
||||
DefaultRestarter restarter = new DefaultRestarter();
|
||||
assertThat(restarter.isSupported()).isTrue();
|
||||
} finally {
|
||||
System.clearProperty(RestartStrategyFactory.PROPERTY_STRATEGY);
|
||||
System.clearProperty(RestartStrategyFactory.RESTART_STRATEGY);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,13 +29,14 @@ import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import sonia.scm.Platform;
|
||||
import sonia.scm.config.WebappConfigProvider;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -123,12 +124,12 @@ class RestartStrategyFactoryTest {
|
||||
}
|
||||
|
||||
private static Builder builder() {
|
||||
WebappConfigProvider.setConfigBindings(Collections.emptyMap());
|
||||
return new Builder();
|
||||
}
|
||||
|
||||
private static class Builder {
|
||||
|
||||
private final Properties properties = new Properties();
|
||||
private final Map<String, String> environment = new HashMap<>();
|
||||
private Platform platform = new Platform("Linux", "64Bit", "x64");
|
||||
|
||||
@@ -137,11 +138,11 @@ class RestartStrategyFactoryTest {
|
||||
}
|
||||
|
||||
public Builder withStrategy(String strategy) {
|
||||
return withProperty(RestartStrategyFactory.PROPERTY_STRATEGY, strategy);
|
||||
return withConfig(strategy);
|
||||
}
|
||||
|
||||
public Builder withProperty(String key, String value) {
|
||||
properties.setProperty(key, value);
|
||||
private Builder withConfig(String strategy) {
|
||||
WebappConfigProvider.setConfigBindings(Map.of(RestartStrategyFactory.RESTART_STRATEGY, strategy));
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -156,9 +157,7 @@ class RestartStrategyFactoryTest {
|
||||
}
|
||||
|
||||
public RestartStrategyFactory create() {
|
||||
return new RestartStrategyFactory(platform, environment, properties);
|
||||
return new RestartStrategyFactory(platform, environment);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user