diff --git a/hc_spider/watchdog.py b/hc_spider/watchdog.py new file mode 100644 index 0000000..33f64f4 --- /dev/null +++ b/hc_spider/watchdog.py @@ -0,0 +1,36 @@ +import os +import threading +import time + +from hc_spider.model import SharedObjects + + +class Watchdog(threading.Thread): + _shared_objects: SharedObjects + + def __init__(self, **kwargs) -> None: + self._shared_objects = SharedObjects(**kwargs) + super().__init__() + self.daemon = True + self.name = "Watchdog" + + def run(self) -> None: + print(f"{self.name} started with pid [{os.getpid()}]") + fifo_file = self._shared_objects.config.get("watchdog_file") + if os.path.exists(fifo_file) is False: + os.mkfifo(fifo_file) + + with open(fifo_file) as f: + while True: + time.sleep(1) + data = f.read().strip("\n") + match data: + case "exit": + self._shared_objects.shutdown_event.set() + self._shared_objects.watchdog_event.set() + break + + print(f"[{self.name}] is shutting down", flush=True) + + def __del__(self) -> None: + print(f"[{self.name}] exited", flush=True)