fix(douyu): 账号只要还持有 CK 就不再从工作台消失
线上事故:分配给客服的账号(如「远行者103」)在斗鱼工作台与 CK 列表里 查不到,只有账号管理里还能搜到。原因是可见性判定写成了 LoginTask.status == "success",而按设计保留旧 CK 的三种状态都不再是 success: - 普通登录批次启动时会把复用的成功记录置为 pending/running(旧 CK 保留), 批次被停止或失败后一直停在 pending,账号永久消失; - CK 重登失败置为 relogin_failed(旧 CK 保留),账号从工作台消失; - 普通登录失败置为 failed(复用记录的旧 CK 未清空),同样消失。 线上现状:12545 个账号持有 CK,但只有 11703 个能出现在工作台,842 个 (644 重登失败 + 176 排队中 + …)对客服和管理员都不可见。 改动: - douyu_service 新增 cookie_holding_filter()/latest_cookie_login_task(), 以「记录里还留着 Cookie」作为唯一判据,并抽出 LOGIN_IN_FLIGHT_STATUSES 与 cookie_operable_filter() 供检测/重登入口复用; - 斗鱼工作台账号列表、任务账号校验、账号管理 has_cookie 筛选、分配校验与 概览、CK 管理/操作列表、首页 CK 统计统一改用该判据; - 工作台取 CK 从 latest_success_login_task 换成 latest_cookie_login_task, 保证重登失败/排队中的账号仍能被任务正常执行; - 新增 cleanup_orphan_login_tasks():进程重启后把遗留的 pending/running 收敛为 failed 并保留旧 CK,避免页面永久显示排队中。 新增回归测试覆盖上述状态下的可见性、可执行性与启动清理。
This commit is contained in:
@@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import and_, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from core.douyu.activity_client import DouyuActivityClient
|
||||
@@ -181,44 +181,66 @@ def ensure_douyu_config(db: Session) -> DouyuConfig:
|
||||
return config
|
||||
|
||||
|
||||
def latest_success_login_task(db: Session, account_id: int) -> LoginTask | None:
|
||||
"""读取账号最近一次成功登录记录。"""
|
||||
# 登录/重登批次正在处理的状态:这些记录已被批次接管,不允许并发操作。
|
||||
LOGIN_IN_FLIGHT_STATUSES = ("pending", "running", "relogin_pending", "relogin_running")
|
||||
|
||||
|
||||
def cookie_holding_filter():
|
||||
"""判定登录记录当前是否仍持有 Cookie。
|
||||
|
||||
账号是否有 CK 只取决于记录里是否还留着 Cookie,而不是取决于批次状态:
|
||||
普通登录在排队时会把复用的成功记录临时置为 ``pending``/``running``,
|
||||
CK 重登失败会置为 ``relogin_failed``,这些状态都按设计保留旧 Cookie
|
||||
(见 ``login_service`` 的「旧 Cookie 已保留」分支),账号必须继续可见。
|
||||
只有 Cookie 真正被清空(账号检测成功)时才不再算持有 CK。
|
||||
"""
|
||||
return and_(
|
||||
LoginTask.cookie.isnot(None),
|
||||
LoginTask.cookie != "",
|
||||
)
|
||||
|
||||
|
||||
def latest_cookie_login_task(db: Session, account_id: int) -> LoginTask | None:
|
||||
"""读取账号最近一条仍持有 Cookie 的登录记录。"""
|
||||
return (
|
||||
db.query(LoginTask)
|
||||
.filter(
|
||||
LoginTask.account_id == account_id,
|
||||
LoginTask.status == "success",
|
||||
LoginTask.cookie != "",
|
||||
cookie_holding_filter(),
|
||||
)
|
||||
.order_by(LoginTask.id.desc())
|
||||
.first()
|
||||
)
|
||||
|
||||
|
||||
def latest_success_cookie(db: Session, account_id: int) -> str:
|
||||
"""读取账号最近一次成功登录 Cookie。"""
|
||||
task = latest_success_login_task(db, account_id)
|
||||
def cookie_operable_filter():
|
||||
"""判定登录记录能否基于现有 Cookie 再做检测或重登。
|
||||
|
||||
正在被登录/重登批次处理的行(pending/running/relogin_*)已由批次接管,
|
||||
并发再点检测或重登只会互相覆盖状态,因此排除在外。
|
||||
"""
|
||||
return and_(
|
||||
cookie_holding_filter(),
|
||||
LoginTask.status.notin_(LOGIN_IN_FLIGHT_STATUSES),
|
||||
)
|
||||
|
||||
|
||||
def latest_cookie(db: Session, account_id: int) -> str:
|
||||
"""读取账号当前 Cookie。"""
|
||||
task = latest_cookie_login_task(db, account_id)
|
||||
return task.cookie if task else ""
|
||||
|
||||
|
||||
def cookie_account_ids_query(db: Session):
|
||||
"""返回拥有成功 Cookie 的斗鱼账号 ID 查询。"""
|
||||
return (
|
||||
db.query(LoginTask.account_id)
|
||||
.filter(LoginTask.status == "success", LoginTask.cookie != "")
|
||||
.distinct()
|
||||
)
|
||||
def cookie_holding_account_ids_query(db: Session):
|
||||
"""返回当前仍持有 Cookie 的斗鱼账号 ID 查询。"""
|
||||
return db.query(LoginTask.account_id).filter(cookie_holding_filter()).distinct()
|
||||
|
||||
|
||||
def visible_douyu_task_accounts(db: Session, account_ids: list[int]) -> list[Account]:
|
||||
"""只保留存在成功 Cookie 的斗鱼账号。"""
|
||||
"""只保留当前仍持有 Cookie 的斗鱼账号。"""
|
||||
if not account_ids:
|
||||
return []
|
||||
cookie_ids = select(LoginTask.account_id).where(
|
||||
LoginTask.status == "success",
|
||||
LoginTask.cookie != "",
|
||||
LoginTask.cookie.isnot(None),
|
||||
)
|
||||
cookie_ids = select(LoginTask.account_id).where(cookie_holding_filter())
|
||||
return (
|
||||
db.query(Account)
|
||||
.filter(Account.id.in_(account_ids), Account.id.in_(cookie_ids))
|
||||
|
||||
Reference in New Issue
Block a user