fix(fulfillment): 修正自动选号候选池

This commit is contained in:
yml
2026-11-18 12:16:20 +08:00
parent 27da489b79
commit 79da819384
3 changed files with 106 additions and 19 deletions
@@ -9,7 +9,7 @@ from __future__ import annotations
from dataclasses import dataclass
from datetime import UTC, datetime, timedelta
from sqlalchemy import or_, select
from sqlalchemy import case, or_, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
@@ -19,7 +19,7 @@ from ..account_lifecycle import MANUAL_STATES
VALIDATION_LEASE_MINUTES = 5
BINDING_LEASE_MINUTES = 120
EXCHANGE_COOLDOWN_DAYS = 365
POOL_OWNER_ROLES = ("super_admin", "operation")
POOL_OWNER_ROLES = ("super_admin", "operation", "support")
@dataclass(frozen=True)
@@ -41,6 +41,13 @@ def _now(now: datetime | None = None) -> datetime:
return now or datetime.now(UTC)
def _pool_owner_ids():
return select(User.id).where(
User.role.in_(POOL_OWNER_ROLES),
User.is_active.is_(True),
)
def eligible_accounts(
db: Session,
*,
@@ -52,7 +59,7 @@ def eligible_accounts(
"""筛选能人工兑换指定精英宝典商品的账号。"""
moment = _now(now)
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
owner_ids = select(User.id).where(User.role.in_(POOL_OWNER_ROLES))
owner_ids = _pool_owner_ids()
active_lease = (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
@@ -114,28 +121,49 @@ def live_candidate_accounts(
db: Session,
*,
limit: int = 20,
required_points: int = 0,
commodity_id: str = "",
) -> list[Account]:
"""选出值得实时检测的账号,不读取 CK/积分/开通等缓存结论。"""
owner_ids = select(User.id).where(User.role.in_(POOL_OWNER_ROLES))
"""按缓存质量优先排序,但保留实时检测作为最终准入判断。"""
owner_ids = _pool_owner_ids()
active_lease = (
select(AccountLease.id)
.where(AccountLease.active_account_id == Account.id)
.exists()
)
return (
db.query(Account)
.filter(
Account.cookie.isnot(None),
Account.cookie != "",
Account.auto_managed.is_(True),
Account.status.notin_(MANUAL_STATES),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
~active_lease,
query = db.query(Account).filter(
Account.cookie.isnot(None),
Account.cookie != "",
Account.auto_managed.is_(True),
Account.status.notin_(MANUAL_STATES),
or_(Account.assigned_to.is_(None), Account.assigned_to.in_(owner_ids)),
~active_lease,
)
commodity = commodity_id.strip()
if commodity:
since = _now() - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
exchanged = (
select(AccountEvent.id)
.where(
AccountEvent.account_id == Account.id,
AccountEvent.event_type == "exchange",
AccountEvent.subject_key == commodity,
AccountEvent.occurred_at >= since,
)
.exists()
)
.order_by(
query = query.filter(~exchanged)
return (
query.order_by(
case((Account.ck_check_status == "valid", 0), else_=1),
case((Account.status == "active", 0), else_=1),
case((Account.elite_opened.is_(True), 0), else_=1),
case(
(Account.points >= max(0, int(required_points)), 0),
else_=1,
),
Account.ck_checked_at.is_(None).desc(),
Account.ck_checked_at.asc(),
Account.points.is_(None).desc(),
Account.points.asc(),
Account.id.asc(),
)
@@ -56,6 +56,7 @@ ACTIVE_STATUSES = {
BINDING_QR_TTL_SECONDS = int(os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "600"))
AUTO_SYNC_INTERVAL_SECONDS = 900.0
DEFAULT_LIVE_CANDIDATE_LIMIT = 20
_SYNC_LOCK = Lock()
_AUTO_SYNC_LOCK = Lock()
@@ -888,9 +889,23 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
manual_id = str(douyu_config_value("manual_id", config.manual_id))
rid = str(douyu_config_value("rid", config.rid))
act_alias = str(douyu_config_value("bind_act_alias", config.bind_act_alias))
# 单次最多实时检测 5 个,避免一次选号请求被外部接口拖到超时。
# 检测时间会写回 ck_checked_at,下一次点击自动轮到下一批。
candidates = live_candidate_accounts(db, limit=5)
try:
candidate_limit = int(
os.getenv(
"PLATFORM_FULFILLMENT_LIVE_CANDIDATE_LIMIT",
str(DEFAULT_LIVE_CANDIDATE_LIMIT),
)
)
except ValueError:
candidate_limit = DEFAULT_LIVE_CANDIDATE_LIMIT
candidate_limit = max(1, min(candidate_limit, 100))
# 缓存用于优先排序,最终是否可用仍由下面的逐个实时检测决定。
candidates = live_candidate_accounts(
db,
limit=candidate_limit,
required_points=required_points,
commodity_id=task.commodity_id,
)
diagnostics: list[dict[str, Any]] = []
qualified: list[tuple[Account, DouyuLiveAccountState]] = []
selected: AccountCandidate | None = None