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,8 @@ import re
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import Account, LoginTask
|
||||
from ..models import Account
|
||||
from .douyu_service import cookie_holding_account_ids_query
|
||||
|
||||
EMAIL_PATTERN = re.compile(r"^[^\s@|]+@[^\s@|]+\.[^\s@|]+$")
|
||||
|
||||
@@ -22,16 +23,12 @@ def split_account_line(line: str) -> list[str]:
|
||||
|
||||
|
||||
def cookie_account_ids_query(db: Session):
|
||||
"""返回有成功登录记录(cookie非空)的账号ID子查询。"""
|
||||
return (
|
||||
db.query(LoginTask.account_id)
|
||||
.filter(
|
||||
LoginTask.status == "success",
|
||||
LoginTask.cookie != "",
|
||||
LoginTask.cookie.isnot(None),
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
"""返回当前仍持有 Cookie 的账号ID子查询。
|
||||
|
||||
以「记录里还留着 CK」为准,而不是以登录批次状态为准:排队中的登录与
|
||||
重登失败都会保留旧 CK,账号不应因此从筛选结果里消失。
|
||||
"""
|
||||
return cookie_holding_account_ids_query(db)
|
||||
|
||||
|
||||
def parse_and_build_accounts(
|
||||
|
||||
@@ -21,7 +21,7 @@ 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 latest_success_login_task, update_account_profile_from_cookie
|
||||
from .douyu_service import latest_cookie_login_task, update_account_profile_from_cookie
|
||||
|
||||
|
||||
class DouyuBatchRunner(
|
||||
@@ -56,10 +56,10 @@ class DouyuBatchRunner(
|
||||
self._push_log(
|
||||
"info", f"[{current}/{total}] 开始: {self._account_name(account)}"
|
||||
)
|
||||
login_task = latest_success_login_task(worker_db, account.id)
|
||||
login_task = latest_cookie_login_task(worker_db, account.id)
|
||||
cookie = login_task.cookie if login_task else ""
|
||||
if not cookie:
|
||||
self._mark_task(worker_db, task, "failed", "账号没有成功登录 Cookie")
|
||||
self._mark_task(worker_db, task, "failed", "账号没有可用登录 Cookie")
|
||||
self._push_log(
|
||||
"warning", f"[{current}] {self._account_name(account)} 无 Cookie"
|
||||
)
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -646,5 +646,30 @@ def cleanup_orphan_relogin_tasks(
|
||||
return len(tasks)
|
||||
|
||||
|
||||
def cleanup_orphan_login_tasks(
|
||||
db: Session,
|
||||
message: str = "登录已中断(服务重启),旧 Cookie 已保留",
|
||||
) -> int:
|
||||
"""服务重启后收敛遗留的普通登录状态。
|
||||
|
||||
普通登录批次停止或进程重启时,未轮到的记录会永远停在 pending/running,
|
||||
页面上一直显示「排队中」,旧 CK 也失去终态说明。统一收敛为 failed 并
|
||||
保留 Cookie,账号仍然可见可重登。
|
||||
"""
|
||||
tasks = (
|
||||
db.query(LoginTask).filter(LoginTask.status.in_(("pending", "running"))).all()
|
||||
)
|
||||
if not tasks:
|
||||
return 0
|
||||
|
||||
finished_at = datetime.now(UTC)
|
||||
for task in tasks:
|
||||
task.status = "failed"
|
||||
task.message = message
|
||||
task.finished_at = finished_at
|
||||
db.commit()
|
||||
return len(tasks)
|
||||
|
||||
|
||||
# 在模块末尾导入 SessionLocal(避免循环导入)
|
||||
from ..database import SessionLocal
|
||||
|
||||
Reference in New Issue
Block a user