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
|
2023-05-29 12:31:17 +02:00
|
|
|
from multiprocessing.synchronize import Event as MpEvent
|
2020-11-01 22:45:04 +01:00
|
|
|
|
2022-11-04 03:23:09 +01:00
|
|
|
from frigate.object_detection import ObjectDetectProcess
|
2023-07-06 16:28:50 +02:00
|
|
|
from frigate.util.services import restart_frigate
|
2021-09-18 14:40:27 +02: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):
|
2022-11-19 14:16:33 +01:00
|
|
|
def __init__(self, detectors: dict[str, ObjectDetectProcess], stop_event: MpEvent):
|
2024-10-03 03:35:46 +02:00
|
|
|
super().__init__(name="frigate_watchdog")
|
2020-11-01 22:45:04 +01:00
|
|
|
self.detectors = detectors
|
|
|
|
self.stop_event = stop_event
|
|
|
|
|
2022-04-12 22:24:45 +02:00
|
|
|
def run(self) -> None:
|
2020-11-01 22:45:04 +01:00
|
|
|
time.sleep(10)
|
2021-05-21 17:39:14 +02:00
|
|
|
while not self.stop_event.wait(10):
|
2020-11-01 22:45:04 +01:00
|
|
|
now = datetime.datetime.now().timestamp()
|
|
|
|
|
|
|
|
# check the detection processes
|
|
|
|
for detector in self.detectors.values():
|
2023-07-01 14:47:16 +02:00
|
|
|
detection_start = detector.detection_start.value # type: ignore[attr-defined]
|
|
|
|
# issue https://github.com/python/typeshed/issues/8799
|
|
|
|
# from mypy 0.981 onwards
|
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()
|
2022-04-12 22:24:45 +02:00
|
|
|
elif (
|
|
|
|
detector.detect_process is not None
|
|
|
|
and not detector.detect_process.is_alive()
|
|
|
|
):
|
2023-01-13 14:18:15 +01:00
|
|
|
logger.info("Detection appears to have stopped. Exiting Frigate...")
|
2021-09-18 14:40:27 +02:00
|
|
|
restart_frigate()
|
2021-05-21 17:39:14 +02:00
|
|
|
|
2023-05-29 12:31:17 +02:00
|
|
|
logger.info("Exiting watchdog...")
|