`status` 列从「空壳」变成可信状态:S1 建了列、S2 让账号独立,本提交让状态真正
反映账号处境,并把「哪些账号要人处理」变成可筛选的事实。
## 状态机(7 → 6 个状态)
`imported` / `active` / `invalid` / `manual` / `disabled` / `archived`
**砍掉 `relogin`**:它完全等价于 `invalid + relogin_next_at is not None`,
独立成状态就多出一个必须与 `relogin_next_at` 同步的字段 —— 正是要消灭的「双份真相」。
## 唯一推导入口(防「双份真相」)
新增 `services/account_lifecycle.py`:
- `derive_status(account)` —— **纯函数**,按 cookie / ck_check_status / relogin_*
推导状态;人工态(disabled/archived)优先级最高,不被覆盖;
- `sync_status(db, account)` —— 推导 + 变了才写,**幂等**(没变则不写库、不发事件,
否则巡检每轮都往时间线刷一条);
- `set_manual_state(db, account, state)` —— 停用/归档/`auto`(恢复自动推导);
- `on_login_success(db, account)` —— **「登录成功对账号意味着什么」只定义一次**:
作废本轮重登退避痕迹 + 重算状态。于是**「待人工」的账号被人工登录救回来时状态
会自动恢复**,而不是卡在 manual。
- `MANUAL_KINDS` 从 `jobs/ck_relogin.py` 挪到这里(service 不该反向依赖 job)。
**9 处底层判据写入点全部接线**:登录成功、CK 清除(单个/批量)、ck_health 巡检、
人工 CK 检测、工作台执行前检测、重登复检、重登结算(成功/失败退避)。
## 消费端:补上一个真缺口 + 删掉两处临时派生
- ⚠️ **缺口**:`ck_health` / `ck_relogin` / `count_pending_relogin` 此前只按底层判据
选行,**没排除 disabled/archived** —— S2 提交里已标注,状态一上线就会变成
「停用的账号照样被自动重登」。现已全部排除。
- `automation.manual_queue` 原本用 4 个条件自己拼「需要人工」,现在一句
`status == 'manual'`。同一判据从「3 处各写一遍」收敛成「1 处推导」。
## 接口与前端
- `PUT /api/accounts/{id}/lifecycle`(disabled/archived/auto + 原因,写审计)
- `PUT /api/accounts/{id}/automation`(**接上死开关**:`auto_managed` 此前没有任何
接口/前端能改,永远是默认 True)
- 新增权限点 `account:manage`(运营默认拥有)
- 列表支持 `?status=` 筛选;**归档默认不出现**(要看需显式筛选)
- `AccountOut` / 概览接口补 status / status_label / status_reason / auto_managed
- 前端:账号列表加「状态」列与状态筛选;详情页状态卡 + 停用/归档/恢复/自动化开关
## 两个自查出来的问题(都已修)
1. **幂等键反而丢事件**:状态事件一开始用 `status:{id}:{target}:{秒级时间戳}` 作
`source_ref`,结果同一账号在同一秒内两次切到同一状态就撞唯一键,`emit` 静默吞掉
—— **状态改了、时间线却少一条**,正是本模块要消灭的「悄悄不一致」。
改为不传 `source_ref`:`sync_status` 本身已幂等,不存在重复写入路径。已加用例。
2. **迁移与运行时规则分叉**:迁移 0042 的状态回填 CASE 原本会写出已被砍掉的
`relogin`,且判据与 `derive_status` 不一致(次数用尽的会回填成 invalid 而非 manual)。
已改成逐字对应,并加一条用例**真跑那段 SQL 与纯函数逐条比对**(已验证:
临时改回旧 CASE 该用例立刻红)。
## 验证
- `TZ=Asia/Shanghai`:**404 passed / 6 skipped**(S2 后 373 → 新增 31 条)
- 迁移链 3 passed;ruff check/format 全绿;前端 `tsc -b && vite build` 通过
- 真实开发库核对:落库状态分布 active 4 / imported 8,**与 derive_status 逐账号比对无漂移**
- 新增用例覆盖:derive 真值表(每状态入口 + 人工态优先)、sync 幂等、
人工态切换、**全生命周期走查**(导入→登录→失效→重登→转人工→人工救回→清 CK→停用)、
自动化排除 disabled/archived、列表状态筛选、接口
未做(后续阶段):S4 事件发射点补齐、S5 业务事实直发事件、S6 账号页重构。
193 lines
8.5 KiB
Python
193 lines
8.5 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 sync_status
|
|
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)
|
|
account.ck_check_status = "valid" if cookie_check["valid"] else "invalid"
|
|
account.ck_check_result = {
|
|
key: cookie_check.get(key)
|
|
for key in ("fish_ball", "nickname", "level", "message")
|
|
}
|
|
account.ck_checked_at = cookie_check["checked_at"]
|
|
# 执行前的这次检测同样会改变账号状态(正常 ↔ CK 失效)。
|
|
sync_status(worker_db, account, 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()
|