diff --git a/scripts/cron_health_monitor.py b/scripts/cron_health_monitor.py index 501ee28d..7d1da411 100644 --- a/scripts/cron_health_monitor.py +++ b/scripts/cron_health_monitor.py @@ -46,14 +46,48 @@ def xmpp_push(text): def _create_cron_todo(tname, desc): - """为故障cron创建TODO供自愈处理""" + """为故障cron创建TODO,fix_action带自动重试+修复逻辑""" try: + # 找这个cron对应的脚本 + script_path = None + try: + import json as _j + with open("/home/hmo/.hermes/profiles/position-analyst/cron/jobs.json") as _f: + _crons = _j.load(_f).get("jobs", []) + for _c in _crons: + if _c.get("name") == tname: + _s = _c.get("script", "") + if _s: + _sp = f"/home/hmo/.hermes/profiles/position-analyst/scripts/{_s.split()[0]}" + if __import__('os').path.exists(_sp): + script_path = _sp + break + except Exception: + pass + + if script_path: + fix = ( + f"cd /home/hmo/MoFin && timeout 60 python3 {script_path} 2>&1 " + f"| tail -5; if [ $? -eq 0 ]; then " + f"python3 -c \"import json; p='/home/hmo/.hermes/profiles/position-analyst/cron/jobs.json'; " + f"d=json.load(open(p)); " + f"for j in d['jobs']: " + f" if j.get('name')=='{tname}': j['last_status']='ok'; " + f"json.dump(d,open(p,'w'),ensure_ascii=False,indent=2)\" " + f"&& echo 'AUTO_FIXED'; " + f"else echo 'AUTO_FAIL'; fi" + ) + else: + fix = ( + f"cd /home/hmo/MoFin && echo 'check cron: {tname}' && " + f"python3 scripts/preflight_verify.py --cron-check" + ) + _c = __import__('sqlite3').connect("/home/hmo/MoFin/data/mofin.db", timeout=10) _c.execute( "INSERT OR REPLACE INTO todos (id, title, description, status, priority, source, fix_action, created_at) " "VALUES (?, ?, ?, 'pending', 'high', 'cron_health', ?, datetime('now','localtime'))", - (f"cron_err_{tname.replace(' ','_')}", f"[CRON_ERROR] {tname}", desc, - f"cd /home/hmo/MoFin && python3 scripts/preflight_verify.py --cron-check") + (f"cron_err_{tname.replace(' ','_')}", f"[CRON_ERROR] {tname}", desc, fix) ) _c.commit() _c.close()