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:
+27
-7
@@ -173,6 +173,7 @@ def list_accounts(prof=None):
|
||||
"has_dir": has_dir,
|
||||
"device_name": rr.get("device_name") or "",
|
||||
"account_status": rr.get("account_status") or "",
|
||||
"status_checked_at": rr.get("status_checked_at") or "",
|
||||
"serial": rr.get("serial") or "",
|
||||
"pooled": bool(rr.get("pooled")),
|
||||
"online": bool(rr.get("online")),
|
||||
@@ -1144,24 +1145,43 @@ def roster_rows():
|
||||
return out
|
||||
|
||||
|
||||
def set_account_status(name, status=""):
|
||||
"""按账号名写账号库的「账号状态」(任务变量回写用)。
|
||||
def set_account_status(name, status="", source="", run_id=""):
|
||||
"""按账号名写账号库的「账号状态」,并追加一条检测记录。
|
||||
|
||||
名字先精确匹配昵称,再按抖音号兜底;账号库里没有这个号 → 返回 False(**不新建行**)。
|
||||
供任务里的「读取文字」在变量名是 `account_status` 时调用:读到什么写什么,
|
||||
读不到(元素不存在)写「正常」。
|
||||
供任务里的「读取文字」在变量名是 `account_status` 时调用:读到什么写什么;
|
||||
读不到时调用方不调用(保留原值)。同一 `run_id` + 账号只留一条记录
|
||||
(一次运行里重复写时更新那一条,不刷重复历史)。
|
||||
"""
|
||||
from core.models import db, DeviceAccount
|
||||
from core.models import db, DeviceAccount, AccountStatusLog
|
||||
key = (name or "").strip()
|
||||
if not key:
|
||||
return False
|
||||
now = _now()
|
||||
val = (status or "").strip()[:80]
|
||||
with _ctx():
|
||||
row = (DeviceAccount.query.filter_by(nickname=key).first()
|
||||
or DeviceAccount.query.filter_by(douyin_id=key).first())
|
||||
if row is None:
|
||||
return False
|
||||
row.account_status = (status or "").strip()[:80]
|
||||
row.updated_at = _now()
|
||||
row.account_status = val
|
||||
row.status_checked_at = now
|
||||
row.updated_at = now
|
||||
log = None
|
||||
if run_id:
|
||||
log = AccountStatusLog.query.filter_by(
|
||||
nickname=row.nickname, run_id=str(run_id)[:32]).first()
|
||||
if log is not None:
|
||||
log.status = val
|
||||
log.detected_at = now
|
||||
log.device_name = row.device_name or log.device_name
|
||||
else:
|
||||
db.session.add(AccountStatusLog(
|
||||
id=uuid.uuid4().hex[:8], account_id=row.id,
|
||||
nickname=row.nickname, device_name=row.device_name or "",
|
||||
serial=row.serial or "", status=val,
|
||||
source=(source or "")[:40], run_id=str(run_id or "")[:32],
|
||||
detected_at=now, created_at=now))
|
||||
db.session.commit()
|
||||
return True
|
||||
|
||||
|
||||
@@ -565,6 +565,8 @@ class DeviceAccount(db.Model):
|
||||
# 账号状态(正常 / 账号异常 / 限流…):资产管理里可手改;任务里「读取文字」
|
||||
# 存到变量 `account_status` 时会自动回写这一列(见 tasks/generic/task.py)。
|
||||
account_status = db.Column(db.String(80), default="")
|
||||
# 最近一次「检测到状态」的时间(历史明细在 account_status_log)
|
||||
status_checked_at = db.Column(db.String(20), default="")
|
||||
created_at = db.Column(db.String(20), default="")
|
||||
updated_at = db.Column(db.String(20), default="")
|
||||
|
||||
@@ -577,12 +579,43 @@ class DeviceAccount(db.Model):
|
||||
"can_post_video": bool(self.can_post_video),
|
||||
"bio": self.bio or "", "note": self.note or "",
|
||||
"account_status": self.account_status or "",
|
||||
"status_checked_at": self.status_checked_at or "",
|
||||
"created_at": self.created_at or "", "updated_at": self.updated_at or ""}
|
||||
|
||||
def __repr__(self):
|
||||
return f"<DeviceAccount {self.device_name} {self.nickname}>"
|
||||
|
||||
|
||||
class AccountStatusLog(db.Model):
|
||||
"""账号状态检测记录:每次检测(或手动修改)追加一行。
|
||||
|
||||
资产页「账号状态」列只显示最新值;点「记录」看这个账号的历史:
|
||||
时间 + 状态 + 设备 + 来源(计划运行/任务运行/手动修改)。
|
||||
"""
|
||||
__tablename__ = "account_status_log"
|
||||
id = db.Column(db.String(32), primary_key=True)
|
||||
account_id = db.Column(db.String(32), default="", index=True)
|
||||
nickname = db.Column(db.String(80), default="", index=True)
|
||||
device_name = db.Column(db.String(80), default="")
|
||||
serial = db.Column(db.String(120), default="")
|
||||
status = db.Column(db.String(80), default="")
|
||||
source = db.Column(db.String(40), default="")
|
||||
run_id = db.Column(db.String(32), default="", index=True)
|
||||
detected_at = db.Column(db.String(20), default="", index=True)
|
||||
created_at = db.Column(db.String(20), default="")
|
||||
|
||||
def to_dict(self):
|
||||
return {"id": self.id, "account_id": self.account_id or "",
|
||||
"nickname": self.nickname or "", "device_name": self.device_name or "",
|
||||
"serial": self.serial or "", "status": self.status or "",
|
||||
"source": self.source or "", "run_id": self.run_id or "",
|
||||
"detected_at": self.detected_at or "",
|
||||
"created_at": self.created_at or ""}
|
||||
|
||||
def __repr__(self):
|
||||
return f"<AccountStatusLog {self.nickname} {self.status} @{self.detected_at}>"
|
||||
|
||||
|
||||
class VideoPlan(db.Model):
|
||||
"""视频发布计划:账号 × 发布日期 × 编号 → 一个视频素材 + 一条标题 + 发布结果。
|
||||
|
||||
@@ -1043,6 +1076,8 @@ SCHEMA_MIGRATIONS = [
|
||||
"「读取文字」写变量 account_status 时自动回写,用于创作者中心/异常检测)", None),
|
||||
(22, "应用管理·自动下载:新增 apk_source 表(下载直链、检查间隔、上次检查结果)",
|
||||
None),
|
||||
(23, "账号状态历史:device_account 新增 status_checked_at(最近检测时间);"
|
||||
"新增 account_status_log(每次检测/手改追加一行:状态/时间/设备/来源)", None),
|
||||
]
|
||||
|
||||
# 当前 schema 版本(备份/恢复用它判断新旧,也写进 app_meta.schema_version)
|
||||
|
||||
+7
-4
@@ -1000,15 +1000,18 @@ def _fire(plan_id):
|
||||
"""到点触发(APScheduler 调的,在自己的线程里)。失败只记日志,不冒泡。"""
|
||||
try:
|
||||
from core.models import AePlan
|
||||
# ⚠ `run_plan` 内部要查库/建 AeRun —— **必须在 app context 里调**;
|
||||
# APScheduler 的线程没有请求上下文,之前放在 with 外面会报
|
||||
# "Working outside of application context"(2026-10-21 18:00 实测)。
|
||||
with _ctx():
|
||||
row = AePlan.query.get(plan_id)
|
||||
if row is None or not row.enabled:
|
||||
return
|
||||
plan = row.to_dict()
|
||||
if not _in_date_range(plan) or not _in_window(plan):
|
||||
_log.info("计划 %s 到点但不在日期范围/运行窗口内 → 跳过", plan_id)
|
||||
return
|
||||
res = run_plan(plan_id, trigger="cron")
|
||||
if not _in_date_range(plan) or not _in_window(plan):
|
||||
_log.info("计划 %s 到点但不在日期范围/运行窗口内 → 跳过", plan_id)
|
||||
return
|
||||
res = run_plan(plan_id, trigger="cron")
|
||||
if not res.get("ok"):
|
||||
_log.warning("计划 %s 到点触发失败:%s", plan_id, res.get("error"))
|
||||
except Exception as e:
|
||||
|
||||
@@ -60,6 +60,7 @@ TABLE_LABELS = {
|
||||
"task_step_log": "任务步骤明细",
|
||||
"done_mark": "去重记录(已做过)",
|
||||
"device_account": "账号库(名字 ↔ 哪台手机)",
|
||||
"account_status_log": "账号状态检测记录(每次检测/手改)",
|
||||
"ae_plan": "计划调度(一行一个计划:干什么/给谁/发什么/什么时候/怎么跑)",
|
||||
"ae_plan_run": "计划运行记录(每次触发的成功/失败/跳过统计)",
|
||||
"video_publish_log": "素材发布记录(每条素材每次发布的时间/结果/设备/链接)",
|
||||
|
||||
Reference in New Issue
Block a user