commit a6359c607438d05e66fa1aa0126af9a44a9bf22e Author: Karsten Loesing karsten.loesing@gmx.net Date: Thu Jan 16 11:40:35 2020 +0100
Fix non-RunOnce mode.
This was accidentally broken in #32554. The RunOnce mode worked just fine, but the non-RunOnce mode terminated immediately after scheduling tasks. --- CHANGELOG.md | 7 ++++++- .../java/org/torproject/metrics/collector/Main.java | 8 ++++++-- .../metrics/collector/cron/Scheduler.java | 8 +++++--- .../metrics/collector/cron/ShutdownHook.java | 21 +++++++++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index d3088c3..7161100 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,9 @@ -# Changes in version 1.1?.? - 2020-0?-?? +# Changes in version 1.14.1 - 2020-01-16 + + * Medium changes + - Stay alive after scheduling (daemon) threads for modules. This + was broken by taking out the periodic check for configuration + file updates.
# Changes in version 1.14.0 - 2020-01-15 diff --git a/src/main/java/org/torproject/metrics/collector/Main.java b/src/main/java/org/torproject/metrics/collector/Main.java index 77bb2ca..3822353 100644 --- a/src/main/java/org/torproject/metrics/collector/Main.java +++ b/src/main/java/org/torproject/metrics/collector/Main.java @@ -87,12 +87,16 @@ public class Main { } else { conf.loadAndCheckConfiguration(confPath); } - Scheduler.getInstance().scheduleModuleRuns(collecTorMains, conf); + if (!Scheduler.getInstance().scheduleModuleRuns(collecTorMains, conf)) { + return; + } } catch (ConfigurationException ce) { printUsage(ce.getMessage()); return; } - Runtime.getRuntime().addShutdownHook(new ShutdownHook()); + ShutdownHook shutdownHook = new ShutdownHook(); + Runtime.getRuntime().addShutdownHook(shutdownHook); + shutdownHook.stayAlive(); }
private static void printUsage(String msg) { diff --git a/src/main/java/org/torproject/metrics/collector/cron/Scheduler.java b/src/main/java/org/torproject/metrics/collector/cron/Scheduler.java index 03292f4..70d27f5 100644 --- a/src/main/java/org/torproject/metrics/collector/cron/Scheduler.java +++ b/src/main/java/org/torproject/metrics/collector/cron/Scheduler.java @@ -50,10 +50,10 @@ public final class Scheduler implements ThreadFactory { }
/** - * Schedule all classes given according to the parameters in the - * the configuration. + * Schedule all classes given according to the parameters in the configuration + * and return whether the caller should wait for completion. */ - public void scheduleModuleRuns(Map<Key, + public boolean scheduleModuleRuns(Map<Key, Class<? extends CollecTorMain>> collecTorMains, Configuration conf) { try { gracePeriodMinutes = conf.getLong(Key.ShutdownGraceWaitMinutes); @@ -89,11 +89,13 @@ public final class Scheduler implements ThreadFactory { try { if (conf.getBool(Key.RunOnce)) { scheduler.invokeAll(runOnceMains); + return false; } } catch (ConfigurationException | InterruptedException | RejectedExecutionException | NullPointerException ex) { logger.error("Cannot schedule run-once: {}", ex.getMessage(), ex); } + return true; }
private void scheduleExecutions(CollecTorMain ctm, int offset, int period) { diff --git a/src/main/java/org/torproject/metrics/collector/cron/ShutdownHook.java b/src/main/java/org/torproject/metrics/collector/cron/ShutdownHook.java index c8f0807..ec34a19 100644 --- a/src/main/java/org/torproject/metrics/collector/cron/ShutdownHook.java +++ b/src/main/java/org/torproject/metrics/collector/cron/ShutdownHook.java @@ -13,15 +13,36 @@ public final class ShutdownHook extends Thread {
private static final Logger log = LoggerFactory.getLogger(ShutdownHook.class);
+ private boolean stayAlive = true; + /** Names the shutdown thread for debugging purposes. */ public ShutdownHook() { super("CollecTor-ShutdownThread"); }
+ /** + * Stay alive until the shutdown thread gets run. + */ + public void stayAlive() { + synchronized (this) { + while (this.stayAlive) { + try { + this.wait(); + } catch (InterruptedException e) { + /* Nothing we can do about this. */ + } + } + } + } + @Override public void run() { log.info("Shutdown in progress ... "); Scheduler.getInstance().shutdownScheduler(); + synchronized (this) { + this.stayAlive = false; + this.notify(); + } log.info("Shutdown finished. Exiting."); } }
tor-commits@lists.torproject.org