perf(platform-fulfillment): 优化本地选号与失败任务调度
This commit is contained in:
@@ -25,6 +25,10 @@ AVAILABILITY_EXPIRED = "expired"
|
||||
AVAILABILITY_UNKNOWN = "unknown"
|
||||
|
||||
DEFAULT_CAPABILITY_TTL_SECONDS = 15 * 60
|
||||
DEFAULT_STABLE_CAPABILITY_TTL_SECONDS = 6 * 60 * 60
|
||||
DEFAULT_LIMITED_CAPABILITY_TTL_SECONDS = 24 * 60 * 60
|
||||
DEFAULT_NO_STOCK_CAPABILITY_TTL_SECONDS = 15 * 60
|
||||
DEFAULT_UNKNOWN_CAPABILITY_TTL_SECONDS = 5 * 60
|
||||
|
||||
|
||||
def _env_positive_int(name: str, default: int) -> int:
|
||||
@@ -35,11 +39,39 @@ def _env_positive_int(name: str, default: int) -> int:
|
||||
return value if value > 0 else default
|
||||
|
||||
|
||||
def capability_ttl_seconds() -> int:
|
||||
return _env_positive_int(
|
||||
def capability_ttl_seconds(availability: str | None = None) -> int:
|
||||
"""按能力稳定性返回快照 TTL。
|
||||
|
||||
30k 级账号池无法在 15 分钟内全量刷新一遍。把稳定的“可兑换/未开放”
|
||||
仍按 15 分钟处理,会让本地能力池长期处于过期状态,选号又退化到逐个
|
||||
实时扫描。库存和未知状态变化快,因此保持短 TTL;账号是否具备活动资格、
|
||||
是否达到限兑上限则使用长 TTL,兑换前仍会再做实时复核。
|
||||
"""
|
||||
generic = _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_STATE_TTL_SECONDS",
|
||||
DEFAULT_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability in {AVAILABILITY_AVAILABLE, AVAILABILITY_NOT_OPEN}:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_STABLE_TTL_SECONDS",
|
||||
DEFAULT_STABLE_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability == AVAILABILITY_LIMITED:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_LIMITED_TTL_SECONDS",
|
||||
DEFAULT_LIMITED_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability == AVAILABILITY_NO_STOCK:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_NO_STOCK_TTL_SECONDS",
|
||||
DEFAULT_NO_STOCK_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
if availability == AVAILABILITY_UNKNOWN:
|
||||
return _env_positive_int(
|
||||
"ACCOUNT_COMMODITY_UNKNOWN_TTL_SECONDS",
|
||||
DEFAULT_UNKNOWN_CAPABILITY_TTL_SECONDS,
|
||||
)
|
||||
return generic
|
||||
|
||||
|
||||
def _to_int(value: Any) -> int | None:
|
||||
@@ -310,8 +342,6 @@ def sync_account_goods_states(
|
||||
if not goods_items or account.id is None:
|
||||
return 0
|
||||
moment = observed_at or datetime.now(UTC)
|
||||
ttl_valid_until = moment + timedelta(seconds=capability_ttl_seconds())
|
||||
|
||||
existing_rows = (
|
||||
db.query(AccountCommodityState)
|
||||
.filter(
|
||||
@@ -347,6 +377,9 @@ def sync_account_goods_states(
|
||||
|
||||
availability, reason, raw_status = classify_goods_item(item, now=moment)
|
||||
window = goods_time_window(item, now=moment)
|
||||
ttl_valid_until = moment + timedelta(
|
||||
seconds=capability_ttl_seconds(availability)
|
||||
)
|
||||
last_exchange_at, exchange_count = exchange_stats.get(
|
||||
commodity_id,
|
||||
(None, 0),
|
||||
@@ -392,7 +425,9 @@ def sync_account_goods_states(
|
||||
state.schedule_json = None
|
||||
state.observed_at = moment
|
||||
state.valid_until = None
|
||||
state.next_check_at = ttl_valid_until
|
||||
state.next_check_at = moment + timedelta(
|
||||
seconds=capability_ttl_seconds(AVAILABILITY_NOT_OFFERED)
|
||||
)
|
||||
state.source = source[:24]
|
||||
state.updated_at = moment
|
||||
changed += 1
|
||||
@@ -418,7 +453,7 @@ def record_account_commodity_state(
|
||||
"""写入单个商品的实时检查结论,供下一轮匹配直接使用。"""
|
||||
moment = observed_at or datetime.now(UTC)
|
||||
ttl = (
|
||||
capability_ttl_seconds()
|
||||
capability_ttl_seconds(availability)
|
||||
if valid_for_seconds is None
|
||||
else max(1, int(valid_for_seconds))
|
||||
)
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ from . import service
|
||||
|
||||
DEFAULT_INTERVAL_SECONDS = 20
|
||||
DEFAULT_MIN_INTERVAL_SECONDS = 5
|
||||
DEFAULT_BATCH_SIZE = 2
|
||||
DEFAULT_BATCH_SIZE = 20
|
||||
DEFAULT_RETRY_SECONDS = 45
|
||||
|
||||
_stop_event = threading.Event()
|
||||
@@ -103,7 +103,9 @@ def _schedule_retry(
|
||||
task = db.get(PlatformFulfillmentTask, int(task_id))
|
||||
if task is None or task.execution_status in {"completed", "cancelled"}:
|
||||
return
|
||||
task.next_allocate_at = moment + timedelta(seconds=retry_seconds)
|
||||
scheduled_retry = moment + timedelta(seconds=retry_seconds)
|
||||
if task.next_allocate_at is None or task.next_allocate_at < scheduled_retry:
|
||||
task.next_allocate_at = scheduled_retry
|
||||
db.commit()
|
||||
|
||||
|
||||
@@ -151,6 +153,9 @@ def run_auto_allocation_once(
|
||||
)
|
||||
candidates = (
|
||||
query.order_by(
|
||||
# 失败任务会被推进到未来;按 next_allocate_at 轮转可保证队首
|
||||
# 两个永久无库存任务不会每轮都抢走全部执行槽。
|
||||
PlatformFulfillmentTask.next_allocate_at.asc(),
|
||||
PlatformFulfillmentTask.paid_at.asc(),
|
||||
PlatformFulfillmentTask.id.asc(),
|
||||
)
|
||||
|
||||
@@ -18,6 +18,8 @@ from starlette.concurrency import run_in_threadpool
|
||||
|
||||
from ...models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AccountEvent,
|
||||
AccountLease,
|
||||
DouyuExternalOrder,
|
||||
DouyuGoodsSnapshot,
|
||||
@@ -45,6 +47,9 @@ from ..account_capability_service import (
|
||||
classify_goods_items,
|
||||
record_account_commodity_state,
|
||||
)
|
||||
from ..account_lifecycle import MANUAL_STATES
|
||||
from ..account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ..account_work_queue import enqueue_account_work
|
||||
from ..business_events import record_account_query, record_douyu_task_started
|
||||
from ..douyu_account_probe import (
|
||||
CAPABILITY_OK,
|
||||
@@ -68,7 +73,9 @@ from ..file_service import build_file_url
|
||||
from . import classification
|
||||
from .account_selector import (
|
||||
AccountCandidate,
|
||||
CapabilityCandidate,
|
||||
capability_candidate_accounts,
|
||||
capability_candidate_rows,
|
||||
has_recent_exchange,
|
||||
lease_account,
|
||||
live_candidate_accounts,
|
||||
@@ -93,6 +100,9 @@ BINDING_QR_TTL_SECONDS = int(
|
||||
)
|
||||
AUTO_SYNC_INTERVAL_SECONDS = 900.0
|
||||
DEFAULT_LIVE_CANDIDATE_LIMIT = 20
|
||||
LOCAL_STOCK_RETRY_SECONDS = 5 * 60
|
||||
LOCAL_CACHE_MISS_RETRY_SECONDS = 2 * 60
|
||||
LOCAL_LEASE_CONFLICT_RETRY_SECONDS = 5
|
||||
|
||||
|
||||
def _matching_mode() -> str:
|
||||
@@ -2296,6 +2306,389 @@ def upsert_mapping(
|
||||
return update_mapping(db, mapping_id=row.id, **values)
|
||||
|
||||
|
||||
def _cached_capability_state(
|
||||
candidate: CapabilityCandidate,
|
||||
*,
|
||||
commodity_id: str,
|
||||
) -> DouyuLiveAccountState:
|
||||
"""把新鲜本地能力快照转换成统一的履约状态,不访问斗鱼官网。"""
|
||||
account = candidate.account
|
||||
state = candidate.state
|
||||
reason = str(state.blocking_reason or "")
|
||||
return DouyuLiveAccountState(
|
||||
account_id=int(account.id),
|
||||
account_name=account.nickname or account.username or f"#{account.id}",
|
||||
checked_at=state.observed_at or _now(),
|
||||
ck_verdict="valid",
|
||||
ck_message="本地能力快照关联账号 CK 有效",
|
||||
elite_opened=account.elite_opened,
|
||||
points=account.points,
|
||||
limited_ids=frozenset(),
|
||||
role_change_available_at=account.role_change_available_at,
|
||||
commodity_availability={commodity_id: state.availability},
|
||||
commodity_blocking_reasons=(
|
||||
{commodity_id: reason} if reason else {}
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _capability_availability_counts(
|
||||
db: Session,
|
||||
*,
|
||||
commodity_id: str,
|
||||
scope: str,
|
||||
now: datetime,
|
||||
) -> dict[str, int]:
|
||||
rows = (
|
||||
db.query(AccountCommodityState.availability, func.count(AccountCommodityState.id))
|
||||
.filter(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
)
|
||||
.group_by(AccountCommodityState.availability)
|
||||
.all()
|
||||
)
|
||||
return {str(availability): int(count or 0) for availability, count in rows}
|
||||
|
||||
|
||||
def _local_allocation_retry_at(
|
||||
db: Session,
|
||||
*,
|
||||
task: PlatformFulfillmentTask,
|
||||
now: datetime,
|
||||
availability_counts: dict[str, int],
|
||||
lease_conflict: bool,
|
||||
) -> tuple[datetime, str]:
|
||||
"""按本地状态决定下一轮时间,避免永久无库存任务每 45 秒占槽。"""
|
||||
if lease_conflict:
|
||||
return (
|
||||
now + timedelta(seconds=LOCAL_LEASE_CONFLICT_RETRY_SECONDS),
|
||||
"lease_conflict",
|
||||
)
|
||||
next_open_at = (
|
||||
db.query(func.min(AccountCommodityState.available_from))
|
||||
.filter(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == (task.scope or SCOPE_ELITE),
|
||||
AccountCommodityState.commodity_id == task.commodity_id,
|
||||
AccountCommodityState.availability == AVAILABILITY_NOT_OPEN,
|
||||
AccountCommodityState.available_from.isnot(None),
|
||||
AccountCommodityState.available_from > now,
|
||||
)
|
||||
.scalar()
|
||||
)
|
||||
if next_open_at is not None:
|
||||
return next_open_at + timedelta(seconds=5), "commodity_not_open"
|
||||
if availability_counts.get(AVAILABILITY_NOT_OPEN):
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "commodity_not_open"
|
||||
if availability_counts.get(AVAILABILITY_NO_STOCK):
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "commodity_no_stock"
|
||||
if availability_counts:
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "no_eligible_account"
|
||||
return now + timedelta(seconds=LOCAL_CACHE_MISS_RETRY_SECONDS), "capability_cache_miss"
|
||||
|
||||
|
||||
def _seed_capability_refresh(
|
||||
db: Session,
|
||||
*,
|
||||
task: PlatformFulfillmentTask,
|
||||
now: datetime,
|
||||
) -> int:
|
||||
"""能力池缺失时只入队独立同步槽,不在分配线程里请求官网。"""
|
||||
try:
|
||||
seed_limit = int(
|
||||
os.getenv("PLATFORM_FULFILLMENT_CACHE_REFRESH_SEED_COUNT", "20")
|
||||
)
|
||||
except ValueError:
|
||||
seed_limit = 20
|
||||
seed_limit = max(1, min(seed_limit, 100))
|
||||
account_ids = [
|
||||
int(account_id)
|
||||
for (account_id,) in (
|
||||
db.query(AccountCommodityState.account_id)
|
||||
.join(Account, Account.id == AccountCommodityState.account_id)
|
||||
.filter(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == (task.scope or SCOPE_ELITE),
|
||||
AccountCommodityState.commodity_id == task.commodity_id,
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
Account.ck_check_status == "valid",
|
||||
Account.auto_managed.is_(True),
|
||||
Account.status.notin_(MANUAL_STATES),
|
||||
~db.query(AccountLease.id)
|
||||
.filter(AccountLease.active_account_id == Account.id)
|
||||
.exists(),
|
||||
)
|
||||
.order_by(
|
||||
AccountCommodityState.observed_at.is_(None).desc(),
|
||||
AccountCommodityState.observed_at.asc(),
|
||||
AccountCommodityState.account_id.asc(),
|
||||
)
|
||||
.limit(seed_limit)
|
||||
.all()
|
||||
)
|
||||
]
|
||||
if not account_ids:
|
||||
account_ids = [
|
||||
int(account.id)
|
||||
for account in live_candidate_accounts(
|
||||
db,
|
||||
limit=seed_limit,
|
||||
required_points=int(task.required_points or 0),
|
||||
commodity_id=task.commodity_id,
|
||||
)
|
||||
]
|
||||
for account_id in dict.fromkeys(account_ids):
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=account_id,
|
||||
work_type=WORK_STOREHOME_SYNC,
|
||||
priority=20,
|
||||
due_at=now,
|
||||
reason=f"履约任务 #{task.id} 缺少商品能力快照",
|
||||
payload={
|
||||
"commodity_id": task.commodity_id,
|
||||
"platform_task_id": int(task.id),
|
||||
},
|
||||
)
|
||||
return len(set(account_ids))
|
||||
|
||||
|
||||
def _allocate_task_from_capability(
|
||||
db: Session,
|
||||
*,
|
||||
task: PlatformFulfillmentTask,
|
||||
current: User,
|
||||
mode: str,
|
||||
) -> dict[str, Any]:
|
||||
"""只用本地能力索引选号;外部资格查询留给独立同步队列。"""
|
||||
moment = _now()
|
||||
required_points = int(task.required_points or 0)
|
||||
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))
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
capability_candidates = capability_candidate_rows(
|
||||
db,
|
||||
limit=candidate_limit,
|
||||
required_points=required_points,
|
||||
commodity_id=task.commodity_id,
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
now=moment,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
recent_exchange_ids: set[int] = set()
|
||||
if capability_candidates:
|
||||
candidate_ids = [int(item.account.id) for item in capability_candidates]
|
||||
since = moment - timedelta(days=365)
|
||||
recent_exchange_ids = {
|
||||
int(account_id)
|
||||
for (account_id,) in (
|
||||
db.query(AccountEvent.account_id)
|
||||
.filter(
|
||||
AccountEvent.account_id.in_(candidate_ids),
|
||||
AccountEvent.event_type == "exchange",
|
||||
AccountEvent.subject_key == task.commodity_id,
|
||||
AccountEvent.occurred_at >= since,
|
||||
)
|
||||
.distinct()
|
||||
.all()
|
||||
)
|
||||
}
|
||||
|
||||
diagnostics: list[dict[str, Any]] = []
|
||||
qualified: list[tuple[CapabilityCandidate, DouyuLiveAccountState]] = []
|
||||
for candidate in capability_candidates:
|
||||
account_id = int(candidate.account.id)
|
||||
if account_id in recent_exchange_ids:
|
||||
diagnostics.append(
|
||||
{
|
||||
"accountId": account_id,
|
||||
"accountName": candidate.account.nickname
|
||||
or candidate.account.username,
|
||||
"reasons": ["365 天内已有该商品兑换事件"],
|
||||
"source": "capability",
|
||||
}
|
||||
)
|
||||
continue
|
||||
qualified.append(
|
||||
(
|
||||
candidate,
|
||||
_cached_capability_state(
|
||||
candidate,
|
||||
commodity_id=task.commodity_id,
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
qualified.sort(
|
||||
key=lambda item: (
|
||||
item[1].readiness_rank(required_points=required_points),
|
||||
int(item[1].points or 0),
|
||||
int(item[0].account.id),
|
||||
)
|
||||
)
|
||||
selected: CapabilityCandidate | None = None
|
||||
selected_state: DouyuLiveAccountState | None = None
|
||||
lease = None
|
||||
lease_duration_minutes = _lease_duration_minutes(db)
|
||||
for candidate, cached_state in qualified:
|
||||
account = candidate.account
|
||||
candidate_model = AccountCandidate(
|
||||
account_id=int(account.id),
|
||||
username=str(account.username or ""),
|
||||
nickname=str(account.nickname or ""),
|
||||
points=int(cached_state.points or 0),
|
||||
gold_balance=account.gold_balance,
|
||||
points_expire_at=account.points_expire_at,
|
||||
role_change_available_at=account.role_change_available_at,
|
||||
)
|
||||
lease = lease_account(
|
||||
db,
|
||||
candidate=candidate_model,
|
||||
holder_user_id=current.id,
|
||||
task_id=task.id,
|
||||
commodity_id=task.commodity_id,
|
||||
ttl_minutes=lease_duration_minutes,
|
||||
commit=False,
|
||||
)
|
||||
if lease is not None:
|
||||
selected = candidate
|
||||
selected_state = cached_state
|
||||
break
|
||||
diagnostics.append(
|
||||
{
|
||||
"accountId": int(account.id),
|
||||
"accountName": cached_state.account_name,
|
||||
"reasons": ["账号刚被其他任务占用"],
|
||||
"source": "capability",
|
||||
}
|
||||
)
|
||||
|
||||
if selected is None or selected_state is None or lease is None:
|
||||
availability_counts = _capability_availability_counts(
|
||||
db,
|
||||
commodity_id=task.commodity_id,
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
now=moment,
|
||||
)
|
||||
retry_at, retry_reason = _local_allocation_retry_at(
|
||||
db,
|
||||
task=task,
|
||||
now=moment,
|
||||
availability_counts=availability_counts,
|
||||
lease_conflict=bool(qualified),
|
||||
)
|
||||
refresh_queued = (
|
||||
_seed_capability_refresh(db, task=task, now=moment)
|
||||
if retry_reason == "capability_cache_miss"
|
||||
else 0
|
||||
)
|
||||
if retry_reason == "commodity_not_open":
|
||||
task.issue_code = "commodity_not_open"
|
||||
task.last_error = "该商品尚未到开放时间,已按开放时间安排重新选号"
|
||||
elif retry_reason == "commodity_no_stock":
|
||||
task.issue_code = "commodity_no_stock"
|
||||
task.last_error = "本地能力池显示该商品当前无库存,稍后自动重试"
|
||||
elif retry_reason == "capability_cache_miss":
|
||||
task.issue_code = "capability_cache_miss"
|
||||
task.last_error = "暂无该商品的新鲜能力快照,等待后台补货刷新"
|
||||
elif retry_reason == "lease_conflict":
|
||||
task.issue_code = "account_lease_conflict"
|
||||
task.last_error = "已找到候选账号,但刚被其他任务占用"
|
||||
else:
|
||||
task.issue_code = "no_eligible_account"
|
||||
task.last_error = "本地能力池暂无可用账号"
|
||||
task.execution_status = "ready"
|
||||
task.next_allocate_at = retry_at
|
||||
task.updated_at = moment
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"account_allocation_failed",
|
||||
detail={
|
||||
"candidateCount": len(capability_candidates),
|
||||
"matchingMode": mode,
|
||||
"strategy": "local_capability",
|
||||
"capabilityCandidates": len(capability_candidates),
|
||||
"availabilityCounts": availability_counts,
|
||||
"retryAt": _jsonable(retry_at),
|
||||
"retryReason": retry_reason,
|
||||
"refreshQueued": refresh_queued,
|
||||
"diagnostics": diagnostics[:20],
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
selected_account = selected.account
|
||||
task.account_id = selected_account.id
|
||||
task.lease_id = lease.id
|
||||
task.operator_id = current.id
|
||||
task.execution_status = "allocated"
|
||||
task.next_allocate_at = None
|
||||
task.binding_status = "not_started"
|
||||
soft_reasons = selected_state.soft_requirement_reasons(
|
||||
required_points=required_points
|
||||
)
|
||||
gate_status, gate_issue, gate_message = _exchange_gate(
|
||||
selected_state,
|
||||
task.commodity_id,
|
||||
allow_not_open=is_presale,
|
||||
)
|
||||
if gate_status:
|
||||
task.exchange_status = gate_status
|
||||
task.issue_code = gate_issue
|
||||
task.last_error = gate_message
|
||||
elif selected_state.elite_opened is not True:
|
||||
task.exchange_status = "not_opened"
|
||||
task.issue_code = "elite_not_opened"
|
||||
task.last_error = "账号尚未开通精英宝典,可开通后继续"
|
||||
elif selected_state.points is None or selected_state.points < required_points:
|
||||
task.exchange_status = "points_short"
|
||||
task.issue_code = "points_short"
|
||||
task.last_error = "账号积分不足,可充值后继续"
|
||||
else:
|
||||
task.exchange_status = "eligible"
|
||||
task.issue_code = ""
|
||||
task.last_error = ""
|
||||
task.updated_at = moment
|
||||
_apply_task_lease_policy(db, task, lease=lease)
|
||||
_append_event(
|
||||
db,
|
||||
task.id,
|
||||
"account_allocated",
|
||||
actor_type="user",
|
||||
actor_id=current.id,
|
||||
detail={
|
||||
"accountId": selected_account.id,
|
||||
"accountName": selected_state.account_name,
|
||||
"points": selected_state.points,
|
||||
"eliteOpened": selected_state.elite_opened,
|
||||
"softReasons": soft_reasons,
|
||||
"exchangeStatus": task.exchange_status,
|
||||
"allocationStrategy": "local_capability",
|
||||
"capabilityObservedAt": _jsonable(selected.state.observed_at),
|
||||
"capabilityValidUntil": _jsonable(selected.state.valid_until),
|
||||
},
|
||||
)
|
||||
_emit_order_site_binding_state(db, task.id)
|
||||
db.commit()
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
|
||||
def prepare_manual_allocation(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -2336,6 +2729,18 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
if lease and lease.status == "active":
|
||||
return get_task_detail(db, task.id)
|
||||
|
||||
mode = _matching_mode()
|
||||
if mode in {"hybrid", "capability"}:
|
||||
# 2026-11-22:分配热路径只读本地能力索引。官网资格刷新已经由独立
|
||||
# account_sync_queue 执行;绑码和兑换前仍有各自的实时校验,不应让
|
||||
# 一次选号串行探测 20 个账号并阻塞其他订单。
|
||||
return _allocate_task_from_capability(
|
||||
db,
|
||||
task=task,
|
||||
current=current,
|
||||
mode=mode,
|
||||
)
|
||||
|
||||
config = ensure_douyu_config(db)
|
||||
lease_duration_minutes = _lease_duration_minutes(db)
|
||||
manual_id = str(douyu_config_value("manual_id", config.manual_id))
|
||||
@@ -2351,7 +2756,6 @@ def allocate_task(db: Session, *, task_id: int, current: User) -> dict[str, Any]
|
||||
except ValueError:
|
||||
candidate_limit = DEFAULT_LIVE_CANDIDATE_LIMIT
|
||||
candidate_limit = max(1, min(candidate_limit, 100))
|
||||
mode = _matching_mode()
|
||||
is_presale = classification.normalize_sale_mode(task.sale_mode) == "presale"
|
||||
capability_candidates = (
|
||||
capability_candidate_accounts(
|
||||
|
||||
Reference in New Issue
Block a user