feat(account-check): 增加履约账号 CK 保护目标

This commit is contained in:
yml
2026-11-22 09:57:03 +08:00
parent 492288e84f
commit f2041f2444
10 changed files with 1151 additions and 23 deletions
@@ -13,16 +13,18 @@ from concurrent.futures import as_completed
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import and_, func, or_
from sqlalchemy import and_, case, func, or_
from sqlalchemy.orm import Session
from ....models import Account, AutomationJob
from ...account_lifecycle import MANUAL_STATES
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,
)
@@ -64,7 +66,40 @@ def _candidates_query(db: Session, job: AutomationJob, now: datetime):
# 到点的高价值号先占本轮名额,剩下的再按「最久没查」填。
hv_cutoff = now - timedelta(seconds=high_value_recheck_seconds(interval))
tier = value_tier_expr()
return (
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))
@@ -73,20 +108,24 @@ def _candidates_query(db: Session, job: AutomationJob, now: datetime):
.filter(Account.ck_check_status.notin_(("invalid", "manual")))
# 生命周期状态机:停用/归档的账号不参与任何自动化(见 account_lifecycle)。
.filter(Account.status.notin_(MANUAL_STATES))
.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),
)
)
.order_by(
.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):
@@ -132,6 +171,12 @@ def _detect(account: Account) -> dict[str, Any]:
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 = (
@@ -160,16 +205,21 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
results[account.id] = future.result()
# 只写检测结果三件套;cookie / status / attempt_* 一律不动。
valid = invalid = errors = 0
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 状态变化
@@ -198,6 +248,18 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
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()
@@ -216,6 +278,11 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
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 {
@@ -230,5 +297,7 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
"errors": errors,
"error_ratio": round(error_ratio, 4),
"paused": paused,
"protected_scanned": protected_scanned,
"protected_invalid": protected_invalid,
},
}
@@ -19,7 +19,7 @@ from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import func, or_
from sqlalchemy import case, func, or_
from sqlalchemy.orm import Session
from ....database import WorkerSessionLocal
@@ -40,6 +40,10 @@ from ...account_lifecycle import (
on_login_success,
sync_status,
)
from ...account_protection_service import (
ProtectionSnapshot,
load_protection_snapshot,
)
from ...account_value import value_tier_expr
from ...account_work_queue import (
claim_account_work,
@@ -102,6 +106,7 @@ def build_select_query(
max_attempts: int = 2,
system_user_id: int | None = None,
batch_limit: int | None = None,
protection: ProtectionSnapshot | None = None,
):
"""构造本轮重登候选的查询(与取行分开,便于按方言编译做回归)。
@@ -134,17 +139,33 @@ def build_select_query(
tuple(sorted(MANUAL_KINDS))
)
)
snapshot = protection or load_protection_snapshot(db)
suppressed_ids = snapshot.suppressed_relogin_ids
if suppressed_ids:
# 保护组可以分别关闭自动重登。账号同时属于任一已开启自动重登的组时,
# 仍按允许处理;只有完全被抑制的账号才排除。
query = query.filter(~Account.id.in_(suppressed_ids))
relogin_ids = snapshot.relogin_ids
if system_user_id is not None:
pass # 预留:将来若需要限制到某个归属人,在这里加过滤
# 名额先给第 1、2 档(两套还能换 / 还剩一套 / 积分≥300)。
# 同一档里仍按退避时间,避免把还没到点的高价值号提前拉起来。
limit = int(batch_limit if batch_limit is not None else job.batch_size or 0)
return query.order_by(
value_tier_expr().asc(),
ordering = [
Account.relogin_next_at.is_(None).desc(),
Account.relogin_next_at.asc(),
Account.id.asc(),
).limit(max(limit, 1))
]
if relogin_ids:
ordering.insert(
0,
case((Account.id.in_(relogin_ids), 0), else_=1),
)
ordering.insert(
1 if relogin_ids else 0,
value_tier_expr().asc(),
)
return query.order_by(*ordering).limit(max(limit, 1))
def _load_batch_state(
@@ -208,7 +229,8 @@ def count_pending_relogin(db: Session, *, max_attempts: int = 2) -> int:
count 只会得到批次上限(页面上显示恒等于 10),完全看不出积压。这里也**不
按退避窗口过滤** —— 退避是「什么时候试」的细节,不影响「还有多少要处理」。
"""
return int(
snapshot = load_protection_snapshot(db)
query = (
db.query(func.count(Account.id))
.filter(Account.ck_check_status == "invalid")
.filter(cookie_holding_filter())
@@ -221,9 +243,10 @@ def count_pending_relogin(db: Session, *, max_attempts: int = 2) -> int:
tuple(sorted(MANUAL_KINDS))
)
)
.scalar()
or 0
)
if snapshot.suppressed_relogin_ids:
query = query.filter(~Account.id.in_(snapshot.suppressed_relogin_ids))
return int(query.scalar() or 0)
def _system_user_id(db: Session) -> int | None:
@@ -453,12 +476,14 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
now=now,
pending=pending,
)
protection = load_protection_snapshot(db)
accounts = build_select_query(
db,
job,
now,
max_attempts=max_attempts,
batch_limit=adaptive.batch_size,
protection=protection,
).all()
if not accounts:
db.commit()
@@ -466,11 +491,12 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
selected_account_ids = tuple(int(account.id) for account in accounts)
for account in accounts:
priority = 10 if int(account.id) in protection.relogin_ids else 30
enqueue_account_work(
db,
account_id=int(account.id),
work_type=WORK_LOGIN_RELOGIN,
priority=30,
priority=priority,
max_attempts=20,
due_at=now,
reason="CK 失效,等待自动重登",