Archived
0

Zond: детектирование проблемного запуска процесса

This commit is contained in:
2017-07-22 18:33:47 +03:00
parent 2a1aad2f9f
commit 9bfb222787
4 changed files with 64 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys' group = 'asys'
version = '0.7.9-SNAPSHOT' version = '0.7.10-SNAPSHOT'
apply plugin: 'application' apply plugin: 'application'

View File

@@ -55,4 +55,12 @@ public class Config {
return 0; return 0;
} }
} }
public long getLong(String key) {
try {
return Long.parseLong(properties.getProperty(key));
} catch (NumberFormatException e) {
return 0;
}
}
} }

View File

@@ -36,6 +36,7 @@ public class PingMonitor {
lastPingTime = 0; lastPingTime = 0;
}); });
this.threadPingMon.start();
} }
public void stop() { public void stop() {

View File

@@ -22,6 +22,7 @@ public class ZondCommandHandler implements CommandHandler {
private Thread threadExec; private Thread threadExec;
private Server server; private Server server;
private PipeInputStream proxyStdIn; private PipeInputStream proxyStdIn;
private PingMonitor pingMonitor;
ZondCommandHandler(PipeInputStream proxyStdIn) { ZondCommandHandler(PipeInputStream proxyStdIn) {
this.proxyStdIn = proxyStdIn; this.proxyStdIn = proxyStdIn;
@@ -47,31 +48,7 @@ public class ZondCommandHandler implements CommandHandler {
Connector.getInstance().shutdown(); Connector.getInstance().shutdown();
Shell.getInstance().shutdown(); Shell.getInstance().shutdown();
} else if (line.equalsIgnoreCase("start")) { } else if (line.equalsIgnoreCase("start")) {
if (watchdog == null) { startProcess();
watchdog = new ZondExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT, proxyStdIn);
executor.setWatchdog(watchdog);
Runnable task = () -> {
int code = 0;
try {
server = new Server();
server.start(Config.getInstance().getInt("bridge.port"));
code = executor.execute(commandLine);
} catch (ExecuteException ignore) {
} catch (IOException e) {
Shell.getInstance().getOutput().println("Exception message: " + e.getMessage());
code = -99;
}
server.shutdown();
server = null;
watchdog = null;
Shell.getInstance().getOutput().println("Process finished. Code: " + code);
};
threadExec = new Thread(task, "Zond Exec");
threadExec.start();
}
} else if (line.equalsIgnoreCase("kill")) { } else if (line.equalsIgnoreCase("kill")) {
if (watchdog != null) { if (watchdog != null) {
server.shutdown(); server.shutdown();
@@ -97,4 +74,56 @@ public class ZondCommandHandler implements CommandHandler {
this.executor = executor; this.executor = executor;
this.commandLine = commandLine; this.commandLine = commandLine;
} }
private void startProcess() {
if (watchdog == null) {
watchdog = new ZondExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT, proxyStdIn);
executor.setWatchdog(watchdog);
final long delay = Config.getInstance().getLong("pingmonitor.delay");
pingMonitor = new PingMonitor(delay);
Runnable task = () -> {
short _try = 0;
do {
int code = 0;
long deadTime = System.currentTimeMillis() + delay;
try {
server = new Server();
server.start(Config.getInstance().getInt("bridge.port"));
pingMonitor.start(() -> {});
code = executor.execute(commandLine);
} catch (ExecuteException ignore) {
} catch (IOException e) {
Shell.getInstance().getOutput().println("Exception message: " + e.getMessage());
code = -99;
}
server.shutdown();
pingMonitor.stop();
server = null;
watchdog = null;
Shell.getInstance().getOutput().println("Process finished. Code: " + code);
if (System.currentTimeMillis() <= deadTime) {
Shell.getInstance().getOutput().println("[!] Premature end process.");
_try++;
if (_try < 2) {
Shell.getInstance().getOutput().println("[!] Try start process again...");
}
} else {
break;
}
} while (_try < 2);
if (_try == 2) {
Shell.getInstance().getOutput().println("[!] Discovered the problem when starting the process");
}
};
threadExec = new Thread(task, "Zond Exec");
threadExec.start();
}
}
} }