ShellService: notify fixes, delete the scripts when they're no longer needed

This commit is contained in:
idk
2021-10-13 23:42:47 -04:00
parent 33c7ff8719
commit a0ab27491c
2 changed files with 51 additions and 21 deletions

View File

@ -1009,13 +1009,11 @@ public class PluginStarter implements Runnable {
if (group == null) {
String[] args = {"-shellservice.name", pluginName, "-shellservice.displayname", pluginName};
ShellService shellService = new ShellService(
ctx.getGlobalContext(),
ctx.getGlobalContext().clientAppManager(),
ctx,
ctx.clientAppManager(),
args
);
// Since we want to return true if the process is running, take the
// oposite of checkPidOfProcess here.
return !shellService.checkPidOfProcess();
return !shellService.checkIsStopped();
}
boolean rv = group.activeCount() > 0;

View File

@ -71,7 +71,6 @@ public class ShellService implements ClientApp {
_context = context;
_cmgr = listener;
_log = context.getGlobalContext().logManager().getLog(ShellService.class);
// _cmgr = context.getGlobalContext().clientAppManager();
String[] procArgs = trimArgs(args);
@ -99,24 +98,46 @@ public class ShellService implements ClientApp {
return Script;
}
private void deleteScript() {
File dir = _context.getTempDir();
if (SystemVersion.isWindows()) {
File bat = new File(dir, "shellservice-"+getName()+".bat");
if (bat.exists()) {
bat.delete();
}
} else {
File sh = new File(dir, "shellservice-"+getName()+".sh");
if (sh.exists()) {
sh.delete();
}
}
}
private String writeScript(String[] procArgs){
File dir = _context.getTempDir();
if (System.getProperty("os.name").indexOf("win") >= 0) {
if (SystemVersion.isWindows()) {
File bat = new File(dir, "shellservice-"+getName()+".bat");
if (bat.exists()) {
bat.delete();
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing Batch Script " + bat.toString());
FileWriter batWriter = null;
try {
bat.createNewFile();
FileWriter batWriter = new FileWriter(bat);
batWriter = new FileWriter(bat);
batWriter.write(batchScript(procArgs));
batWriter.close();
} catch (IOException ioe) {
if (_log.shouldLog(Log.ERROR))
_log.error("Error writing wrapper script shellservice-" + getName() + ".sh", ioe);
bat.delete();
} finally {
try {
batWriter.close();
} catch (IOException ioe) {
if (_log.shouldLog(Log.ERROR))
_log.error("Error writing wrapper script shellservice-" + getName() + ".sh", ioe);
}
}
bat.setExecutable(true);
return bat.getAbsolutePath();
@ -127,15 +148,22 @@ public class ShellService implements ClientApp {
}
if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing Shell Script " + sh.toString());
FileWriter shWriter = null;
try {
sh.createNewFile();
FileWriter shWriter = new FileWriter(sh);
shWriter = new FileWriter(sh);
shWriter.write(shellScript(procArgs));
shWriter.close();
} catch (IOException ioe) {
if (_log.shouldLog(Log.ERROR))
_log.error("Error writing wrapper script shellservice-" + getName() + ".sh", ioe);
sh.delete();
} finally {
try {
shWriter.close();
} catch (IOException ioe) {
if (_log.shouldLog(Log.ERROR))
_log.error("Error writing wrapper script shellservice-" + getName() + ".sh", ioe);
}
}
sh.setExecutable(true);
return sh.getAbsolutePath();
@ -260,7 +288,7 @@ public class ShellService implements ClientApp {
private static boolean isProcessIdRunning(String pid) {
boolean running = false;
if (System.getProperty("os.name").indexOf("win") >= 0) {
if (SystemVersion.isWindows()) {
running = isProcessIdRunningOnWindows(pid);
} else {
running = isProcessIdRunningOnUnix(pid);
@ -353,7 +381,7 @@ public class ShellService implements ClientApp {
if (shellService._log.shouldLog(Log.ERROR))
shellService._log.error("Error while launching " + shellService.getName(), t);
}finally{
shellService.checkPidOfProcess();
shellService.checkIsStopped();
}
}
@ -363,7 +391,7 @@ public class ShellService implements ClientApp {
* notify the router that it has been started.
*/
public void startup() throws Throwable {
boolean start = checkPidOfProcess();
boolean start = checkIsStopped();
if (start) {
try {
if (_log.shouldLog(Log.DEBUG))
@ -387,9 +415,10 @@ public class ShellService implements ClientApp {
_log.error("Error getting PID of application from recently instantiated shellservice" + getName());
if (_log.shouldLog(Log.DEBUG))
_log.debug("Started " + getName() + "process with PID: " + pid);
writeDownPID(pid);
writeDownPID(pid);
deleteScript();
}
_cmgr.notify();
_cmgr.notify(this, getState(), "ShellService " + getName() + " Started", null);
return;
}
@ -404,7 +433,7 @@ public class ShellService implements ClientApp {
*
* @return {@code true} if the PID is NOT running, {@code false} if the PID is running
*/
public boolean checkPidOfProcess() {
public boolean checkIsStopped() {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Checking process status " + getName());
String oldPid = readInPID();
@ -440,7 +469,7 @@ public class ShellService implements ClientApp {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Stopping " + getName() + "process started with ShellService, PID: " + pid);
_p.destroy();
} else if (System.getProperty("os.name").indexOf("win") >= 0) {
} else if (SystemVersion.isWindows()) {
if (_log.shouldLog(Log.DEBUG))
_log.debug("Stopping " + getName() + "process with PID: " + pid + "on Windows");
String cmd[] = {"cmd", "/c", "taskkill /F /T /PID " + pid};
@ -451,8 +480,9 @@ public class ShellService implements ClientApp {
String cmd[] = {"kill", pid};
Runtime.getRuntime().exec(cmd);
}
checkPidOfProcess();
_cmgr.notify();
checkIsStopped();
deleteScript();
_cmgr.notify(this, getState(), "ShellService "+getName()+" Stopped", null);
}
/**
@ -466,8 +496,10 @@ public class ShellService implements ClientApp {
if (_pb == null || _context == null || _cmgr == null)
return ClientAppState.UNINITIALIZED;
String pid = readInPID();
if (pid.equals("") && _log.shouldLog(Log.ERROR))
if (pid.equals("") && _log.shouldLog(Log.ERROR)) {
_log.error("Error getting PID of application from shellservice-" + getName() + ".pid");
return ClientAppState.STOPPED;
}
if (isProcessIdRunning(pid)) {
return ClientAppState.RUNNING;
} else {