feat(automation): 巡检和重登优先保住高价值账号的 CK

价值按套装和积分现算,不看标签。第 1、2 档 6 小时巡检一次,并先占重登名额。

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
yml2213
2026-10-05 17:51:04 +08:00
co-authored by Cursor
parent 27e9107e38
commit 998998c390
10 changed files with 190 additions and 43 deletions
@@ -20,6 +20,9 @@
| **没查过的** | `overview_queried_at IS NULL` | **优先**(补历史欠账)|
| 查过但过期了 | `overview_queried_at < now - interval_seconds` | 其次(周期性刷新)|
同一档里面,已分配的排在未分配前面,最近登录成功(`cookie_updated_at`)的再靠前。
价值分要先有查询结果才算得出来,所以新登录、已经分给客服的号先补。
用户明确要求这两类分开 —— 否则"周期性刷新老账号"会永远挤掉"从没查过的新账号"。
## 查询记录
@@ -86,9 +89,16 @@ def build_select_query(db: Session, job: AutomationJob, now: datetime):
batch = _positive_int(job.batch_size, 30)
return (
_base_query(db, job, now)
# 没查过的(IS NULL = 1)排最前 —— MySQL 没有 NULLS FIRST,
# 用 `IS NULL` 的布尔值降序等价实现
.order_by(Account.overview_queried_at.is_(None).desc(), Account.id.asc())
# 没查过的排最前。同一档里:已分配的优先,再是最近登录成功的
# (cookie_updated_at 越新越靠前;空的排后面)。
# MySQL 没有 NULLS FIRST,用 IS NULL 的布尔值表达。
.order_by(
Account.overview_queried_at.is_(None).desc(),
Account.assigned_to.isnot(None).desc(),
Account.cookie_updated_at.is_(None).asc(),
Account.cookie_updated_at.desc(),
Account.id.asc(),
)
.limit(batch)
)
@@ -13,11 +13,16 @@ from concurrent.futures import ThreadPoolExecutor, as_completed
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import func, or_
from sqlalchemy import and_, func, or_
from sqlalchemy.orm import Session
from ....models import Account, AutomationJob
from ...account_lifecycle import MANUAL_STATES, on_ck_verdict
from ...account_value import (
HIGH_VALUE_TIER_MAX,
high_value_recheck_seconds,
value_tier_expr,
)
from ...cookie_check_service import check_douyu_cookie
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
@@ -64,13 +69,19 @@ 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。
"""
cutoff = now - timedelta(seconds=max(int(job.interval_seconds or 0), 60))
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()
return (
db.query(Account)
.filter(cookie_operable_filter())
@@ -80,10 +91,12 @@ def _candidates_query(db: Session, job: AutomationJob, now: datetime):
.filter(
or_(
Account.ck_checked_at.is_(None),
Account.ck_checked_at < cutoff,
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(
tier.asc(),
Account.ck_checked_at.is_(None).desc(),
Account.ck_checked_at.asc(),
Account.id.asc(),
@@ -30,6 +30,7 @@ from ...account_lifecycle import (
on_login_success,
sync_status,
)
from ...account_value import value_tier_expr
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
from ...login_service import LoginBatchRunner, get_relogin_limits
@@ -121,7 +122,10 @@ def build_select_query(
)
if system_user_id is not None:
pass # 预留:将来若需要限制到某个归属人,在这里加过滤
# 名额先给第 1、2 档(两套还能换 / 还剩一套 / 积分≥300)。
# 同一档里仍按退避时间,避免把还没到点的高价值号提前拉起来。
return query.order_by(
value_tier_expr().asc(),
Account.relogin_next_at.is_(None).desc(),
Account.relogin_next_at.asc(),
Account.id.asc(),