fix(douyu): 补全账号能力目录并回收失效租约
This commit is contained in:
@@ -9,7 +9,7 @@ from __future__ import annotations
|
||||
from dataclasses import dataclass
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from sqlalchemy import case, or_, select
|
||||
from sqlalchemy import and_, case, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
@@ -18,6 +18,7 @@ from ...models import (
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
AccountSyncState,
|
||||
)
|
||||
from ..account_capability_service import OFFER_STATE_ACTIVE
|
||||
from ..account_events_service import (
|
||||
@@ -36,6 +37,8 @@ from ..account_query_filters import (
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
from ..account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ..account_work_queue import active_account_work_exists
|
||||
|
||||
VALIDATION_LEASE_MINUTES = 5
|
||||
BINDING_LEASE_MINUTES = 120
|
||||
@@ -343,6 +346,96 @@ def capability_candidate_accounts(
|
||||
]
|
||||
|
||||
|
||||
def capability_refresh_candidate_accounts(
|
||||
db: Session,
|
||||
*,
|
||||
commodity_ids: list[str] | tuple[str, ...],
|
||||
scope: str = "elite",
|
||||
limit: int = 20,
|
||||
now: datetime | None = None,
|
||||
) -> list[Account]:
|
||||
"""为待履约商品补充候选账号。
|
||||
|
||||
2026-11-22:原刷新作业只扫描已有商品状态行的账号,新账号永远不会被
|
||||
发现;后续又错误地只扫描“此刻可分配”的账号。能力目录必须覆盖全部有效
|
||||
CK,租约、协作占用和换绑冷却只影响“选号”,不能影响“补目录”。
|
||||
"""
|
||||
commodities = tuple(dict.fromkeys(str(item).strip() for item in commodity_ids))
|
||||
commodities = tuple(item for item in commodities if item)
|
||||
if not commodities:
|
||||
return []
|
||||
moment = _now(now)
|
||||
fresh_storehome = (
|
||||
select(AccountSyncState.id)
|
||||
.where(
|
||||
AccountSyncState.account_id == Account.id,
|
||||
AccountSyncState.platform == "douyu",
|
||||
AccountSyncState.domain == "storehome",
|
||||
AccountSyncState.status == "success",
|
||||
AccountSyncState.valid_until.isnot(None),
|
||||
AccountSyncState.valid_until > moment,
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
refresh_needed = []
|
||||
for commodity_id in commodities:
|
||||
state_exists = (
|
||||
select(AccountCommodityState.id)
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
state_due = (
|
||||
select(AccountCommodityState.id)
|
||||
.where(
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
or_(
|
||||
AccountCommodityState.valid_until.is_(None),
|
||||
AccountCommodityState.valid_until <= moment,
|
||||
AccountCommodityState.next_check_at.is_(None),
|
||||
AccountCommodityState.next_check_at <= moment,
|
||||
),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
refresh_needed.append(
|
||||
or_(
|
||||
and_(~fresh_storehome, ~state_exists),
|
||||
state_due,
|
||||
)
|
||||
)
|
||||
|
||||
return (
|
||||
db.query(Account)
|
||||
.filter(
|
||||
or_(*refresh_needed),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
|
||||
)
|
||||
.order_by(
|
||||
case((active_lease_exists(), 1), else_=0),
|
||||
case((active_collab_order_exists(), 1), else_=0),
|
||||
case((role_change_ready_filter(moment), 0), else_=1),
|
||||
case((Account.role_bind_status == "unbound", 0), else_=1),
|
||||
Account.overview_queried_at.is_(None).desc(),
|
||||
Account.overview_queried_at.asc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
.limit(max(1, min(int(limit), 100)))
|
||||
.all()
|
||||
)
|
||||
|
||||
|
||||
def has_recent_exchange(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -590,6 +683,7 @@ __all__ = [
|
||||
"acquire_account_lease",
|
||||
"active_lease_for_account",
|
||||
"capability_candidate_accounts",
|
||||
"capability_refresh_candidate_accounts",
|
||||
"eligible_accounts",
|
||||
"expire_platform_fulfillment_leases",
|
||||
"has_recent_exchange",
|
||||
|
||||
Reference in New Issue
Block a user