From 66d102abf14012d3bddf9d78c543e21f00a4a49b Mon Sep 17 00:00:00 2001 From: misyaguziya <53165965+misyaguziya@users.noreply.github.com> Date: Wed, 29 Oct 2025 04:46:46 +0900 Subject: [PATCH] =?UTF-8?q?test=5Fclient.py=E3=82=92=E6=94=B9=E5=96=84:=20?= =?UTF-8?q?watchdog=20=E3=82=B9=E3=83=AC=E3=83=83=E3=83=89=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0=E3=81=97=E3=81=A6=20/run/feed=5Fwatchdog=20?= =?UTF-8?q?=E3=82=9230=E7=A7=92=E9=96=93=E9=9A=94=E3=81=A7=E9=80=81?= =?UTF-8?q?=E4=BF=A1=E3=80=81=E5=88=9D=E6=9C=9F=E5=8C=96=E5=BE=8C=E3=81=AB?= =?UTF-8?q?=E8=B5=B7=E5=8B=95=E3=83=BBcleanup=E3=81=A7=E5=81=9C=E6=AD=A2?= =?UTF-8?q?=E3=81=97=E3=81=A6join=E3=81=99=E3=82=8B=E5=87=A6=E7=90=86?= =?UTF-8?q?=E3=81=A8=E9=80=81=E4=BF=A1=E3=82=A8=E3=83=A9=E3=83=BC=E3=81=AE?= =?UTF-8?q?=E3=83=8F=E3=83=B3=E3=83=89=E3=83=AA=E3=83=B3=E3=82=B0=E3=82=92?= =?UTF-8?q?=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src-python/test_client.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src-python/test_client.py b/src-python/test_client.py index 4000b316..9f68a48b 100644 --- a/src-python/test_client.py +++ b/src-python/test_client.py @@ -13,6 +13,7 @@ import json import base64 import sys import time +import threading from typing import Optional, Dict, Any if os.path.exists("config.json"): @@ -40,11 +41,16 @@ class TestClient: bufsize=1, cwd='.' ) + self._watchdog_stop_event = threading.Event() + self._watchdog_thread: Optional[threading.Thread] = None # 初期化完了を待つ print(f"{Color.CYAN}バックエンドの初期化を待機中...{Color.RESET}") self._wait_for_initialization() print(f"{Color.GREEN}バックエンド起動完了{Color.RESET}\n") + + # 初期化完了後 watchdog 開始 + self._start_watchdog() def _wait_for_initialization(self, timeout: Optional[float] = None): """バックエンド初期化完了 (/run/initialization_complete) を待機する。 @@ -252,9 +258,36 @@ class TestClient: traceback.print_exc() return {"status": 500, "endpoint": endpoint, "result": f"Error: {e}"} + def _start_watchdog(self): + """watchdog スレッドを開始 (30秒間隔で /run/feed_watchdog 送信)""" + def _watchdog_loop(): + print(f"{Color.CYAN}[Watchdog]{Color.RESET} 開始 (30秒間隔)") + while not self._watchdog_stop_event.is_set(): + if self._watchdog_stop_event.wait(timeout=30): + break + if self.process.poll() is None: + try: + request = {"endpoint": "/run/feed_watchdog"} + request_json = json.dumps(request, ensure_ascii=False) + self.process.stdin.write(request_json + '\n') + self.process.stdin.flush() + # レスポンスは受信しない(バックグラウンド送信) + except Exception as e: + print(f"{Color.YELLOW}[Watchdog]{Color.RESET} 送信エラー: {e}") + break + print(f"{Color.CYAN}[Watchdog]{Color.RESET} 終了") + + self._watchdog_thread = threading.Thread(target=_watchdog_loop, daemon=True) + self._watchdog_thread.start() + def cleanup(self): """バックエンドプロセスを終了""" print(f"\n{Color.CYAN}バックエンドプロセスを終了中...{Color.RESET}") + # watchdog 停止 + if self._watchdog_thread and self._watchdog_thread.is_alive(): + self._watchdog_stop_event.set() + self._watchdog_thread.join(timeout=2) + # プロセス終了 self.process.terminate() try: self.process.wait(timeout=5)