[tor-commits] [sbws/master] Put scanner start time in state file ...

pastly at torproject.org pastly at torproject.org
Thu Aug 9 14:21:19 UTC 2018


commit 38799a012055755fb10c9919bc4ae5524a358cf3
Author: Matt Traudt <sirmatt at ksu.edu>
Date:   Mon Jul 16 14:52:45 2018 -0400

    Put scanner start time in state file ...
    
    - Remove started_at file in config et al.
    - Add state_fname to config et al.
    - Remove read/write functions for the started_at file in scanner.py
      and genearte.py
---
 CHANGELOG.md            |  2 ++
 sbws/config.default.ini |  2 +-
 sbws/core/scanner.py    | 22 ++++------------------
 sbws/lib/v3bwfile.py    | 30 +++++++-----------------------
 sbws/util/config.py     |  4 ++--
 5 files changed, 16 insertions(+), 44 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index ddd7f21..9a5ceee 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 
 - Log line on start up with sbws version, platform info, and library versions
 (trac#26751)
+- State file for storing the last time `sbws scanner` was started, and able to
+  be used for storing many other types of state in the future. (GH#166)
 
 ### Changed
 
diff --git a/sbws/config.default.ini b/sbws/config.default.ini
index b21ba0b..121154d 100644
--- a/sbws/config.default.ini
+++ b/sbws/config.default.ini
@@ -2,7 +2,7 @@
 datadir = ${sbws_home}/datadir
 v3bw_dname = ${sbws_home}/v3bw
 v3bw_fname = ${v3bw_dname}/{}.v3bw
-started_filepath = ${sbws_home}/started_at
+state_fname = ${sbws_home}/state.dat
 log_dname = ${sbws_home}/log
 
 [destinations]
diff --git a/sbws/core/scanner.py b/sbws/core/scanner.py
index 5bcf276..5e98e99 100644
--- a/sbws/core/scanner.py
+++ b/sbws/core/scanner.py
@@ -7,10 +7,8 @@ from ..lib.resultdump import ResultErrorStream
 from ..lib.relaylist import RelayList
 from ..lib.relayprioritizer import RelayPrioritizer
 from ..lib.destination import DestinationList
-from ..util.filelock import FileLock
 from ..util.timestamp import now_isodt_str
-# from ..util.simpleauth import authenticate_to_server
-# from ..util.sockio import (make_socket, close_socket)
+from ..util.state import State
 from sbws.globals import (fail_hard, is_initted)
 import sbws.util.stem as stem_utils
 import sbws.util.requests as requests_utils
@@ -316,22 +314,7 @@ def result_putter_error(target):
     return closure
 
 
-def write_start_ts(conf):
-    """Write ISO formated timestamp which represents the date and time
-    when scanner started.
-
-    :param ConfigParser conf: configuration
-    """
-    generator_started = now_isodt_str()
-    log.info('Scanner started at {}'.format(generator_started))
-    filepath = conf['paths']['started_filepath']
-    with FileLock(filepath):
-        with open(filepath, 'w') as fd:
-            fd.write(generator_started)
-
-
 def run_speedtest(args, conf):
-    write_start_ts(conf)
     controller, _ = stem_utils.init_controller(
         path=conf['tor']['control_socket'])
     if not controller:
@@ -398,6 +381,9 @@ def main(args, conf):
 
     os.makedirs(conf['paths']['datadir'], exist_ok=True)
 
+    state = State(conf['paths']['state_fname'])
+    state['scanner_started'] = now_isodt_str()
+
     try:
         run_speedtest(args, conf)
     except KeyboardInterrupt as e:
diff --git a/sbws/lib/v3bwfile.py b/sbws/lib/v3bwfile.py
index c1dc890..2784c2f 100644
--- a/sbws/lib/v3bwfile.py
+++ b/sbws/lib/v3bwfile.py
@@ -9,8 +9,9 @@ from statistics import median
 from sbws import __version__
 from sbws.globals import SPEC_VERSION, BW_LINE_SIZE
 from sbws.lib.resultdump import ResultSuccess, _ResultType
-from sbws.util.filelock import FileLock, DirectoryLock
+from sbws.util.filelock import DirectoryLock
 from sbws.util.timestamp import now_isodt_str, unixts_to_isodt_str
+from sbws.util.state import State
 
 log = logging.getLogger(__name__)
 
@@ -66,27 +67,6 @@ def warn_if_not_accurate_enough(bw_lines, scale_constant):
                     'allowed', (1 - accuracy_ratio) * 100, margin * 100)
 
 
-def read_started_ts(conf):
-    """Read ISO formated timestamp which represents the date and time
-    when scanner started.
-
-    :param ConfigParser conf: configuration
-    :returns: str, ISO formated timestamp
-    """
-    try:
-        filepath = conf['paths']['started_filepath']
-    except TypeError:
-        return None
-    try:
-        with FileLock(filepath):
-            with open(filepath, 'r') as fd:
-                generator_started = fd.read()
-    except FileNotFoundError as e:
-        log.warn('File %s not found.%s', filepath, e)
-        return None
-    return generator_started
-
-
 def num_results_of_type(results, type_str):
     return len([r for r in results if r.type == type_str])
 
@@ -210,7 +190,11 @@ class V3BWHeader(object):
 
     @staticmethod
     def generator_started_from_file(conf):
-        return read_started_ts(conf)
+        state = State(conf['paths']['state_fname'])
+        if 'scanner_started' in state:
+            return state['scanner_started']
+        else:
+            return None
 
     @staticmethod
     def latest_bandwidth_from_results(results):
diff --git a/sbws/util/config.py b/sbws/util/config.py
index bfd828c..6f37bb8 100644
--- a/sbws/util/config.py
+++ b/sbws/util/config.py
@@ -207,8 +207,8 @@ def _validate_paths(conf):
     sec = 'paths'
     err_tmpl = Template('$sec/$key ($val): $e')
     unvalidated_keys = [
-        'datadir', 'sbws_home', 'v3bw_fname', 'v3bw_dname',
-        'started_filepath', 'log_dname']
+        'datadir', 'sbws_home', 'v3bw_fname', 'v3bw_dname', 'state_fname',
+        'log_dname']
     all_valid_keys = unvalidated_keys
     allow_missing = ['sbws_home']
     errors.extend(_validate_section_keys(conf, sec, all_valid_keys, err_tmpl,





More information about the tor-commits mailing list