private void stopProcesses() { List<WatcherThread> watcherThreadsCopy = new ArrayList<>(this.watcherThreads); // create a copy and reverse it to terminate in reverse order of startup (dependency order) Collections.reverse(watcherThreadsCopy); for (WatcherThread watcherThread : watcherThreadsCopy) { ProcessRef ref = watcherThread.getProcessRef(); if (!ref.isStopped()) { LOG.info("{} is stopping", ref); ref.askForGracefulAsyncStop(); long killAt = System.currentTimeMillis() + TIMEOUTS.getTerminationTimeout(); while (!ref.isStopped() && System.currentTimeMillis() < killAt) { try { Thread.sleep(10L); } catch (InterruptedException e) { // stop asking for graceful stops, Monitor will hardly kill all processes break; } } if (!ref.isStopped()) { LOG.info("{} failed to stop in a timely fashion. Killing it.", ref); } ref.stop(); LOG.info("{} is stopped", ref); } } // all processes are stopped, no need to keep references to these WatcherThread anymore trace("all processes stopped, clean list of watcherThreads..."); this.watcherThreads.clear(); }
private boolean didAnyProcessRequestRestart() { for (WatcherThread watcherThread : watcherThreads) { ProcessRef processRef = watcherThread.getProcessRef(); if (processRef.getCommands().askedForRestart()) { LOG.info("Process [{}] requested restart", processRef.getKey()); return true; } } return false; }