Ticket #14826: 0001-task-14826-depending-on-the-starting-time-the-update.patch

File 0001-task-14826-depending-on-the-starting-time-the-update.patch, 6.0 KB (added by iwakeh, 5 years ago)
  • src/main/java/org/torproject/onionoo/cron/Main.java

    From e26f7629b216fc3866a3267d15cbebbeb9156d72 Sun Feb 15 18:00:00 2015
    From: iwakeh <iwakeh@users.ourproject.org>
    Date: Sun, 15 Feb 2015 18:00:00 +0000
    Subject: [PATCH] task-14826: depending on the starting time the updater will
     run every hour at minute 15,16,17,18,or 19. The Lock file ist only used for
     the single run.
    
    ---
     .../java/org/torproject/onionoo/cron/Main.java     | 140 +++++++++++++--------
     1 file changed, 89 insertions(+), 51 deletions(-)
    
    diff --git a/src/main/java/org/torproject/onionoo/cron/Main.java b/src/main/java/org/torproject/onionoo/cron/Main.java
    index 986f22d..c42a38a 100644
    a b  
    22 * See LICENSE for licensing information */
    33package org.torproject.onionoo.cron;
    44
     5import java.util.Calendar;
     6import static java.util.concurrent.TimeUnit.MINUTES;
     7import java.util.concurrent.Executors;
     8import java.util.concurrent.ScheduledFuture;
     9import java.util.concurrent.ScheduledExecutorService;
    510import org.slf4j.Logger;
    611import org.slf4j.LoggerFactory;
    712import org.torproject.onionoo.docs.DocumentStore;
    import org.torproject.onionoo.util.LockFile; 
    1318import org.torproject.onionoo.writer.DocumentWriterRunner;
    1419
    1520/* Update search data and status data files. */
     21
    1622public class Main {
    1723
    1824  private static Logger log = LoggerFactory.getLogger(Main.class);
    public class Main { 
    2026  private Main() {
    2127  }
    2228
     29  private static final ScheduledExecutorService scheduler =
     30      Executors.newScheduledThreadPool(1);
     31
    2332  public static void main(String[] args) {
    24     log.debug("Started ...");
    25     LockFile lf = new LockFile();
    26     log.info("Initializing.");
    27     if (lf.acquireLock()) {
    28       log.info("Acquired lock");
    29     } else {
    30       log.error("Could not acquire lock.  Is Onionoo "
    31           + "already running?  Terminating");
     33    boolean runOnce = "true".equals(System.getProperty(
     34        "onionoo.cron.runonce", "true"));
     35    if (runOnce){
     36      log.info("Going to run one-time updater ... ");
     37      LockFile lf = new LockFile();
     38      log.info("Initializing.");
     39      if (lf.acquireLock()) {
     40          log.info("Acquired lock");
     41      } else {
     42          log.error("Could not acquire lock.  Is Onionoo "
     43                    + "already running?  Terminating");
     44          return;
     45      }
     46      new Updater().run();
     47      log.info("Releasing lock.");
     48      if (lf.releaseLock()) {
     49        log.info("Released lock");
     50      } else {
     51        log.error("Could not release lock.  The next "
     52            + "execution may not start as expected");
     53      }
    3254      return;
    3355    }
    34     log.debug(" ... running .... ");
    35 
    36     DescriptorSource dso = DescriptorSourceFactory.getDescriptorSource();
    37     log.info("Initialized descriptor source");
    38     DocumentStore ds = DocumentStoreFactory.getDocumentStore();
    39     log.info("Initialized document store");
    40     StatusUpdateRunner sur = new StatusUpdateRunner();
    41     log.info("Initialized status update runner");
    42     DocumentWriterRunner dwr = new DocumentWriterRunner();
    43     log.info("Initialized document writer runner");
    44 
    45     log.info("Downloading descriptors.");
    46     dso.downloadDescriptors();
    47 
    48     log.info("Reading descriptors.");
    49     dso.readDescriptors();
    50 
    51     log.info("Updating internal status files.");
    52     sur.updateStatuses();
    53 
    54     log.info("Updating document files.");
    55     dwr.writeDocuments();
    56 
    57     log.info("Shutting down.");
    58     dso.writeHistoryFiles();
    59     log.info("Wrote parse histories");
    60     ds.flushDocumentCache();
    61     log.info("Flushed document cache");
    62 
    63     log.info("Gathering statistics.");
    64     sur.logStatistics();
    65     dwr.logStatistics();
    66     log.info("Descriptor source\n" + dso.getStatsString());
    67     log.info("Document store\n" + ds.getStatsString());
    68 
    69     log.info("Releasing lock.");
    70     if (lf.releaseLock()) {
    71       log.info("Released lock");
    72     } else {
    73       log.error("Could not release lock.  The next "
    74           + "execution may not start as expected");
    75     }
     56    log.info("Periodic updater started.");
     57    final Runnable updater = new Updater();
     58    int m = Calendar.getInstance().get(Calendar.MINUTE);
     59    int initialDelay = ((75-m)%60 + m%5) %60;
     60
     61    // run after initialDelay delay and then every hour
     62    final ScheduledFuture<?> updaterHandle =
     63    scheduler.scheduleAtFixedRate(updater, initialDelay, 60, MINUTES);
     64    log.info("Periodic updater will start every hour at minute "
     65             + ((m+initialDelay)%60) + ".");
     66  }
     67
     68  private static class Updater implements Runnable{
     69
     70    private Logger log = LoggerFactory.getLogger(Main.class);
     71
     72    public void run() {
     73      log.debug("Started update ...");
     74
     75      DescriptorSource dso = DescriptorSourceFactory.getDescriptorSource();
     76      log.info("Initialized descriptor source");
     77      DocumentStore ds = DocumentStoreFactory.getDocumentStore();
     78      log.info("Initialized document store");
     79      StatusUpdateRunner sur = new StatusUpdateRunner();
    7680
    77     log.info("Terminating.");
     81      log.info("Initialized status update runner");
     82      DocumentWriterRunner dwr = new DocumentWriterRunner();
     83      log.info("Initialized document writer runner");
     84
     85      log.info("Downloading descriptors.");
     86      dso.downloadDescriptors();
     87
     88      log.info("Reading descriptors.");
     89      dso.readDescriptors();
     90
     91      log.info("Updating internal status files.");
     92      sur.updateStatuses();
     93
     94      log.info("Updating document files.");
     95      dwr.writeDocuments();
     96
     97      log.info("Shutting down.");
     98      dso.writeHistoryFiles();
     99      log.info("Wrote parse histories");
     100      ds.flushDocumentCache();
     101      log.info("Flushed document cache");
     102
     103      log.info("Gathering statistics.");
     104      sur.logStatistics();
     105      dwr.logStatistics();
     106      log.info("Descriptor source\n" + dso.getStatsString());
     107      log.info("Document store\n" + ds.getStatsString());
     108
     109      log.info("Done.");
     110
     111      // clean-up to prevent oom-exc
     112      ds.invalidateDocumentCache();
     113      DocumentStoreFactory.setDocumentStore(null);
     114      DescriptorSourceFactory.setDescriptorSource(null);
     115    }
    78116  }
    79117}
    80118