[tor-commits] [ooni-probe/master] Configure default decks when the scheduler is run for the first time.

art at torproject.org art at torproject.org
Mon Sep 19 12:14:24 UTC 2016


commit 4e777252050d8b0266ce726ee8e43abfb6eaeb21
Author: Arturo Filastò <arturo at filasto.net>
Date:   Fri Sep 2 17:36:14 2016 +0200

    Configure default decks when the scheduler is run for the first time.
    
    * Add hook for running an operation when the task is run for the first time.
---
 ooni/agent/scheduler.py | 28 ++++++++++++++++++++++++++--
 ooni/deck/store.py      |  3 +++
 ooni/director.py        |  2 +-
 ooni/settings.py        | 33 ---------------------------------
 4 files changed, 30 insertions(+), 36 deletions(-)

diff --git a/ooni/agent/scheduler.py b/ooni/agent/scheduler.py
index 54551f6..7c2212b 100644
--- a/ooni/agent/scheduler.py
+++ b/ooni/agent/scheduler.py
@@ -12,7 +12,7 @@ from ooni.scripts import oonireport
 from ooni import resources
 from ooni.utils import log, SHORT_DATE
 from ooni.utils.files import human_size_to_bytes, directory_usage
-from ooni.deck.store import input_store, deck_store
+from ooni.deck.store import input_store, deck_store, DEFAULT_DECKS
 from ooni.settings import config
 from ooni.contrib import croniter
 from ooni.geoip import probe_ip
@@ -41,6 +41,10 @@ class FileSystemlockAndMutex(object):
         self._fs_lock.unlock()
         self._mutex.release()
 
+# We use this date to indicate that the scheduled task has never run.
+# Easter egg, try to see what is special about this date :)?
+CANARY_DATE = datetime(1957, 8, 4)
+
 class DidNotRun(Exception):
     pass
 
@@ -76,7 +80,7 @@ class ScheduledTask(object):
     def last_run(self):
         self._last_run.restat(False)
         if not self._last_run.exists():
-            return datetime.fromtimestamp(0)
+            return CANARY_DATE
         with self._last_run.open('r') as in_file:
             date_str = in_file.read()
         return datetime.strptime(date_str, self._time_format)
@@ -89,6 +93,13 @@ class ScheduledTask(object):
     def task(self):
         raise NotImplemented
 
+    def first_run(self):
+        """
+        This hook is called if it's the first time a particular scheduled
+        operation is run.
+        """
+        pass
+
     @defer.inlineCallbacks
     def run(self):
         yield self._last_run_lock.acquire()
@@ -96,6 +107,8 @@ class ScheduledTask(object):
             self._last_run_lock.release()
             raise DidNotRun
         try:
+            if self.last_run == CANARY_DATE:
+                yield defer.maybeDeferred(self.first_run)
             yield self.task()
             self._update_last_run()
         except:
@@ -240,6 +253,13 @@ class RefreshDeckList(ScheduledTask):
         self.scheduler = scheduler
         super(RefreshDeckList, self).__init__(schedule, identifier)
 
+    def first_run(self):
+        """
+        On first run we enable the default decks.
+        """
+        for deck_id in DEFAULT_DECKS:
+            deck_store.enable(deck_id)
+
     def task(self):
         self.scheduler.refresh_deck_list()
 
@@ -302,6 +322,10 @@ class SchedulerService(service.MultiService):
             if isinstance(scheduled_task, RunDeck):
                 self._scheduled_tasks.remove(scheduled_task)
 
+        if not config.is_initialized():
+            # Disable scheduling measurements if we are not initialized.
+            return
+
         for deck_id, deck in deck_store.list_enabled():
             if deck.schedule is None:
                 continue
diff --git a/ooni/deck/store.py b/ooni/deck/store.py
index c938d56..bd852c7 100644
--- a/ooni/deck/store.py
+++ b/ooni/deck/store.py
@@ -11,6 +11,9 @@ from ooni.otime import timestampNowISO8601UTC
 from ooni.resources import check_for_update
 from ooni.settings import config
 
+# These are the decks to be run by default.
+DEFAULT_DECKS = ['web-full']
+
 class InputNotFound(Exception):
     pass
 
diff --git a/ooni/director.py b/ooni/director.py
index ab31bf5..cb2bdf0 100644
--- a/ooni/director.py
+++ b/ooni/director.py
@@ -398,7 +398,7 @@ class Director(object):
                 self._tor_starting.callback(self._tor_state)
             except Exception as exc:
                 log.err("Failed to start tor")
-                log.exc(exc)
+                log.exception(exc)
                 self._tor_starting.errback(Failure(exc))
 
         elif config.tor.control_port and config.tor_state is None:
diff --git a/ooni/settings.py b/ooni/settings.py
index 9f24f9b..a1c55fc 100644
--- a/ooni/settings.py
+++ b/ooni/settings.py
@@ -266,31 +266,6 @@ class OConfig(object):
         with open(initialized_path, 'w+'): pass
 
     @property
-    def last_run_version(self):
-        """
-        :return: Version identifying the last run version of ooniprobe.
-        """
-        last_run_version_path = os.path.join(
-            self.running_path, "last_run_version"
-        )
-        if not os.path.exists(last_run_version_path):
-            return parse_version("0")
-        with open(last_run_version_path) as in_file:
-            last_run_version = in_file.read()
-        return parse_version(last_run_version)
-
-    @property
-    def current_version(self):
-        return parse_version(ooniprobe_version)
-
-    def set_last_run_version(self):
-        last_run_version_path = os.path.join(
-            self.running_path, "last_run_version"
-        )
-        with open(last_run_version_path, "w") as out_file:
-            out_file.write(ooniprobe_version)
-
-    @property
     def running_path(self):
         """
         This is the directory used to store state application data.
@@ -408,14 +383,6 @@ class OConfig(object):
                 if exc.errno != errno.EEXIST:
                     raise
 
-        # This means ooniprobe was installed for the first time or is coming
-        # from a 1.x series installation. We should configure the default deck.
-        if self.last_run_version.public == "0":
-            from ooni.deck.store import deck_store
-            DEFAULT_DECKS = ['web-full']
-            for deck_id in DEFAULT_DECKS:
-                deck_store.enable(deck_id)
-
     def create_config_file(self, include_ip=False, include_asn=True,
                            include_country=True, should_upload=True,
                            preferred_backend="onion"):





More information about the tor-commits mailing list