[tor-commits] [collector/master] Make the start happen at HH:mm:00, implements part 3 of #19776.

karsten at torproject.org karsten at torproject.org
Tue Aug 9 09:11:20 UTC 2016


commit edde3fce478b6afb2e2840a9de8fd471bab90c21
Author: iwakeh <iwakeh at torproject.org>
Date:   Fri Aug 5 08:45:51 2016 +0200

    Make the start happen at HH:mm:00, implements part 3 of #19776.
---
 .../org/torproject/collector/cron/Scheduler.java   | 23 +++++++++++++++-------
 .../torproject/collector/cron/SchedulerTest.java   | 11 +++++++++++
 2 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/src/main/java/org/torproject/collector/cron/Scheduler.java b/src/main/java/org/torproject/collector/cron/Scheduler.java
index 5535a8a..25490aa 100644
--- a/src/main/java/org/torproject/collector/cron/Scheduler.java
+++ b/src/main/java/org/torproject/collector/cron/Scheduler.java
@@ -74,18 +74,27 @@ public class Scheduler implements ThreadFactory {
     }
   }
 
+  private static final long MILLIS_IN_A_MINUTE = 60_000L;
+
   private void scheduleExecutions(CollecTorMain ctm, int offset, int period) {
     this.log.info("Periodic updater started for " + ctm.getClass().getName()
         + "; offset=" + offset + ", period=" + period + ".");
-    int currentMinute = Calendar.getInstance().get(Calendar.MINUTE);
-    int initialDelay = (period - (currentMinute % period) + offset) % period;
+    long periodMillis = period * MILLIS_IN_A_MINUTE;
+    long initialDelayMillis = computeInitialDelayMillis(
+        System.currentTimeMillis(), offset * MILLIS_IN_A_MINUTE, periodMillis);
 
     /* Run after initialDelay delay and then every period min. */
-    this.log.info("Periodic updater will start every " + period + "th min "
-        + "at minute " + ((currentMinute + initialDelay) % period) + "."
-        + "  The first start will happen in " + initialDelay + " minute(s).");
-    this.scheduler.scheduleAtFixedRate(ctm, initialDelay, period,
-        TimeUnit.MINUTES);
+    log.info("Periodic updater will first run in {} and then every {} minutes.",
+        initialDelayMillis < MILLIS_IN_A_MINUTE ? "under 1 minute"
+        : (initialDelayMillis / MILLIS_IN_A_MINUTE) + " minute(s)", period);
+    this.scheduler.scheduleAtFixedRate(ctm, initialDelayMillis, periodMillis,
+        TimeUnit.MILLISECONDS);
+  }
+
+  protected static long computeInitialDelayMillis(long currentMillis,
+      long offsetMillis, long periodMillis) {
+    return (periodMillis - (currentMillis % periodMillis) + offsetMillis)
+        % periodMillis;
   }
 
   /**
diff --git a/src/test/java/org/torproject/collector/cron/SchedulerTest.java b/src/test/java/org/torproject/collector/cron/SchedulerTest.java
index 4c01f91..a1b9af1 100644
--- a/src/test/java/org/torproject/collector/cron/SchedulerTest.java
+++ b/src/test/java/org/torproject/collector/cron/SchedulerTest.java
@@ -60,5 +60,16 @@ public class SchedulerTest {
     Scheduler.getInstance().shutdownScheduler();
   }
 
+  @Test()
+  public void testDelayComputation() {
+    assertEquals(59_993L,
+        Scheduler.computeInitialDelayMillis(7L, 60_000L, 300_000L));
+    assertEquals(7L,
+        Scheduler.computeInitialDelayMillis(59_993L, 60_000L, 300_000L));
+    assertEquals(299_999L,
+        Scheduler.computeInitialDelayMillis(60_001L, 60_000L, 300_000L));
+    assertEquals(60_009L,
+        Scheduler.computeInitialDelayMillis(299_991L, 60_000L, 300_000L));
+  }
 }
 





More information about the tor-commits mailing list