S3 让 `status` 可信,S4 让**时间线完整**:账号上发生的事,时间线里都能查到。 ## 补齐「定义了却从没发过」的事件 `assigned` / `tag_changed` 这两个类型早就定义了、前端 `EVENT_STYLE` 也渲染了, 但**全仓没有任何代码发过**(`grep` 只有定义处)。本次接上: - 单个分配 / 批量分配 → `assigned`(detail 带 assigned_to / assigned_username) - 单个改标签 / 批量改标签 / 按筛选批量改标签 → `tag_changed`(detail 带 from/to) ## 新增 `ck_cleared`,并修掉 `ck_invalid` 的刷屏 - **`ck_cleared`**:清 CK 是**不可逆**操作(重登当前成功率极低),时间线里却不留痕。 现在单个清除与批量清除共用 `account_lifecycle.on_ck_cleared()`。 - **`ck_invalid` 改为只在「由好变坏」时发**。旧实现每轮巡检只要判定 invalid 就发一条, 源码注释写着「避免每轮刷屏」,**但代码并没有做到** —— 6 小时一轮、失效一周就是 28 条一模一样的事件。判定失效是**一次状态转变**,不是每轮读数。`error`(查不到) 同样不改变结论、不发事件。 ## 审计与时间线一次写完(防漏一边) 新增 `audit_service.record_account_action()`:**审计 + 账号事件一起写**,并把 `account_id` 落到审计上。这两件事必须成对出现 —— 只写审计,时间线就缺「谁改了什么」; 只发事件,合规审计就缺一条。分成两次调用写,迟早有人漏一边(`assigned` 就是这么漏的)。 顺带把批量删除的审计补上被删账号 id(`detail.account_ids`,截前 200 条): 删账号会**连带删掉它的事件时间线**(外键),审计是唯一痕迹 —— 这正是 10-03 那次事故要留的东西。 ## 批量操作不再逐条开 SAVEPOINT 批量分配/改标签可能覆盖几千个账号,而每个账号的时间线都该各留一条。 逐条走 `emit()` 会给每条开一个 SAVEPOINT,几千条时请求明显变慢。 新增 `emit_bulk()`:一次 executemany,整批包在**一个** SAVEPOINT 里, 与 `emit()` 一样**绝不抛异常**(整批失败就整批丢弃 + warning,不影响业务写入)。 ## 检测入口收口:4 处 → 1 个 `on_ck_verdict()` `ck_health` / 人工检测 / 工作台执行前检测 / 重登后复检 原本各自写 `ck_check_status` + `ck_check_result` + `ck_checked_at` + 发事件 + 调 `sync_status`, 口径容易分叉(`ck_invalid` 的刷屏就是这么来的)。现在统一走 `account_lifecycle.on_ck_verdict()`,顺序固定:**写结论 → 由好变坏记事件 → 重算状态**。 至此 `account_lifecycle` 成为**唯一**写生命周期字段的模块,并加了一条**源码扫描用例** 把它钉死(任何地方直接 `acc.status = ...` 都会让它红,并指出 file:line)—— 比写在文档里的约定可靠。虎牙的 `huya_accounts.status` 是同一个病,但属另一套模型, 已在用例里显式排除并注明。 ## 接口与前端 - 时间线支持 `?category=` 筛选(lifecycle / auth / business / ops); 响应补 `category` / `subject_*` / `actor_*` - 前端 `EVENT_STYLE` 补上 `status_changed`(**S3 我漏的**:加了事件类型却没加样式, 页面上显示成原始英文)与 `ck_cleared`;时间线加「分类」筛选 ## 验证 - `TZ=Asia/Shanghai`:**420 passed / 6 skipped**(S3 后 404 → 新增 16 条) - 迁移链 3 passed;ruff check/format 全绿;前端 `tsc -b && vite build` 与 `npm run lint`(0 errors)均通过 - **两条关键用例都做过「反向验证」**(先注入旧行为确认会红): - 把 `ck_invalid` 改回「每轮都发」→ 用例报 `3 == 1`; - 在路由里直接写 `acc.status = ...` → 不变量用例给出精确 file:line。 - 新用例还覆盖:批量分配/改标签逐账号发事件、清 CK 事件与审计关联、 `emit_bulk` 的跳过与容错、时间线分类筛选、批量删除的 id 留痕。 未做(后续):S5 业务事实直发事件 + `douyu_tasks` 保留策略、S6 账号页重构 + `bind_status` 三列拆解。`remark_changed` 目前没有改备注的接口,故无发射点(已在文档标注)。
193 lines
8.4 KiB
Python
193 lines
8.4 KiB
Python
"""斗鱼活动任务批次执行器(入口聚合;功能域已拆分到 douyu_runner_*.py)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from concurrent.futures import ThreadPoolExecutor, as_completed
|
|
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from core.douyu import DouyuActivityError
|
|
|
|
from ..database import SessionLocal
|
|
from ..models import DouyuTask
|
|
from .account_lifecycle import on_ck_verdict
|
|
from .cookie_check_service import check_douyu_cookie
|
|
from .douyu_runner_bind import BindMixin
|
|
from .douyu_runner_core import ( # noqa: F401 (douyu_batch_registry 供 routers 重导出)
|
|
DouyuBatchRunnerCore,
|
|
douyu_batch_registry,
|
|
)
|
|
from .douyu_runner_donate import DonateMixin
|
|
from .douyu_runner_gold import GoldMixin
|
|
from .douyu_runner_goods import GoodsMixin
|
|
from .douyu_runner_manual import ManualMixin
|
|
from .douyu_runner_xpd import XpdMixin
|
|
from .douyu_service import update_account_profile_from_cookie
|
|
|
|
|
|
class DouyuBatchRunner(
|
|
DouyuBatchRunnerCore,
|
|
BindMixin,
|
|
ManualMixin,
|
|
GoldMixin,
|
|
DonateMixin,
|
|
GoodsMixin,
|
|
XpdMixin,
|
|
):
|
|
"""批量执行斗鱼活动任务(功能域 Mixin 聚合 + 批次调度)。"""
|
|
|
|
def _execute_one(self, task_id: int, config: dict, total: int):
|
|
worker_db = SessionLocal()
|
|
try:
|
|
task = (
|
|
worker_db.query(DouyuTask)
|
|
.options(joinedload(DouyuTask.account))
|
|
.filter(DouyuTask.id == task_id)
|
|
.first()
|
|
)
|
|
if not task or self._stop.is_set():
|
|
return
|
|
account = task.account
|
|
self._update_task_progress(worker_db, task, "running", "执行中")
|
|
|
|
with self._counter_lock:
|
|
self._started += 1
|
|
current = self._started
|
|
|
|
self._push_log(
|
|
"info", f"[{current}/{total}] 开始: {self._account_name(account)}"
|
|
)
|
|
# CK 与健康状态都挂在账号上(2026-10-04 CK 解耦)。
|
|
cookie = account.cookie or ""
|
|
if not cookie:
|
|
self._mark_task(worker_db, task, "failed", "账号没有可用登录 Cookie")
|
|
self._push_log(
|
|
"warning", f"[{current}] {self._account_name(account)} 无 Cookie"
|
|
)
|
|
return
|
|
|
|
cookie_check = check_douyu_cookie(cookie)
|
|
# 执行前的这次检测同样要写结论、由好变坏记事件、重算状态。
|
|
on_ck_verdict(
|
|
worker_db,
|
|
account,
|
|
"valid" if cookie_check["valid"] else "invalid",
|
|
detail=cookie_check,
|
|
occurred_at=cookie_check["checked_at"],
|
|
)
|
|
worker_db.commit()
|
|
if not cookie_check["valid"]:
|
|
message = f"Cookie 已失效,请重新登录:{cookie_check['message']}"
|
|
self._mark_task(worker_db, task, "failed", message)
|
|
self._push_log(
|
|
"warning", f"[{current}] {self._account_name(account)} {message}"
|
|
)
|
|
return
|
|
|
|
update_account_profile_from_cookie(account, cookie)
|
|
|
|
handler = {
|
|
"refresh_goods": self._execute_refresh_goods,
|
|
"refresh_esports_goods": self._execute_refresh_esports_goods,
|
|
"get_bind_qr": self._execute_get_bind_qr,
|
|
"confirm_bind": self._execute_confirm_bind,
|
|
"create_elite_qr": self._execute_create_elite_qr,
|
|
"query_elite_status": self._execute_query_elite_status,
|
|
"prepare_esports_bind": self._execute_prepare_esports_bind,
|
|
"get_esports_bind_qr": self._execute_get_esports_bind_qr,
|
|
"query_esports_game_name": self._execute_query_esports_game_name,
|
|
"confirm_esports_bind": self._execute_confirm_esports_bind,
|
|
"create_esports_qr": self._execute_create_esports_qr,
|
|
"query_esports_points": self._execute_query_esports_points,
|
|
"donate_esports_chicken_gift": self._execute_donate_esports_chicken_gift,
|
|
"donate_esports_firework_gift": self._execute_donate_esports_firework_gift,
|
|
"create_gold_qr": self._execute_create_gold_qr,
|
|
"donate_elite_gift": self._execute_donate_elite_gift,
|
|
"query_points": self._execute_query_points,
|
|
"lock_goods": self._execute_lock_goods,
|
|
"pay_locked_order": self._execute_pay_locked_order,
|
|
"exchange_goods": self._execute_exchange_goods,
|
|
"exchange_esports_goods": self._execute_exchange_esports_goods,
|
|
"query_game_name": self._execute_query_game_name,
|
|
"query_change_bind_time": self._execute_query_change_bind_time,
|
|
"query_limited_goods": self._execute_query_limited_goods,
|
|
"query_gold_balance": self._execute_query_gold_balance,
|
|
"query_exchange_records": self._execute_query_exchange_records,
|
|
"prefetch_csrf_token": self._execute_prefetch_csrf_token,
|
|
"get_xpd_bind_qr": self._execute_get_xpd_bind_qr,
|
|
"query_xpd_bind_info": self._execute_query_xpd_bind_info,
|
|
"confirm_xpd_bind": self._execute_confirm_xpd_bind,
|
|
"query_xpd_role": self._execute_query_xpd_role,
|
|
"refresh_xpd_goods": self._execute_refresh_xpd_goods,
|
|
"query_xpd_balance": self._execute_query_xpd_balance,
|
|
"query_xpd_fragments": self._execute_query_xpd_fragments,
|
|
"query_xpd_purchase_records": self._execute_query_xpd_purchase_records,
|
|
"exchange_xpd_goods": self._execute_exchange_xpd_goods,
|
|
}.get(task.task_type)
|
|
if handler is None:
|
|
self._mark_task(worker_db, task, "failed", "不支持的任务类型")
|
|
return
|
|
|
|
handler(worker_db, task, account, cookie, config)
|
|
self._push_log(
|
|
"success", f"[{current}] {self._account_name(account)} {task.message}"
|
|
)
|
|
except DouyuActivityError as exc:
|
|
if "task" in locals() and task:
|
|
self._mark_task(worker_db, task, "failed", str(exc))
|
|
self._push_log("warning", f"斗鱼任务失败: {exc}")
|
|
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
|
if "task" in locals() and task:
|
|
self._mark_task(worker_db, task, "error", str(exc))
|
|
self._push_log("error", f"斗鱼任务异常: {exc}")
|
|
finally:
|
|
worker_db.close()
|
|
|
|
def run(self):
|
|
"""执行批次任务。"""
|
|
self._push_log("info", f"斗鱼任务批次 {self.batch_id} 开始")
|
|
try:
|
|
config = self._config_info(self.db)
|
|
tasks = (
|
|
self.db.query(DouyuTask)
|
|
.filter(
|
|
DouyuTask.batch_id == self.batch_id, DouyuTask.status == "planned"
|
|
)
|
|
.order_by(DouyuTask.id.asc())
|
|
.all()
|
|
)
|
|
if not tasks:
|
|
self._push_log("warning", "没有可执行的斗鱼任务")
|
|
self._push_log("result", "")
|
|
return
|
|
|
|
for task in tasks:
|
|
task.status = "pending"
|
|
task.message = "等待执行"
|
|
self.db.commit()
|
|
for task in tasks:
|
|
self._push_task_event(task)
|
|
|
|
total = len(tasks)
|
|
with ThreadPoolExecutor(max_workers=self.concurrency) as executor:
|
|
futures = []
|
|
for task in tasks:
|
|
if self._stop.is_set():
|
|
break
|
|
futures.append(
|
|
executor.submit(self._execute_one, task.id, config, total)
|
|
)
|
|
for future in as_completed(futures):
|
|
try:
|
|
future.result()
|
|
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
|
self._push_log("error", f"Worker 异常: {exc}")
|
|
|
|
if self._stop.is_set():
|
|
self._push_log("warning", f"斗鱼任务批次 {self.batch_id} 已停止")
|
|
else:
|
|
self._push_log("info", f"斗鱼任务批次 {self.batch_id} 完成")
|
|
self._push_log("result", "")
|
|
finally:
|
|
self.db.close()
|