👍️[Update] Model : Watchdog timerの終了処理をmain loopの終了処理に変更

This commit is contained in:
misyaguziya
2024-09-27 01:02:20 +09:00
parent 71225bf045
commit a0407afa54
6 changed files with 35 additions and 26 deletions

View File

@@ -1000,8 +1000,8 @@ class Config:
self._MAX_MIC_THRESHOLD = 2000 self._MAX_MIC_THRESHOLD = 2000
self._MAX_SPEAKER_THRESHOLD = 4000 self._MAX_SPEAKER_THRESHOLD = 4000
self._WATCHDOG_TIMEOUT = 30 self._WATCHDOG_TIMEOUT = 60
self._WATCHDOG_INTERVAL = 1 self._WATCHDOG_INTERVAL = 20
# Read Write # Read Write
self._ENABLE_TRANSLATION = False self._ENABLE_TRANSLATION = False

View File

@@ -783,12 +783,20 @@ class Model:
self.overlay.shutdownOverlay() self.overlay.shutdownOverlay()
def startWatchdog(self): 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): def feedWatchdog(self):
self.watchdog.feed() self.watchdog.feed()
def setWatchdogCallback(self, callback):
self.watchdog.setCallback(callback)
def stopWatchdog(self): 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() model = Model()

View File

@@ -1,30 +1,22 @@
import os from typing import Callable
import time import time
from threading import Thread, Event
from utils import printLog from utils import printLog
class Watchdog: class Watchdog:
def __init__(self, timeout:int=60, interval:int=1): def __init__(self, timeout:int=60, interval:int=20):
self.timeout = timeout self.timeout = timeout
self.interval = interval self.interval = interval
self.last_feed_time = time.time() 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): def feed(self):
self.last_feed_time = time.time() self.last_feed_time = time.time()
def _watchdog_loop(self): def setCallback(self, callback):
while not self._stop_event.is_set(): self.callback = callback
if time.time() - self.last_feed_time > self.timeout:
printLog("Watchdog timeout! Shutting down...")
os._exit(1)
time.sleep(self.interval) 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)

View File

@@ -1537,6 +1537,10 @@ class Controller:
model.feedWatchdog() model.feedWatchdog()
return {"status":200, "result":True} return {"status":200, "result":True}
@staticmethod
def setWatchdogCallback(callback) -> dict:
model.setWatchdogCallback(callback)
@staticmethod @staticmethod
def stopWatchdog(*args, **kwargs) -> dict: def stopWatchdog(*args, **kwargs) -> dict:
model.stopWatchdog() model.stopWatchdog()

View File

@@ -319,6 +319,7 @@ mapping = {
class Main: class Main:
def __init__(self) -> None: def __init__(self) -> None:
self.queue = Queue() self.queue = Queue()
self.main_loop = True
def receiver(self) -> None: def receiver(self) -> None:
while True: while True:
@@ -380,15 +381,19 @@ class Main:
th_handler.daemon = True th_handler.daemon = True
th_handler.start() th_handler.start()
def loop(self) -> None: def start(self) -> None:
while True: while self.main_loop:
time.sleep(1) time.sleep(1)
def stop(self) -> None:
self.main_loop = False
if __name__ == "__main__": if __name__ == "__main__":
main = Main() main = Main()
main.startReceiver() main.startReceiver()
main.startHandler() main.startHandler()
controller.setWatchdogCallback(main.stop)
controller.init() controller.init()
# mappingのすべてのstatusをTrueにする # mappingのすべてのstatusをTrueにする
@@ -398,7 +403,7 @@ if __name__ == "__main__":
process = "main" process = "main"
match process: match process:
case "main": case "main":
main.loop() main.start()
case "test": case "test":
for _ in range(100): for _ in range(100):

View File

@@ -142,5 +142,5 @@ const startFeedingToWatchDog = () => {
const { asyncStdoutToPython } = useStdoutToPython(); const { asyncStdoutToPython } = useStdoutToPython();
setInterval(() => { setInterval(() => {
asyncStdoutToPython("/run/feed_watchdog"); asyncStdoutToPython("/run/feed_watchdog");
}, 10000); // 10000ミリ秒 = 10秒 }, 20000); // 20000ミリ秒 = 20秒
}; };