void refreshState() { if (process.isAlive()) { if (!operational.get() && process.isOperational()) { operational.set(true); eventListeners.forEach(l -> l.onProcessEvent(processId, ProcessEventListener.Type.OPERATIONAL)); } if (process.askedForRestart()) { process.acknowledgeAskForRestart(); eventListeners.forEach(l -> l.onProcessEvent(processId, ProcessEventListener.Type.ASK_FOR_RESTART)); } } else { stopForcibly(); } }
private void stopGracefully(long timeout, TimeUnit timeoutUnit) { if (process == null) { return; } try { // request graceful stop process.askForStop(); process.waitFor(timeout, timeoutUnit); } catch (InterruptedException e) { // can't wait for the termination of process. Let's assume it's down. LOG.warn("Interrupted while stopping process {}", processId, e); Thread.currentThread().interrupt(); } catch (Throwable e) { LOG.error("Can not ask for graceful stop of process {}", processId, e); } }
public void stopForcibly() { eventWatcher.interrupt(); stopWatcher.interrupt(); if (process != null) { process.destroyForcibly(); waitForDown(); process.closeStreams(); } if (stdOutGobbler != null) { StreamGobbler.waitUntilFinish(stdOutGobbler); stdOutGobbler.interrupt(); } if (stdErrGobbler != null) { StreamGobbler.waitUntilFinish(stdErrGobbler); stdErrGobbler.interrupt(); } lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); }
public boolean start(Supplier<ProcessMonitor> commandLauncher) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { // has already been started return false; } try { this.process = commandLauncher.get(); } catch (RuntimeException e) { LOG.error("Fail to launch process [{}]", processId.getKey(), e); lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); throw e; } this.stdOutGobbler = new StreamGobbler(process.getInputStream(), processId.getKey()); this.stdOutGobbler.start(); this.stdErrGobbler = new StreamGobbler(process.getErrorStream(), processId.getKey()); this.stdErrGobbler.start(); this.stopWatcher.start(); this.eventWatcher.start(); // Could be improved by checking the status "up" in shared memory. // Not a problem so far as this state is not used by listeners. lifecycle.tryToMoveTo(Lifecycle.State.STARTED); return true; }
@Override public void run() { try { while (process.isAlive()) { refreshState(); Thread.sleep(watcherDelayMs); } } catch (InterruptedException e) { // request to stop watching process. To avoid unexpected behaviors // the process is stopped. Thread.currentThread().interrupt(); stopForcibly(); } } }
@Override public void run() { try { process.waitFor(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // stop watching process } stopForcibly(); } }
public void stopForcibly() { eventWatcher.interrupt(); stopWatcher.interrupt(); if (process != null) { process.destroyForcibly(); waitForDown(); process.closeStreams(); } if (stdOutGobbler != null) { StreamGobbler.waitUntilFinish(stdOutGobbler); stdOutGobbler.interrupt(); } if (stdErrGobbler != null) { StreamGobbler.waitUntilFinish(stdErrGobbler); stdErrGobbler.interrupt(); } lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); }
public boolean start(Supplier<ProcessMonitor> commandLauncher) { if (!lifecycle.tryToMoveTo(Lifecycle.State.STARTING)) { // has already been started return false; } try { this.process = commandLauncher.get(); } catch (RuntimeException e) { LOG.error("Fail to launch process [{}]", processId.getKey(), e); lifecycle.tryToMoveTo(Lifecycle.State.STOPPED); throw e; } this.stdOutGobbler = new StreamGobbler(process.getInputStream(), processId.getKey()); this.stdOutGobbler.start(); this.stdErrGobbler = new StreamGobbler(process.getErrorStream(), processId.getKey()); this.stdErrGobbler.start(); this.stopWatcher.start(); this.eventWatcher.start(); // Could be improved by checking the status "up" in shared memory. // Not a problem so far as this state is not used by listeners. lifecycle.tryToMoveTo(Lifecycle.State.STARTED); return true; }
/** * Sends kill signal and awaits termination. No guarantee that process is gracefully terminated (=shutdown hooks * executed). It depends on OS. */ public void stop(long timeout, TimeUnit timeoutUnit) { if (lifecycle.tryToMoveTo(Lifecycle.State.STOPPING)) { stopGracefully(timeout, timeoutUnit); if (process != null && process.isAlive()) { LOG.info("{} failed to stop in a timely fashion. Killing it.", processId.getKey()); } // enforce stop and clean-up even if process has been gracefully stopped stopForcibly(); } else { // already stopping or stopped waitForDown(); } }
@Override public void run() { try { process.waitFor(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); // stop watching process } stopForcibly(); } }
void refreshState() { if (process.isAlive()) { if (!operational.get() && process.isOperational()) { operational.set(true); eventListeners.forEach(l -> l.onProcessEvent(processId, ProcessEventListener.Type.OPERATIONAL)); } if (process.askedForRestart()) { process.acknowledgeAskForRestart(); eventListeners.forEach(l -> l.onProcessEvent(processId, ProcessEventListener.Type.ASK_FOR_RESTART)); } } else { stopForcibly(); } }
private void stopGracefully(long timeout, TimeUnit timeoutUnit) { if (process == null) { return; } try { // request graceful stop process.askForStop(); process.waitFor(timeout, timeoutUnit); } catch (InterruptedException e) { // can't wait for the termination of process. Let's assume it's down. LOG.warn("Interrupted while stopping process {}", processId, e); Thread.currentThread().interrupt(); } catch (Throwable e) { LOG.error("Can not ask for graceful stop of process {}", processId, e); } }
@Override public void run() { try { while (process.isAlive()) { refreshState(); Thread.sleep(watcherDelayMs); } } catch (InterruptedException e) { // request to stop watching process. To avoid unexpected behaviors // the process is stopped. Thread.currentThread().interrupt(); stopForcibly(); } } }
/** * Sends kill signal and awaits termination. No guarantee that process is gracefully terminated (=shutdown hooks * executed). It depends on OS. */ public void stop(long timeout, TimeUnit timeoutUnit) { if (lifecycle.tryToMoveTo(Lifecycle.State.STOPPING)) { stopGracefully(timeout, timeoutUnit); if (process != null && process.isAlive()) { LOG.info("{} failed to stop in a timely fashion. Killing it.", processId.getKey()); } // enforce stop and clean-up even if process has been gracefully stopped stopForcibly(); } else { // already stopping or stopped waitForDown(); } }