Added watchdog module

This commit is contained in:
Szirtesi,Tamás Gábor 2023-10-09 14:22:53 +02:00
parent bb1cab74df
commit 205b3e483d

36
hc_spider/watchdog.py Normal file
View File

@ -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)