hc-spider/hc_spider/watchdog.py

37 lines
1.1 KiB
Python

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)