RuffyScripter: Abort a running command after 90s timeout

This commit is contained in:
Johannes Mockenhaupt 2017-07-14 20:24:32 +02:00
parent f34fed1f05
commit 0729d7a114
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -113,6 +113,9 @@ public class RuffyScripter {
} }
ensureConnected(); ensureConnected();
// TODO reuse thread, scheduler ...
Thread cmdThread;
// TODO make this a safe lock // TODO make this a safe lock
synchronized (this) { synchronized (this) {
cmdResult = null; cmdResult = null;
@ -122,7 +125,7 @@ public class RuffyScripter {
// wait till pump is ready for input // wait till pump is ready for input
waitForMenuUpdate(); waitForMenuUpdate();
log.debug("Cmd execution: connection ready, executing cmd " + cmd); log.debug("Cmd execution: connection ready, executing cmd " + cmd);
new Thread(new Runnable() { cmdThread = new Thread(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
@ -134,16 +137,25 @@ public class RuffyScripter {
} }
} }
}).start(); });
cmdThread.start();
} }
// TODO really? // TODO really?
long timeout = System.currentTimeMillis() + 90 * 1000;
while (activeCmd != null) { while (activeCmd != null) {
SystemClock.sleep(500); SystemClock.sleep(500);
log.trace("Waiting for running command to complete"); log.trace("Waiting for running command to complete");
if (System.currentTimeMillis() > timeout) {
log.error("Running command " + activeCmd + " timed out");
cmdThread.interrupt();
activeCmd = null;
cmdResult = null;
return new CommandResult().success(false).enacted(false).message("Command timed out");
}
} }
log.debug("Command result: " + cmdResult);
log.debug("Command result: " + cmdResult);
CommandResult r = cmdResult; CommandResult r = cmdResult;
cmdResult = null; cmdResult = null;
return r; return r;
@ -162,10 +174,11 @@ public class RuffyScripter {
} }
try { try {
boolean connectSuccesful = ruffyService.doRTConnect() == 0; boolean connectInitSuccessful = ruffyService.doRTConnect() == 0;
log.debug("Connect init successful: " + connectSuccesful); log.debug("Connect init successful: " + connectInitSuccessful);
while (currentMenu == null) { while (currentMenu == null) {
log.debug("Waiting for first menu update to be sent"); log.debug("Waiting for first menu update to be sent");
// waitForMenuUpdate times out after 90s and throws a CommandException
waitForMenuUpdate(); waitForMenuUpdate();
} }
} catch (RemoteException e) { } catch (RemoteException e) {