blakeblackshear.frigate/frigate/watchdog.py

41 lines
1.3 KiB
Python
Raw Normal View History

2020-11-01 22:45:04 +01:00
import datetime
2020-11-04 04:26:39 +01:00
import logging
2020-11-01 22:45:04 +01:00
import threading
2020-11-04 13:31:25 +01:00
import time
import os
import signal
2020-11-01 22:45:04 +01:00
2020-11-04 04:26:39 +01:00
logger = logging.getLogger(__name__)
2021-02-17 14:23:32 +01:00
2020-11-01 22:45:04 +01:00
class FrigateWatchdog(threading.Thread):
def __init__(self, detectors, stop_event):
threading.Thread.__init__(self)
2021-02-17 14:23:32 +01:00
self.name = "frigate_watchdog"
2020-11-01 22:45:04 +01:00
self.detectors = detectors
self.stop_event = stop_event
def run(self):
time.sleep(10)
while True:
# wait a bit before checking
time.sleep(10)
if self.stop_event.is_set():
2020-11-04 04:26:39 +01:00
logger.info(f"Exiting watchdog...")
2020-11-01 22:45:04 +01:00
break
now = datetime.datetime.now().timestamp()
# check the detection processes
for detector in self.detectors.values():
detection_start = detector.detection_start.value
2021-02-17 14:23:32 +01:00
if detection_start > 0.0 and now - detection_start > 10:
logger.info(
"Detection appears to be stuck. Restarting detection process..."
)
2020-11-01 22:45:04 +01:00
detector.start_or_restart()
elif not detector.detect_process.is_alive():
2021-02-13 16:56:26 +01:00
logger.info("Detection appears to have stopped. Exiting frigate...")
os.kill(os.getpid(), signal.SIGTERM)