👍️[Update] Model : Watchdog timerの終了処理をmain loopの終了処理に変更
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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()
|
||||
@@ -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)
|
||||
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)
|
||||
@@ -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()
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -142,5 +142,5 @@ const startFeedingToWatchDog = () => {
|
||||
const { asyncStdoutToPython } = useStdoutToPython();
|
||||
setInterval(() => {
|
||||
asyncStdoutToPython("/run/feed_watchdog");
|
||||
}, 10000); // 10000ミリ秒 = 10秒
|
||||
}, 20000); // 20000ミリ秒 = 20秒
|
||||
};
|
||||
Reference in New Issue
Block a user