# HG changeset patch # User Paul Fisher # Date 1579581383 18000 # Node ID 7117db65715e751b0b7a13fbd0a70bee95f8a47c # Parent 9daa281d996b8b2f843eadb15e18e0c6a90a560a Make weatherlog shutdown properly by handling SIGTERM. diff -r 9daa281d996b -r 7117db65715e setup.py --- a/setup.py Thu Jan 16 23:52:30 2020 -0500 +++ b/setup.py Mon Jan 20 23:36:23 2020 -0500 @@ -2,7 +2,7 @@ setuptools.setup( name='weatherlog', - version='0.2.1', + version='0.2.2', packages=setuptools.find_packages(), python_requires='>=3.7', install_requires=[ diff -r 9daa281d996b -r 7117db65715e weatherlog/daemon.py --- a/weatherlog/daemon.py Thu Jan 16 23:52:30 2020 -0500 +++ b/weatherlog/daemon.py Mon Jan 20 23:36:23 2020 -0500 @@ -2,6 +2,8 @@ import argparse import enum +import signal +import threading import time import typing as t @@ -21,18 +23,21 @@ log: logger.BufferedLogger, writer: logger.RemoteWriter, interval: int = DEFAULT_INTERVAL_SECS, -): +) -> None: """Sets up and runs a logger daemon.""" - log.start() + evt = threading.Event() + signal.signal(signal.SIGTERM, lambda *args: evt.set()) cycle = 0 start = time.time() + running = True + log.start() try: - while True: + while running: log.write(rd.read().as_dict()) cycle += 1 target = start + interval * cycle now = time.time() - time.sleep(max(target - now, MIN_INTERVAL_SECS)) + running = not evt.wait(max(target - now, MIN_INTERVAL_SECS)) finally: log.close() @@ -54,7 +59,7 @@ auth: t.Dict[str, object] -def parse_config(config_file: str): +def parse_config(config_file: str) -> _Config: with open(config_file, 'r') as infile: config = toml.load(infile) return _Config(