Skip to content

Commit

Permalink
0003525: Service wrapper: remove reliance on SYM_HOME environment
Browse files Browse the repository at this point in the history
variable
  • Loading branch information
erilong committed Apr 20, 2018
1 parent 00d9f9b commit 22aa3fa
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 34 deletions.
Expand Up @@ -26,12 +26,6 @@ public enum Status {
START_PENDING, RUNNING, STOP_PENDING, STOPPED;
}

public static final String ENV_SYM_HOME = "SYM_HOME";

public static final String SYSPROP_TMPDIR = "java.io.tmpdir";

public static final String JAR_NAME = "symmetric-wrapper.jar";

public static final int RC_BAD_USAGE = 1;
public static final int RC_INVALID_ARGUMENT = 2;
public static final int RC_MISSING_CONFIG_FILE = 3;
Expand All @@ -53,7 +47,6 @@ public enum Status {
public static final int RC_NATIVE_ERROR = 19;
public static final int RC_MISSING_LIB_FOLDER = 20;
public static final int RC_MISSING_SERVER_PROPERTIES = 21;
public static final int RC_MISSING_SYM_HOME = 22;
public static final int RC_FAIL_CHECK_STATUS = 23;
public static final int RC_ALREADY_RUNNING = 24;

Expand Down
Expand Up @@ -147,19 +147,12 @@ protected boolean isPidRunning(int pid) {
if (procFile.canRead()) {
try {
List<String> args = readProcFile(procFile);
String appName = config.getApplicationParameters().get(0);
boolean isJava = false;
boolean isMe = false;

for (String arg : args) {
if (arg.contains(config.getJavaCommand())) {
isJava = true;
}
if (arg.contains(appName) || arg.contains(Constants.JAR_NAME)) {
isMe = true;
return true;
}
}
return isJava && isMe;
return false;

This comment has been minimized.

Copy link
@matthoward

matthoward Mar 11, 2019

Contributor

@erilong Do you happen to remember why you removed this extra check on the appName? We have some machines that sometimes are not properly shutdown and when that happens they occasionally fail to actually run the sym service after reboot. We believe what is happening is that the server PID from before the shutdown occasionally gets re-used, and causes this code to incorrectly attempt to kill that other PID so maybe the kill command is throwing an exception or something. It seems like these extra checks in isPidRunning that were removed might solve that problem.

This comment has been minimized.

Copy link
@erilong

erilong Mar 11, 2019

Author Contributor

The changes were making symmetric-wrapper more generic so it can be used in another project that might have a different JAR name. Maybe I should have left the application parameter, it could probably still check that if it's being used. It is still checking that the command includes the Java executable, so it would have to be another process running as Java for it to accidentally think it's still running, which seems unlikely. The kill command at the bottom of that method is passing a 0 for the signal, which means don't send a signal, just check for the existence of the process and do error checking that we can access it.

} catch (IOException e) {
}
}
Expand Down
Expand Up @@ -21,30 +21,113 @@
package org.jumpmind.symmetric.wrapper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Wrapper {

public static void main(String[] args) throws Exception {

String dir = System.getenv(Constants.ENV_SYM_HOME);

if (dir == null || dir.equals("")) {
// Backwards compatible with 3.6 by allowing config file argument to determine home
if (args.length > 1) {
int index = args[1].lastIndexOf(File.separator);
if (index == -1) {
dir = "..";
} else {
dir = args[1].substring(0, index + 1) + "..";
}

if (args.length < 1) {
printUsage();
System.exit(Constants.RC_BAD_USAGE);
}

String appDir = null;
String configFile = null;
String jarFile = Wrapper.class.getProtectionDomain().getCodeSource().getLocation().getFile();

if (args.length > 1) {
configFile = args[1];
appDir = getParentDir(configFile);
} else {
appDir = getParentDir(jarFile);
configFile = findConfigFile(appDir + File.separator + "conf");
}

WrapperService service = WrapperService.getInstance();
try {
service.loadConfig(appDir, configFile, jarFile);
} catch (FileNotFoundException e) {
System.out.println("Missing config file " + configFile);
System.out.println(e.getMessage());
System.exit(Constants.RC_MISSING_CONFIG_FILE);
} catch (IOException e) {
System.out.println("Cannot read config file " + configFile);
System.out.println(e.getMessage());
System.exit(Constants.RC_FAIL_READ_CONFIG_FILE);
}

try {
if (args[0].equalsIgnoreCase("start")) {
service.start();
} else if (args[0].equalsIgnoreCase("exec")) {
service.execJava(false);
} else if (args[0].equalsIgnoreCase("init")) {
service.init();
} else if (args[0].equalsIgnoreCase("stop")) {
service.stop();
} else if (args[0].equalsIgnoreCase("restart")) {
service.restart();
} else if (args[0].equalsIgnoreCase("install")) {
service.install();
} else if (args[0].equalsIgnoreCase("uninstall")) {
service.uninstall();
} else if (args[0].equalsIgnoreCase("status")) {
service.status();
} else if (args[0].equalsIgnoreCase("console")) {
service.console();
} else {
System.out.println("Missing " + Constants.ENV_SYM_HOME + " environment variable.");
System.exit(Constants.RC_MISSING_SYM_HOME);
System.out.println("ERROR: Invalid argument");
printUsage();
System.exit(Constants.RC_INVALID_ARGUMENT);
}
} catch (WrapperException e) {
System.out.println("Error " + e.getErrorCode() + ": " + e.getMessage());
if (e.getCause() != null) {
System.out.println("Exception " + e.getCause().getClass().getSimpleName() + ": "
+ e.getCause().getMessage());
}
if (e.getNativeErrorCode() > 0) {
System.out.println("Native error " + e.getErrorCode());
}
System.exit(e.getErrorCode());
} catch (Throwable ex) {
ex.printStackTrace();
System.exit(-1);
}
String configFile = dir + File.separator + "conf" + File.separator + "sym_service.conf";
String jarFile = dir + File.separator + "lib" + File.separator + Constants.JAR_NAME;

WrapperHelper.run(args, dir, configFile, jarFile);
}

protected static String getParentDir(String filepath) {
int index = filepath.lastIndexOf(File.separator);
if (index == -1) {
return "..";
} else {
return filepath.substring(0, index + 1) + "..";
}
}

protected static String findConfigFile(String dirpath) {
File dir = new File(dirpath);
if (dir.exists() && dir.isDirectory()) {
for (String name : dir.list()) {
if (name.endsWith("_service.conf")) {
return dirpath + File.separator + name;
}
}
}
return dirpath + File.separator + "wrapper_service.conf";
}

protected static void printUsage() {
System.out.println("Usage: <start|stop|restart|install|remove|console>");
System.out.println(" start - Start service");
System.out.println(" stop - Stop service");
System.out.println(" restart - Restart service");
System.out.println(" install - Install service");
System.out.println(" uninstall - Uninstall service");
System.out.println(" status - Status of service");
System.out.println(" console - Run from console");
}

}

0 comments on commit 22aa3fa

Please sign in to comment.