perf(platform-fulfillment): 优化本地选号与失败任务调度

This commit is contained in:
yml
2026-11-22 19:15:44 +08:00
parent 6a0e88055d
commit e652d30283
10 changed files with 701 additions and 131 deletions
@@ -63,6 +63,14 @@ class AccountCandidate:
return self.nickname or self.username or f"#{self.account_id}"
@dataclass(frozen=True)
class CapabilityCandidate:
"""一个新鲜能力快照及其对应账号,供本地零外部请求选号。"""
account: Account
state: AccountCommodityState
def _now(now: datetime | None = None) -> datetime:
return now or datetime.now(UTC)
@@ -269,7 +277,7 @@ def live_candidate_accounts(
return rows
def capability_candidate_accounts(
def _capability_candidate_query(
db: Session,
*,
commodity_id: str,
@@ -278,11 +286,10 @@ def capability_candidate_accounts(
limit: int = 20,
now: datetime | None = None,
allow_not_open: bool = False,
) -> list[Account]:
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
):
commodity = commodity_id.strip()
if not commodity:
return []
return None
moment = _now(now)
since = moment - timedelta(days=EXCHANGE_COOLDOWN_DAYS)
owner_ids = _pool_owner_ids()
@@ -330,7 +337,7 @@ def capability_candidate_accounts(
),
)
return (
db.query(Account)
db.query(Account, AccountCommodityState)
.join(
AccountCommodityState,
AccountCommodityState.account_id == Account.id,
@@ -349,6 +356,7 @@ def capability_candidate_accounts(
),
Account.cookie.isnot(None),
Account.cookie != "",
Account.ck_check_status == "valid",
Account.auto_managed.is_(True),
Account.status.notin_(MANUAL_STATES),
or_(
@@ -376,10 +384,62 @@ def capability_candidate_accounts(
Account.id.asc(),
)
.limit(max(1, min(int(limit), 100)))
.all()
)
def capability_candidate_rows(
db: Session,
*,
commodity_id: str,
required_points: int = 0,
scope: str = "elite",
limit: int = 20,
now: datetime | None = None,
allow_not_open: bool = False,
) -> list[CapabilityCandidate]:
"""读取账号与能力快照;只访问本地数据库,不触发官网请求。"""
query = _capability_candidate_query(
db,
commodity_id=commodity_id,
required_points=required_points,
scope=scope,
limit=limit,
now=now,
allow_not_open=allow_not_open,
)
if query is None:
return []
return [
CapabilityCandidate(account=account, state=state)
for account, state in query.all()
]
def capability_candidate_accounts(
db: Session,
*,
commodity_id: str,
required_points: int = 0,
scope: str = "elite",
limit: int = 20,
now: datetime | None = None,
allow_not_open: bool = False,
) -> list[Account]:
"""从新鲜的商品能力索引读取候选账号,不产生外部请求。"""
return [
candidate.account
for candidate in capability_candidate_rows(
db,
commodity_id=commodity_id,
required_points=required_points,
scope=scope,
limit=limit,
now=now,
allow_not_open=allow_not_open,
)
]
def has_recent_exchange(
db: Session,
*,
@@ -411,6 +471,7 @@ def lease_account(
commodity_id: str,
ttl_minutes: int | None = None,
now: datetime | None = None,
commit: bool = True,
) -> AccountLease | None:
"""尝试领取一个账号;撞唯一索引时返回 None。"""
return acquire_account_lease(
@@ -423,6 +484,7 @@ def lease_account(
batch_id=f"pf:{task_id}",
ttl_minutes=ttl_minutes,
now=now,
commit=commit,
)