commit 7cec1c874adffd14b7f152e289a0ccdc7cac7ad5 Author: iwakeh iwakeh@torproject.org Date: Mon Sep 5 20:54:09 2016 +0200
Provide a shutdown-hook that allows running modules to finish properly.
Implements task-19016. Adapted logback default to log shutdown-hook log statements also to the console.
Corrected coverage-check values. --- build.xml | 4 ++-- src/main/java/org/torproject/collector/Main.java | 2 ++ .../org/torproject/collector/cron/Scheduler.java | 8 ++++--- .../torproject/collector/cron/ShutdownHook.java | 28 ++++++++++++++++++++++ src/main/resources/logback.xml | 9 +++++++ 5 files changed, 46 insertions(+), 5 deletions(-)
diff --git a/build.xml b/build.xml index 1e292b7..11d1f92 100644 --- a/build.xml +++ b/build.xml @@ -280,13 +280,13 @@ <include name="**/*.java" /> </fileset> </cobertura-report> - <cobertura-check totallinerate="5" totalbranchrate="1" > + <cobertura-check totallinerate="7" totalbranchrate="2" > <regex pattern="org.torproject.collector.conf" branchrate="87" linerate="100"/> <regex pattern="org.torproject.collector.cron" branchrate="50" linerate="71" /> <regex pattern="org.torproject.collector.Main" - branchrate="100" linerate="100" /> + branchrate="100" linerate="91" /> </cobertura-check> </target> <target name="test" depends="compile,compile-tests"> diff --git a/src/main/java/org/torproject/collector/Main.java b/src/main/java/org/torproject/collector/Main.java index 997ee2a..289cf30 100644 --- a/src/main/java/org/torproject/collector/Main.java +++ b/src/main/java/org/torproject/collector/Main.java @@ -9,6 +9,7 @@ import org.torproject.collector.conf.ConfigurationException; import org.torproject.collector.conf.Key; import org.torproject.collector.cron.CollecTorMain; import org.torproject.collector.cron.Scheduler; +import org.torproject.collector.cron.ShutdownHook; import org.torproject.collector.exitlists.ExitListDownloader; import org.torproject.collector.index.CreateIndexJson; import org.torproject.collector.relaydescs.ArchiveWriter; @@ -79,6 +80,7 @@ public class Main { printUsage(ce.getMessage()); return; } + Runtime.getRuntime().addShutdownHook(new ShutdownHook()); }
private static void printUsage(String msg) { diff --git a/src/main/java/org/torproject/collector/cron/Scheduler.java b/src/main/java/org/torproject/collector/cron/Scheduler.java index 12fe1bb..1e72dc0 100644 --- a/src/main/java/org/torproject/collector/cron/Scheduler.java +++ b/src/main/java/org/torproject/collector/cron/Scheduler.java @@ -29,6 +29,8 @@ public final class Scheduler implements ThreadFactory { public static final String ACTIVATED = "Activated"; public static final String PERIODMIN = "PeriodMinutes"; public static final String OFFSETMIN = "OffsetMinutes"; + private static final long GRACE_MIN = 20L; + private static final long MILLIS_IN_A_MINUTE = 60_000L;
private static final Logger logger = LoggerFactory.getLogger(Scheduler.class);
@@ -89,8 +91,6 @@ public final class Scheduler implements ThreadFactory { } }
- private static final long MILLIS_IN_A_MINUTE = 60_000L; - private void scheduleExecutions(CollecTorMain ctm, int offset, int period) { logger.info("Periodic updater started for " + ctm.getClass().getName() + "; offset=" + offset + ", period=" + period + "."); @@ -118,8 +118,10 @@ public final class Scheduler implements ThreadFactory { */ public void shutdownScheduler() { try { + logger.info("Waiting at most {} minutes for termination " + + "of running tasks ... ", GRACE_MIN); scheduler.shutdown(); - scheduler.awaitTermination(20L, java.util.concurrent.TimeUnit.MINUTES); + scheduler.awaitTermination(GRACE_MIN, java.util.concurrent.TimeUnit.MINUTES); logger.info("Shutdown of all scheduled tasks completed successfully."); } catch (InterruptedException ie) { List<Runnable> notTerminated = scheduler.shutdownNow(); diff --git a/src/main/java/org/torproject/collector/cron/ShutdownHook.java b/src/main/java/org/torproject/collector/cron/ShutdownHook.java new file mode 100644 index 0000000..519f16d --- /dev/null +++ b/src/main/java/org/torproject/collector/cron/ShutdownHook.java @@ -0,0 +1,28 @@ +/* Copyright 2016 The Tor Project + * See LICENSE for licensing information */ + +package org.torproject.collector.cron; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Scheduler that starts the modules configured in collector.properties. + */ +public final class ShutdownHook extends Thread { + + private static final Logger log = LoggerFactory.getLogger(ShutdownHook.class); + + /** Names the shutdown thread for debugging purposes. */ + public ShutdownHook() { + super("CollecTor-ShutdownThread"); + } + + @Override + public void run() { + log.info("Shutdown in progress ... "); + Scheduler.getInstance().shutdownScheduler(); + log.info("Shutdown finished. Exiting."); + } +} + diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 2d8a1ce..1d97d00 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -25,6 +25,12 @@ </filter> </appender>
+ <appender name="SHUTDOWN" class="ch.qos.logback.core.ConsoleAppender"> + <encoder> + <pattern>${utc-date-pattern} %level %logger{20}:%line %msg%n</pattern> + </encoder> + </appender> + <appender name="FILEALL" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${fileall-logname}.log</file> <encoder> @@ -146,6 +152,9 @@ <appender-ref ref="CONSOLE" /> </logger>
+ <logger name="org.torproject.collector.cron.ShutdownHook" > + <appender-ref ref="SHUTDOWN" /> + </logger>
<root level="ALL"> <appender-ref ref="FILEALL" />