Files

49 lines
1.6 KiB
Python

"""ジョブ完了待ちポーリング。Windmill の success フィールドで判定する。"""
import time
from windmill_client import get_job, get_job_logs
from config import POLL_INTERVAL, POLL_MAX_COUNT
class JobTimeout(Exception):
pass
def poll_until_done(job_id: str) -> tuple[bool, str]:
"""
ジョブが完了するまでポーリングする。
判定優先順位:
1. success is False → 失敗(即返却)
2. success is True → 成功(即返却)
3. それ以外 → 継続待機
ログ文字列は主判定に使わない(誤検知防止)。
Returns:
(success: bool, logs: str)
Raises:
JobTimeout: POLL_MAX_COUNT * POLL_INTERVAL 秒以内に完了しなかった場合
"""
for _ in range(POLL_MAX_COUNT):
job = get_job(job_id)
success = job.get("success")
if success is False:
logs = get_job_logs(job_id)
# result.error があればログに付加(ログが空でもエラー詳細を取得できる)
result_error = job.get("result", {}) or {}
error_detail = result_error.get("error", {}) or {}
error_msg = error_detail.get("message", "")
if error_msg and error_msg not in (logs or ""):
logs = f"{logs}\n[result.error] {error_msg}".strip()
return False, logs
if success is True:
logs = get_job_logs(job_id)
return True, logs
time.sleep(POLL_INTERVAL)
timeout_sec = POLL_MAX_COUNT * POLL_INTERVAL
raise JobTimeout(f"ジョブ {job_id}{timeout_sec} 秒以内に完了しませんでした")