fix(计划): 定时触发补 app context;账号状态加检测时间与历史记录
- _fire 内调用 run_plan(修复 18:00 Working outside application context,无运行记录) - device_account 新增 status_checked_at;新表 account_status_log(schema v23) - 每次检测/手改留痕;资产表显示检测时间 + 「记录」弹窗 - API /api/af/account_status_logs;备份标签/文档/测试同步
This commit is contained in:
@@ -363,6 +363,40 @@ finally:
|
||||
device_pool.list_devices, device_pool.list_online, device_pool.serial_of = (
|
||||
_orig_devices, _orig_online, _orig_serial_of)
|
||||
|
||||
print("▶ ⑥ 定时触发:run_plan 必须在 app context 内调用")
|
||||
import threading # noqa: E402
|
||||
from flask import has_app_context # noqa: E402
|
||||
with app.app_context():
|
||||
db.session.add(AePlan(id="firetest", name="firetest", enabled=True,
|
||||
accounts=json.dumps(["号甲"]),
|
||||
segments=json.dumps([]),
|
||||
schedule=json.dumps({"points": ["09:00"]}),
|
||||
created_at="2026-10-17 00:00:00",
|
||||
updated_at="2026-10-17 00:00:00"))
|
||||
db.session.commit()
|
||||
_seen = []
|
||||
_orig_run = plan_mod.run_plan
|
||||
|
||||
|
||||
def _fake_run(pid, trigger="manual"):
|
||||
_seen.append((pid, trigger, has_app_context()))
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
plan_mod.run_plan = _fake_run
|
||||
try:
|
||||
_t = threading.Thread(target=plan_mod._fire, args=("firetest",))
|
||||
_t.start()
|
||||
_t.join(timeout=10)
|
||||
finally:
|
||||
plan_mod.run_plan = _orig_run
|
||||
with app.app_context():
|
||||
_row = AePlan.query.get("firetest")
|
||||
if _row:
|
||||
db.session.delete(_row)
|
||||
db.session.commit()
|
||||
check("定时触发时 run_plan 有 app context", _seen, [("firetest", "cron", True)])
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print("❌ 失败 %d 项:%s" % (len(fails), "、".join(fails)))
|
||||
|
||||
Reference in New Issue
Block a user