Commit 38799a01 authored by Matt Traudt's avatar Matt Traudt
Browse files

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
parent b30288e0
...@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ...@@ -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 - Log line on start up with sbws version, platform info, and library versions
(trac#26751) (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 ### Changed
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
datadir = ${sbws_home}/datadir datadir = ${sbws_home}/datadir
v3bw_dname = ${sbws_home}/v3bw v3bw_dname = ${sbws_home}/v3bw
v3bw_fname = ${v3bw_dname}/{}.v3bw v3bw_fname = ${v3bw_dname}/{}.v3bw
started_filepath = ${sbws_home}/started_at state_fname = ${sbws_home}/state.dat
log_dname = ${sbws_home}/log log_dname = ${sbws_home}/log
[destinations] [destinations]
......
...@@ -7,10 +7,8 @@ from ..lib.resultdump import ResultErrorStream ...@@ -7,10 +7,8 @@ from ..lib.resultdump import ResultErrorStream
from ..lib.relaylist import RelayList from ..lib.relaylist import RelayList
from ..lib.relayprioritizer import RelayPrioritizer from ..lib.relayprioritizer import RelayPrioritizer
from ..lib.destination import DestinationList from ..lib.destination import DestinationList
from ..util.filelock import FileLock
from ..util.timestamp import now_isodt_str from ..util.timestamp import now_isodt_str
# from ..util.simpleauth import authenticate_to_server from ..util.state import State
# from ..util.sockio import (make_socket, close_socket)
from sbws.globals import (fail_hard, is_initted) from sbws.globals import (fail_hard, is_initted)
import sbws.util.stem as stem_utils import sbws.util.stem as stem_utils
import sbws.util.requests as requests_utils import sbws.util.requests as requests_utils
...@@ -316,22 +314,7 @@ def result_putter_error(target): ...@@ -316,22 +314,7 @@ def result_putter_error(target):
return closure 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): def run_speedtest(args, conf):
write_start_ts(conf)
controller, _ = stem_utils.init_controller( controller, _ = stem_utils.init_controller(
path=conf['tor']['control_socket']) path=conf['tor']['control_socket'])
if not controller: if not controller:
...@@ -398,6 +381,9 @@ def main(args, conf): ...@@ -398,6 +381,9 @@ def main(args, conf):
os.makedirs(conf['paths']['datadir'], exist_ok=True) os.makedirs(conf['paths']['datadir'], exist_ok=True)
state = State(conf['paths']['state_fname'])
state['scanner_started'] = now_isodt_str()
try: try:
run_speedtest(args, conf) run_speedtest(args, conf)
except KeyboardInterrupt as e: except KeyboardInterrupt as e:
......
...@@ -9,8 +9,9 @@ from statistics import median ...@@ -9,8 +9,9 @@ from statistics import median
from sbws import __version__ from sbws import __version__
from sbws.globals import SPEC_VERSION, BW_LINE_SIZE from sbws.globals import SPEC_VERSION, BW_LINE_SIZE
from sbws.lib.resultdump import ResultSuccess, _ResultType 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.timestamp import now_isodt_str, unixts_to_isodt_str
from sbws.util.state import State
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -66,27 +67,6 @@ def warn_if_not_accurate_enough(bw_lines, scale_constant): ...@@ -66,27 +67,6 @@ def warn_if_not_accurate_enough(bw_lines, scale_constant):
'allowed', (1 - accuracy_ratio) * 100, margin * 100) '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): def num_results_of_type(results, type_str):
return len([r for r in results if r.type == type_str]) return len([r for r in results if r.type == type_str])
...@@ -210,7 +190,11 @@ class V3BWHeader(object): ...@@ -210,7 +190,11 @@ class V3BWHeader(object):
@staticmethod @staticmethod
def generator_started_from_file(conf): 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 @staticmethod
def latest_bandwidth_from_results(results): def latest_bandwidth_from_results(results):
......
...@@ -207,8 +207,8 @@ def _validate_paths(conf): ...@@ -207,8 +207,8 @@ def _validate_paths(conf):
sec = 'paths' sec = 'paths'
err_tmpl = Template('$sec/$key ($val): $e') err_tmpl = Template('$sec/$key ($val): $e')
unvalidated_keys = [ unvalidated_keys = [
'datadir', 'sbws_home', 'v3bw_fname', 'v3bw_dname', 'datadir', 'sbws_home', 'v3bw_fname', 'v3bw_dname', 'state_fname',
'started_filepath', 'log_dname'] 'log_dname']
all_valid_keys = unvalidated_keys all_valid_keys = unvalidated_keys
allow_missing = ['sbws_home'] allow_missing = ['sbws_home']
errors.extend(_validate_section_keys(conf, sec, all_valid_keys, err_tmpl, errors.extend(_validate_section_keys(conf, sec, all_valid_keys, err_tmpl,
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment