feat(douyu): 接入重登工作队列与平台状态观测

This commit is contained in:
yml
2026-11-21 21:58:31 +08:00
parent e178a393e4
commit 3b2d4f89fc
8 changed files with 456 additions and 5 deletions
@@ -27,10 +27,17 @@ from ...account_events_service import EVENT_RELOGIN_FAILED, emit
from ...account_lifecycle import (
MANUAL_KINDS,
MANUAL_STATES,
WORK_LOGIN_RELOGIN,
on_login_success,
sync_status,
)
from ...account_value import value_tier_expr
from ...account_work_queue import (
claim_account_work,
enqueue_account_work,
fail_account_work_type,
reschedule_account_work_type,
)
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
from ...login_failure import classify_failure
from ...login_service import LoginBatchRunner, get_relogin_limits
@@ -235,6 +242,47 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
if not accounts:
return {"scanned": 0, "valid": 0, "invalid": 0, "skipped": 0}
selected_account_ids = tuple(int(account.id) for account in accounts)
for account in accounts:
enqueue_account_work(
db,
account_id=int(account.id),
work_type=WORK_LOGIN_RELOGIN,
priority=30,
max_attempts=20,
due_at=now,
reason="CK 失效,等待自动重登",
)
db.commit()
work_items = claim_account_work(
db,
work_types=(WORK_LOGIN_RELOGIN,),
account_ids=selected_account_ids,
limit=len(selected_account_ids),
now=now,
)
db.commit()
claimed_account_ids = [int(item.account_id) for item in work_items]
if not claimed_account_ids:
return {
"scanned": 0,
"valid": 0,
"invalid": 0,
"skipped": len(selected_account_ids),
"message": "候选账号的重登工作项已被其他执行器领取",
}
account_map = {
int(account.id): account
for account in db.query(Account)
.filter(Account.id.in_(claimed_account_ids))
.all()
}
accounts = [
account_map[account_id]
for account_id in claimed_account_ids
if account_id in account_map
]
# 调度单位是**账号**(CK 在账号上);登录记录只是这次尝试的历史。
account_ids = [account.id for account in accounts]
proxy = db.query(ProxyConfigModel).first()
@@ -325,6 +373,13 @@ def settle_results(
for account_id in account_ids:
entry = fresh.get(account_id)
if entry is None:
reschedule_account_work_type(
db,
account_id=int(account_id),
work_type=WORK_LOGIN_RELOGIN,
due_at=now + timedelta(hours=1),
reason="自动重登未产生账号任务,重新排队",
)
continue
account, task = entry
if task.attempt_status == "success":
@@ -359,11 +414,25 @@ def settle_results(
)
# 状态跟着落到 inactive 的分支(invalid 或 manual)——推导函数负责判定
sync_status(db, account, occurred_at=now)
fail_account_work_type(
db,
account_id=int(account.id),
work_type=WORK_LOGIN_RELOGIN,
reason=f"自动重登转人工({kind})",
now=now,
)
continue
if kind == "email_quota":
counters["email_quota"] += 1
counters["retry"] += 1
account.relogin_next_at = _next_retry_at(kind, account.relogin_attempts, now)
reschedule_account_work_type(
db,
account_id=int(account.id),
work_type=WORK_LOGIN_RELOGIN,
due_at=account.relogin_next_at,
reason=f"自动重登未成功({kind}),按账号退避时间重试",
)
# 排了下一次重试 → 仍是 invalid(不是 manual)
sync_status(db, account, occurred_at=now)
return counters