watchdog.py 1.2 KB

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