Skip to content

Commit baa2db4

Browse files
committed
Stop command handling: await grace period after stop command
Changes the way the stop command is treated. With this change, the stop command can either be synchronous or asynchronous. Before it was only possible to use synchronous commands, as the main process was destroyed immediately after the stop command exited. Now the grace period is awaited in case an asynchronous trigger causes the main process to exit within that period. Request: MO Change-Id: Ia72eb54f8c895b19e1a117a5e6a953206db30ce5
1 parent f533e89 commit baa2db4

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pcu/src/main/java/io/bdeploy/pcu/ProcessController.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ private void doInvokeStopCommand(List<String> stopCommand) {
830830
}
831831
try {
832832
logger.log(l -> l.info("Invoking configured stop command {}.", stopCommand));
833+
long stopCommandStartTime = System.currentTimeMillis();
833834
Process p = launch(stopCommand, true);
834835
boolean exited = p.waitFor(processConfig.processControl.gracePeriod, TimeUnit.MILLISECONDS);
835836
if (!exited) {
@@ -845,9 +846,22 @@ private void doInvokeStopCommand(List<String> stopCommand) {
845846
// stop command completed within the timeout, check status
846847
int exitValue = p.exitValue();
847848
if (exitValue != 0) {
848-
logger.log(l -> l.warn("Stop command exited with non-zero code: {} ", exitValue));
849+
logger.log(l -> l.warn("Stop command exited with non-zero code: {}", exitValue));
849850
} else {
850-
logger.log(l -> l.info("Stop command exited with return code 0 "));
851+
long remainingGracePeriod = processConfig.processControl.gracePeriod
852+
- (System.currentTimeMillis() - stopCommandStartTime);
853+
logger.log(l -> l.info("Stop command exited with return code 0, remaining grace period: {}",
854+
remainingGracePeriod));
855+
856+
// stop command exited, lets give the process a little extra time to exit as well.
857+
try {
858+
processExit.get(remainingGracePeriod, TimeUnit.MILLISECONDS);
859+
logger.log(l -> l.info("Stop command successfullly terminated main process"));
860+
} catch (TimeoutException e) {
861+
logger.log(l -> l.warn("Main process did not exit after stop grace period"));
862+
} catch (Exception e) {
863+
logger.log(l -> l.warn("Exception while waiting for application to terminate.", e));
864+
}
851865
}
852866
}
853867
} catch (Exception e) {

0 commit comments

Comments
 (0)