implemented restart strategy for windows services

This commit is contained in:
Sebastian Sdorra
2020-05-03 11:23:42 +02:00
parent 6e120cdd00
commit bac253d276
4 changed files with 240 additions and 151 deletions

View File

@@ -23,11 +23,14 @@
*/
package sonia.scm.lifecycle;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Strings;
import sonia.scm.PlatformType;
import sonia.scm.Platform;
import sonia.scm.util.SystemUtil;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.Properties;
final class RestartStrategyFactory {
@@ -41,7 +44,15 @@ final class RestartStrategyFactory {
*/
static final String STRATEGY_NONE = "none";
private 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) {
this.platform = platform;
this.environment = environment;
this.systemProperties = systemProperties;
}
/**
@@ -51,14 +62,24 @@ final class RestartStrategyFactory {
* @return configured strategy or {@code null}
*/
static RestartStrategy create(ClassLoader webAppClassLoader) {
String property = System.getProperty(PROPERTY_STRATEGY);
RestartStrategyFactory factory = new RestartStrategyFactory(
SystemUtil.getPlatform(),
System.getenv(),
System.getProperties()
);
return factory.fromClassLoader(webAppClassLoader);
}
@VisibleForTesting
RestartStrategy fromClassLoader(ClassLoader webAppClassLoader) {
String property = systemProperties.getProperty(PROPERTY_STRATEGY);
if (Strings.isNullOrEmpty(property)) {
return forPlatform();
}
return fromProperty(webAppClassLoader, property);
}
private static RestartStrategy fromProperty(ClassLoader webAppClassLoader, String property) {
private RestartStrategy fromProperty(ClassLoader webAppClassLoader, String property) {
if (STRATEGY_NONE.equalsIgnoreCase(property)) {
return null;
} else if (ExitRestartStrategy.NAME.equalsIgnoreCase(property)) {
@@ -68,7 +89,7 @@ final class RestartStrategyFactory {
}
}
private static RestartStrategy fromClassName(String className, ClassLoader classLoader) {
private RestartStrategy fromClassName(String className, ClassLoader classLoader) {
try {
Class<? extends RestartStrategy> rsClass = Class.forName(className).asSubclass(RestartStrategy.class);
return createInstance(rsClass, classLoader);
@@ -77,7 +98,7 @@ final class RestartStrategyFactory {
}
}
private static RestartStrategy createInstance(Class<? extends RestartStrategy> rsClass, ClassLoader classLoader) throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchMethodException {
private RestartStrategy createInstance(Class<? extends RestartStrategy> rsClass, ClassLoader classLoader) throws InstantiationException, IllegalAccessException, java.lang.reflect.InvocationTargetException, NoSuchMethodException {
try {
Constructor<? extends RestartStrategy> constructor = rsClass.getConstructor(ClassLoader.class);
return constructor.newInstance(classLoader);
@@ -86,12 +107,11 @@ final class RestartStrategyFactory {
}
}
private static RestartStrategy forPlatform() {
// we do not use SystemUtil here, to allow testing
String osName = System.getProperty(SystemUtil.PROPERTY_OSNAME);
PlatformType platform = PlatformType.createPlatformType(osName);
private RestartStrategy forPlatform() {
if (platform.isPosix()) {
return new PosixRestartStrategy();
} else if (platform.isWindows() && WinSWRestartStrategy.isSupported(environment)) {
return new WinSWRestartStrategy();
}
return null;
}

View File

@@ -1,4 +1,51 @@
package sonia.scm.lifecycle;
public class WinSWRestartStrategy {
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.Map;
/**
* A {@link RestartStrategy} which can be used if scm-manager was started as windows
* service with WinSW.
*
* @see <a href="https://github.com/winsw/winsw/blob/master/doc/selfRestartingService.md">Self-restarting Windows services</a>
*/
class WinSWRestartStrategy extends RestartStrategy {
private static final Logger LOG = LoggerFactory.getLogger(WinSWRestartStrategy.class);
static final String ENV_EXECUTABLE = "WINSW_EXECUTABLE";
@Override
@SuppressWarnings("java:S2142")
protected void executeRestart(InjectionContext context) {
String executablePath = System.getenv(ENV_EXECUTABLE);
try {
int rs = execute(executablePath);
if (rs != 0) {
LOG.error("winsw {} returned status code {}", executablePath, rs);
}
} catch (IOException | InterruptedException e) {
LOG.error("failed to execute winsw at {}", executablePath, e);
}
LOG.error("scm-manager is in an unrecoverable state, we will now exit the java process");
System.exit(1);
}
private int execute(String executablePath) throws InterruptedException, IOException {
return new ProcessBuilder(executablePath, "restart!").start().waitFor();
}
static boolean isSupported(Map<String, String> environment) {
String executablePath = environment.get(ENV_EXECUTABLE);
if (Strings.isNullOrEmpty(executablePath)) {
return false;
}
File exe = new File(executablePath);
return exe.exists();
}
}