diff --git a/src-python/config.py b/src-python/config.py index 81d27b54..44f3fa4e 100644 --- a/src-python/config.py +++ b/src-python/config.py @@ -1000,8 +1000,8 @@ class Config: self._MAX_MIC_THRESHOLD = 2000 self._MAX_SPEAKER_THRESHOLD = 4000 - self._WATCHDOG_TIMEOUT = 30 - self._WATCHDOG_INTERVAL = 1 + self._WATCHDOG_TIMEOUT = 60 + self._WATCHDOG_INTERVAL = 20 # Read Write self._ENABLE_TRANSLATION = False diff --git a/src-python/model.py b/src-python/model.py index 4773d9d5..8c465e7e 100644 --- a/src-python/model.py +++ b/src-python/model.py @@ -783,12 +783,20 @@ class Model: self.overlay.shutdownOverlay() def startWatchdog(self): - self.watchdog.start() + self.th_watchdog = threadFnc(self.watchdog.start) + self.th_watchdog.daemon = True + self.th_watchdog.start() def feedWatchdog(self): self.watchdog.feed() + def setWatchdogCallback(self, callback): + self.watchdog.setCallback(callback) + def stopWatchdog(self): - self.watchdog.stop() + if isinstance(self.th_watchdog, threadFnc): + self.th_watchdog.stop() + self.th_watchdog.join() + self.th_watchdog = None model = Model() \ No newline at end of file diff --git a/src-python/models/watchdog/watchdog.py b/src-python/models/watchdog/watchdog.py index 10b5b9e1..089e8680 100644 --- a/src-python/models/watchdog/watchdog.py +++ b/src-python/models/watchdog/watchdog.py @@ -1,30 +1,22 @@ -import os +from typing import Callable import time -from threading import Thread, Event from utils import printLog class Watchdog: - def __init__(self, timeout:int=60, interval:int=1): + def __init__(self, timeout:int=60, interval:int=20): self.timeout = timeout self.interval = interval self.last_feed_time = time.time() - self._stop_event = Event() - self._watchdog_thread = Thread(target=self._watchdog_loop) - - def start(self): - self._watchdog_thread.start() - - def stop(self): - self._stop_event.set() - self._watchdog_thread.join() def feed(self): self.last_feed_time = time.time() - def _watchdog_loop(self): - while not self._stop_event.is_set(): - if time.time() - self.last_feed_time > self.timeout: - printLog("Watchdog timeout! Shutting down...") - os._exit(1) + def setCallback(self, callback): + self.callback = callback - time.sleep(self.interval) \ No newline at end of file + def start(self): + if time.time() - self.last_feed_time > self.timeout: + printLog("Watchdog timeout! Shutting down...") + if isinstance(self.callback, Callable): + self.callback() + time.sleep(self.interval) \ No newline at end of file diff --git a/src-python/webui_controller.py b/src-python/webui_controller.py index 23aa9a89..82edcd0a 100644 --- a/src-python/webui_controller.py +++ b/src-python/webui_controller.py @@ -1537,6 +1537,10 @@ class Controller: model.feedWatchdog() return {"status":200, "result":True} + @staticmethod + def setWatchdogCallback(callback) -> dict: + model.setWatchdogCallback(callback) + @staticmethod def stopWatchdog(*args, **kwargs) -> dict: model.stopWatchdog() diff --git a/src-python/webui_mainloop.py b/src-python/webui_mainloop.py index 800f4058..2e6b8197 100644 --- a/src-python/webui_mainloop.py +++ b/src-python/webui_mainloop.py @@ -319,6 +319,7 @@ mapping = { class Main: def __init__(self) -> None: self.queue = Queue() + self.main_loop = True def receiver(self) -> None: while True: @@ -380,15 +381,19 @@ class Main: th_handler.daemon = True th_handler.start() - def loop(self) -> None: - while True: + def start(self) -> None: + while self.main_loop: time.sleep(1) + def stop(self) -> None: + self.main_loop = False + if __name__ == "__main__": main = Main() main.startReceiver() main.startHandler() + controller.setWatchdogCallback(main.stop) controller.init() # mappingのすべてのstatusをTrueにする @@ -398,7 +403,7 @@ if __name__ == "__main__": process = "main" match process: case "main": - main.loop() + main.start() case "test": for _ in range(100): diff --git a/src-ui/app/App.jsx b/src-ui/app/App.jsx index 08eccea8..5d04fc92 100644 --- a/src-ui/app/App.jsx +++ b/src-ui/app/App.jsx @@ -142,5 +142,5 @@ const startFeedingToWatchDog = () => { const { asyncStdoutToPython } = useStdoutToPython(); setInterval(() => { asyncStdoutToPython("/run/feed_watchdog"); - }, 10000); // 10000ミリ秒 = 10秒 + }, 20000); // 20000ミリ秒 = 20秒 }; \ No newline at end of file