watchdog.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import datetime
  2. import logging
  3. import threading
  4. import time
  5. import os
  6. import signal
  7. logger = logging.getLogger(__name__)
  8. class FrigateWatchdog(threading.Thread):
  9. def __init__(self, detectors, stop_event):
  10. threading.Thread.__init__(self)
  11. self.name = "frigate_watchdog"
  12. self.detectors = detectors
  13. self.stop_event = stop_event
  14. def run(self):
  15. time.sleep(10)
  16. while not self.stop_event.wait(10):
  17. now = datetime.datetime.now().timestamp()
  18. # check the detection processes
  19. for detector in self.detectors.values():
  20. detection_start = detector.detection_start.value
  21. if detection_start > 0.0 and now - detection_start > 10:
  22. logger.info(
  23. "Detection appears to be stuck. Restarting detection process..."
  24. )
  25. detector.start_or_restart()
  26. elif not detector.detect_process.is_alive():
  27. logger.info("Detection appears to have stopped. Exiting frigate...")
  28. os.kill(os.getpid(), signal.SIGTERM)
  29. logger.info(f"Exiting watchdog...")