Files
live-hub-py/web/backend/services/automation/jobs/ck_health.py
T

304 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""CK 巡检作业(P1a)。
只做一件事:把「持有 CK 但久未检测」的账号挑出来,调现成的
``check_douyu_cookie()`` 检测,把结果写回 ``ck_check_*``。
**本作业不修改 CK 本身**:不写 ``cookie`` / ``status`` / ``message``,也不碰
``attempt_*``。P1a 上线后人工照旧可以点检测和重登,自动重登是 P1b 的事。
"""
from __future__ import annotations
from concurrent.futures import as_completed
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import and_, case, func, or_
from sqlalchemy.orm import Session
from ....models import Account, AutomationJob
from ...account_lifecycle import MANUAL_STATES, WORK_LOGIN_RELOGIN
from ...account_protection_service import load_protection_snapshot
from ...account_value import (
HIGH_VALUE_TIER_MAX,
high_value_recheck_seconds,
value_tier_expr,
)
from ...account_work_queue import enqueue_account_work
from ...cookie_check_service import (
check_douyu_cookie,
)
from ...cookie_check_service import (
classify_douyu_cookie_result as classify_result, # noqa: F401 - 兼容诊断入口
)
from ...douyu_account_probe import (
persist_probe_result,
probe_session,
)
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
from ...task_runtime import TaskLane, submit
JOB_KEY = "ck_health"
FLUSH_EVERY = 25
# 非鉴权类失败(网络超时、响应异常、被判风控)占比超过它就自动暂停作业。
# 这类失败说明「我们查不到」,不是「这个 CK 失效了」;如果不加区分地写
# invalid,斗鱼接口抖动一小时就能把上千个好账号标成失效,客服会误判成
# CK 大面积挂掉。
ERROR_PAUSE_RATIO = 0.3
ERROR_PAUSE_MIN_SCANNED = 20
def _candidates_query(db: Session, job: AutomationJob, now: datetime):
"""本轮「待检测账号」的完整候选集(**不带 LIMIT**,供计数复用)。
口径与人工点检测一致(``cookie_operable_filter``:账号有 CK 且没有在途尝试),
再加三条:距上次检测超过间隔、账号允许自动管理、**未被停用/归档**。
第 1、2 档用 6 小时间隔,其余用作业上配置的间隔。
排序刻意不用 ``NULLS FIRST``:那是 PostgreSQL/SQLite 语法,**MySQL 会报
1064 语法错误**(生产踩过一次)。改用 ``ck_checked_at IS NULL DESC``,
三个方言下语义一致且都是合法 SQL。
"""
interval = max(int(job.interval_seconds or 0), 60)
cutoff = now - timedelta(seconds=interval)
# 第 1、2 档 6 小时查一次(作业间隔更短时跟着更短的走),其余用作业间隔。
# 到点的高价值号先占本轮名额,剩下的再按「最久没查」填。
hv_cutoff = now - timedelta(seconds=high_value_recheck_seconds(interval))
tier = value_tier_expr()
protection = load_protection_snapshot(
db,
job_config=getattr(job, "config", None),
)
protected_ids = protection.monitored_ids
due_filter = or_(
Account.ck_checked_at.is_(None),
and_(tier <= HIGH_VALUE_TIER_MAX, Account.ck_checked_at < hv_cutoff),
and_(tier > HIGH_VALUE_TIER_MAX, Account.ck_checked_at < cutoff),
)
if protected_ids:
protected_due = []
for group_key, group in protection.groups.items():
group_ids = protection.group_account_ids.get(group_key, frozenset())
if not group["enabled"] or not group_ids:
continue
group_cutoff = now - timedelta(
seconds=int(group["probe_interval_minutes"]) * 60
)
protected_due.append(
and_(
Account.id.in_(group_ids),
or_(
Account.ck_checked_at.is_(None),
Account.ck_checked_at < group_cutoff,
),
)
)
due_filter = or_(
*protected_due,
due_filter,
)
query = (
db.query(Account)
.filter(cookie_operable_filter())
.filter(Account.auto_managed.is_(True))
# invalid 已进入自动重登队列,manual 已停止自动处理;普通 CK 探针
# 再扫它们只会重复消耗官方请求,并掩盖真正需要发现的新失效。
.filter(Account.ck_check_status.notin_(("invalid", "manual")))
# 生命周期状态机:停用/归档的账号不参与任何自动化(见 account_lifecycle)。
.filter(Account.status.notin_(MANUAL_STATES))
.filter(due_filter)
)
if protected_ids:
query = query.order_by(
case((Account.id.in_(protected_ids), 0), else_=1),
tier.asc(),
Account.ck_checked_at.is_(None).desc(),
Account.ck_checked_at.asc(),
Account.id.asc(),
)
else:
query = query.order_by(
tier.asc(),
Account.ck_checked_at.is_(None).desc(),
Account.ck_checked_at.asc(),
Account.id.asc(),
)
return query
def build_select_query(db: Session, job: AutomationJob, now: datetime):
"""构造本轮要检测**账号**的查询(与取行分开,便于按方言编译做回归)。
解耦后**按账号选行**(2026-10-04):CK 与 ``ck_check_*`` 都在 ``accounts`` 上,
以前是 ``db.query(LoginTask).join(Account)`` —— 那样「没有登录历史的账号」
永远进不了巡检队列,而且一个账号有多行时会被重复检测。
"""
return _candidates_query(db, job, now).limit(max(int(job.batch_size or 0), 1))
def _select_accounts(db: Session, job: AutomationJob, now: datetime) -> list[Account]:
return build_select_query(db, job, now).all()
def count_remaining(db: Session, job: AutomationJob, now: datetime) -> int:
"""剩余待检账号数(给页面提示用)。
刻意**不用** ``build_select_query`` —— 那个带 ``LIMIT batch_size``,
取 count 只会得到批次上限,完全看不出积压。
"""
return int(
_candidates_query(db, job, now).order_by(None).with_entities(Account.id).count()
or 0
)
def _detect(account: Account) -> dict[str, Any]:
try:
return probe_session(
account.cookie or "",
checker=check_douyu_cookie,
)
except Exception as exc: # noqa: BLE001 单个账号失败不影响整轮
return {
"status": "error",
"message": f"检测异常: {exc}",
"checked_at": datetime.now(UTC).isoformat(),
"detail": {},
}
def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
now = datetime.now(UTC)
protection = load_protection_snapshot(
db,
job_config=getattr(job, "config", None),
)
monitored_ids = protection.monitored_ids
relogin_ids = protection.relogin_ids
accounts = _select_accounts(db, job, now)
if not accounts:
earliest_checked = (
db.query(func.min(Account.ck_checked_at))
.filter(cookie_holding_filter())
.scalar()
)
msg = "全库暂无到期待检账号"
if earliest_checked:
if earliest_checked.tzinfo is None:
earliest_checked = earliest_checked.replace(tzinfo=UTC)
hours_ago = (now - earliest_checked).total_seconds() / 3600
recheck_hours = max(int(job.interval_seconds or 0), 60) / 3600
msg = f"全库暂无到期待检账号(最久前检测于 {hours_ago:.1f} 小时前,重检间隔为 {recheck_hours:.1f} 小时)"
return {"scanned": 0, "valid": 0, "invalid": 0, "skipped": 0, "message": msg}
results: dict[int, dict[str, Any]] = {}
concurrency = max(min(int(job.concurrency or 1), 16), 1)
futures = {}
for account in accounts:
future = submit(TaskLane.ACCOUNT_CHECK, _detect, account)
if future is not None:
futures[future] = account
for future in as_completed(futures):
account = futures[future]
results[account.id] = future.result()
# 只写检测结果三件套;cookie / status / attempt_* 一律不动。
valid = invalid = errors = protected_scanned = protected_invalid = 0
for index, account in enumerate(accounts, start=1):
result = results.get(account.id)
if result is None:
continue
verdict = str(result.get("status") or "error")
is_protected = int(account.id) in monitored_ids
if is_protected:
protected_scanned += 1
if verdict == "valid":
valid += 1
elif verdict == "invalid":
invalid += 1
if is_protected:
protected_invalid += 1
else:
errors += 1
# 自动巡检不额外刷 account_probe 事件;统一落库仍会处理 CK 状态变化
# 和生命周期事件,避免每 6 小时给所有账号写一条重复查询事件。
detail = result.get("detail") if isinstance(result.get("detail"), dict) else {}
persist_probe_result(
db,
account,
{
"probe_id": f"ck-health:{now.timestamp()}:{account.id}",
"identity": {
"uid": account.uid,
"username": account.username,
"nickname": detail.get("nickname") or "",
"nickname_source": "session",
},
"session": result,
"elite": {},
"eligibility": {},
"roles": {},
"gold": {},
"goods": {},
"errors": {},
},
source="automation.ck_health",
record_event=False,
commit=False,
)
if verdict == "invalid" and int(account.id) in relogin_ids:
# 保护组失效账号优先进入重登队列。on_ck_verdict 会登记默认
# priority=30,这里把已存在的同类型待办降到 10,不重复建行。
enqueue_account_work(
db,
account_id=int(account.id),
work_type=WORK_LOGIN_RELOGIN,
priority=10,
max_attempts=20,
due_at=now,
reason="履约保护账号 CK 失效,优先自动重登",
)
if index % FLUSH_EVERY == 0:
db.commit()
db.commit()
# 剩余待检数量只用于提示,不参与调度。
remaining = count_remaining(db, job, now)
error_ratio = errors / len(accounts) if accounts else 0.0
paused = False
if errors >= ERROR_PAUSE_MIN_SCANNED and error_ratio > ERROR_PAUSE_RATIO:
# 大面积「查不到」通常是斗鱼接口抖动或被风控,继续跑只会污染更多账号。
job.enabled = False
job.updated_at = datetime.now(UTC)
paused = True
message = f"本轮检测 {len(accounts)} 条,剩余待检 {remaining} 条"
if errors:
message += f";非鉴权类失败 {errors} 条({error_ratio:.0%})"
if protected_scanned:
message += (
f";履约保护账号 {protected_scanned} 条"
f",失效 {protected_invalid} 条"
)
if paused:
message += ";失败占比过高,已自动暂停作业"
return {
"scanned": len(accounts),
"valid": valid,
"invalid": invalid,
"skipped": errors,
"message": message,
"detail": {
"remaining": remaining,
"concurrency": concurrency,
"errors": errors,
"error_ratio": round(error_ratio, 4),
"paused": paused,
"protected_scanned": protected_scanned,
"protected_invalid": protected_invalid,
},
}