commit cb78f5fe5b30dc138bb545c8f26b95b7abc90d9d
Author: iwakeh <iwakeh(a)torproject.org>
Date: Tue Sep 6 19:41:57 2016 +0200
Make waiting time for regular module shutdown configurable.
---
.../java/org/torproject/collector/conf/Configuration.java | 15 +++++++++++++++
src/main/java/org/torproject/collector/conf/Key.java | 1 +
.../java/org/torproject/collector/cron/Scheduler.java | 13 ++++++++++---
src/main/resources/collector.properties | 3 +++
.../org/torproject/collector/conf/ConfigurationTest.java | 2 +-
5 files changed, 30 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/torproject/collector/conf/Configuration.java b/src/main/java/org/torproject/collector/conf/Configuration.java
index 4bab136..52e3bad 100644
--- a/src/main/java/org/torproject/collector/conf/Configuration.java
+++ b/src/main/java/org/torproject/collector/conf/Configuration.java
@@ -217,6 +217,21 @@ public class Configuration extends Observable implements Cloneable {
}
/**
+ * Parse a long property.
+ * Verifies that this enum is a Key for a Long value.
+ */
+ public long getLong(Key key) throws ConfigurationException {
+ try {
+ checkClass(key, Long.class);
+ String prop = props.getProperty(key.name());
+ return Long.parseLong(prop);
+ } catch (RuntimeException re) {
+ throw new ConfigurationException("Corrupt property: " + key
+ + " reason: " + re.getMessage(), re);
+ }
+ }
+
+ /**
* Returns a {@code Path} property, e.g.
* {@code pathProperty = /my/path/file}.
*/
diff --git a/src/main/java/org/torproject/collector/conf/Key.java b/src/main/java/org/torproject/collector/conf/Key.java
index fce89a2..5ec1be4 100644
--- a/src/main/java/org/torproject/collector/conf/Key.java
+++ b/src/main/java/org/torproject/collector/conf/Key.java
@@ -9,6 +9,7 @@ import java.nio.file.Path;
*/
public enum Key {
+ ShutdownGraceWaitMinutes(Long.class),
RunOnce(Boolean.class),
ExitlistOutputDirectory(Path.class),
ExitlistUrl(URL.class),
diff --git a/src/main/java/org/torproject/collector/cron/Scheduler.java b/src/main/java/org/torproject/collector/cron/Scheduler.java
index 1e72dc0..1863d73 100644
--- a/src/main/java/org/torproject/collector/cron/Scheduler.java
+++ b/src/main/java/org/torproject/collector/cron/Scheduler.java
@@ -29,7 +29,6 @@ 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);
@@ -37,6 +36,7 @@ public final class Scheduler implements ThreadFactory {
private final ThreadFactory threads = Executors.defaultThreadFactory();
private int currentThreadNo = 0;
+ private long gracePeriodMinutes = 20L;
private final ScheduledExecutorService scheduler =
Executors.newScheduledThreadPool(10, this);
@@ -55,6 +55,12 @@ public final class Scheduler implements ThreadFactory {
*/
public void scheduleModuleRuns(Map<Key,
Class<? extends CollecTorMain>> collecTorMains, Configuration conf) {
+ try {
+ gracePeriodMinutes = conf.getLong(Key.ShutdownGraceWaitMinutes);
+ } catch (ConfigurationException ce) {
+ logger.warn("Cannot read grace period: {}", ce);
+ gracePeriodMinutes = 20L;
+ }
List<Callable<Object>> runOnceMains = new ArrayList<>();
for (Map.Entry<Key, Class<? extends CollecTorMain>> ctmEntry
: collecTorMains.entrySet()) {
@@ -119,9 +125,10 @@ public final class Scheduler implements ThreadFactory {
public void shutdownScheduler() {
try {
logger.info("Waiting at most {} minutes for termination "
- + "of running tasks ... ", GRACE_MIN);
+ + "of running tasks ... ", gracePeriodMinutes);
scheduler.shutdown();
- scheduler.awaitTermination(GRACE_MIN, java.util.concurrent.TimeUnit.MINUTES);
+ scheduler.awaitTermination(gracePeriodMinutes,
+ 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/resources/collector.properties b/src/main/resources/collector.properties
index 15b15aa..0c93cd0 100644
--- a/src/main/resources/collector.properties
+++ b/src/main/resources/collector.properties
@@ -8,6 +8,9 @@
# run one time and without any delay.
# Make sure only to run non-interfering modules together.
RunOnce = false
+##
+# Waiting time for regular shutdown in minutes.
+ShutdownGraceWaitMinutes = 10
## the following defines, if this module is activated
BridgedescsActivated = false
# period in minutes
diff --git a/src/test/java/org/torproject/collector/conf/ConfigurationTest.java b/src/test/java/org/torproject/collector/conf/ConfigurationTest.java
index 89a7750..fda9601 100644
--- a/src/test/java/org/torproject/collector/conf/ConfigurationTest.java
+++ b/src/test/java/org/torproject/collector/conf/ConfigurationTest.java
@@ -37,7 +37,7 @@ public class ConfigurationTest {
public void testKeyCount() throws Exception {
assertEquals("The number of properties keys in enum Key changed."
+ "\n This test class should be adapted.",
- 48, Key.values().length);
+ 49, Key.values().length);
}
@Test()