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)))
|
||||
|
||||
@@ -21,7 +21,8 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspa
|
||||
os.chdir(sys.path[0])
|
||||
|
||||
from flask import Flask # noqa: E402
|
||||
from core.models import db, init_db, DeviceAccount, Device # noqa: E402
|
||||
from core.models import (db, init_db, DeviceAccount, Device, # noqa: E402
|
||||
AccountStatusLog)
|
||||
from core import account_folder as af # noqa: E402
|
||||
from core import account_probe as ap # noqa: E402
|
||||
from core import device_pool # noqa: E402
|
||||
@@ -249,6 +250,20 @@ check("账号正常列表", "锋哥苏州百业探访" in groups, True)
|
||||
check("没有统计字段(一个消息按类型列账号)",
|
||||
("total" in kw or "normal" in kw or "abnormal" in kw), False)
|
||||
|
||||
print("▶ ⑨ 账号状态检测记录(每次检测/手改留痕)")
|
||||
af.set_account_status("锋哥苏州乙探访", "作品异常", source="任务运行", run_id="runA")
|
||||
af.set_account_status("锋哥苏州乙探访", "作品异常", source="任务运行", run_id="runA")
|
||||
af.set_account_status("锋哥苏州乙探访", "账号正常", source="任务运行", run_id="runB")
|
||||
with app.app_context():
|
||||
logs = (AccountStatusLog.query.filter_by(nickname="锋哥苏州乙探访")
|
||||
.order_by(AccountStatusLog.detected_at).all())
|
||||
check("同一次运行只留一条", len([x for x in logs if x.run_id == "runA"]), 1)
|
||||
check("下一次运行记录新状态",
|
||||
[x.status for x in logs if x.run_id == "runB"], ["账号正常"])
|
||||
row = DeviceAccount.query.filter_by(nickname="锋哥苏州乙探访").first()
|
||||
check("账号库最新状态", row.account_status, "账号正常")
|
||||
check("检测时间已写", bool(row.status_checked_at), True)
|
||||
|
||||
print()
|
||||
if fails:
|
||||
print("❌ 失败 %d 项:%s" % (len(fails), "、".join(fails)))
|
||||
|
||||
Reference in New Issue
Block a user