Files
sanguo_vnpy/archive/2026-04-29-cleanup/test/backtest/monitor_task.py
T
2026-04-29 20:15:25 +08:00

79 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import requests
import json
import time
from datetime import datetime
BASE_URL = "http://localhost:7891/api"
task_id = "JJC-20260401-003"
print("="*80)
print(f" 🔍 监控任务: {task_id}")
print("="*80)
max_rounds = 60
round_num = 0
last_state = None
last_now = None
while round_num < max_rounds:
round_num += 1
current_time = datetime.now().strftime('%H:%M:%S')
try:
r = requests.get(f"{BASE_URL}/live-status")
live_data = r.json()
tasks = live_data.get('tasks', [])
our_task = None
for t in tasks:
if t.get('id') == task_id:
our_task = t
break
if our_task:
current_state = our_task.get('state')
current_now = our_task.get('now')
if current_state != last_state or current_now != last_now:
print(f"\n[{current_time}] 📢 状态变化!")
print(f" 任务: {our_task.get('title')}")
print(f" 状态: {current_state}")
print(f" 当前: {current_now}")
flow_log = our_task.get('flow_log', [])
if flow_log:
print(f"\n 最新流程日志:")
last_log = flow_log[-1] if flow_log else None
if last_log:
print(f" 时间: {last_log.get('at')}")
print(f" 从: {last_log.get('from')}")
print(f" 到: {last_log.get('to')}")
print(f" 备注: {last_log.get('remark')}")
if current_state == 'Done':
print(f"\n 🎉 任务完成!")
print(f" 输出: {our_task.get('output')}")
break
elif current_state in ('Cancelled', 'Blocked'):
print(f"\n ⚠️ 任务终止!状态: {current_state}")
print(f" 阻塞原因: {our_task.get('block')}")
break
last_state = current_state
last_now = current_now
else:
print(f"[{current_time}] ⏳ 等待... (状态: {current_state})", end='\r')
else:
print(f"[{current_time}] ❌ 找不到任务 {task_id}")
except Exception as e:
print(f"[{current_time}] ⚠️ 请求异常: {e}")
time.sleep(5)
print("\n" + "="*80)
print(" 📊 监控结束")
print("="*80)