fix(douyu): 补全账号能力目录并回收失效租约
This commit is contained in:
@@ -461,6 +461,10 @@
|
||||
"20261122_0115_account_commodity_eligibility_split.py": {
|
||||
"revision": "20261122_0115",
|
||||
"sha256": "a66dba263e6b2e9b10b2bd52316ef630890b470a3c2b08f8ab0a07ef0521d0fa"
|
||||
},
|
||||
"20261122_0116_enable_fulfillment_lease_reaper.py": {
|
||||
"revision": "20261122_0116",
|
||||
"sha256": "e756ffc9e953fc1b6167376eff830f1441194636626587808e2a2746a9b16fce"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""enable fulfillment lease reaper and demand refresh cadence.
|
||||
|
||||
Revision ID: 20261122_0116
|
||||
Revises: 20261122_0115
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261122_0116"
|
||||
down_revision: str | None = "20261122_0115"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
REAPER_KEY = "platform_fulfillment_lease_reaper"
|
||||
CAPABILITY_REFRESH_KEY = "account_capability_refresh"
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
"""激活安全回收,并让需求补货跟随独立同步槽的分钟级节奏。
|
||||
|
||||
历史上两个作业虽然已注册,但生产行一直保持禁用/5 分钟间隔,导致
|
||||
扫码超时任务无限占号和商品能力补货长期滞后。
|
||||
"""
|
||||
if not sa.inspect(op.get_bind()).has_table("automation_jobs"):
|
||||
return
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE automation_jobs
|
||||
SET enabled = 1,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE `key` = :key
|
||||
"""
|
||||
).bindparams(key=REAPER_KEY)
|
||||
)
|
||||
op.execute(
|
||||
sa.text(
|
||||
"""
|
||||
UPDATE automation_jobs
|
||||
SET run_interval_seconds = 60,
|
||||
updated_at = CURRENT_TIMESTAMP
|
||||
WHERE `key` = :key
|
||||
AND run_interval_seconds = 300
|
||||
"""
|
||||
).bindparams(key=CAPABILITY_REFRESH_KEY)
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
"""不恢复禁用状态;回滚代码后仍应保留安全回收作业配置。"""
|
||||
return
|
||||
@@ -27,9 +27,13 @@ from .account_query_filters import (
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
from .account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from .account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
|
||||
from .account_value import is_high_value_expr
|
||||
from .account_work_queue import enqueue_account_work
|
||||
from .account_work_queue import (
|
||||
active_account_work_count,
|
||||
active_account_work_exists,
|
||||
enqueue_account_work,
|
||||
)
|
||||
|
||||
VALUE_HIGH = "high_value"
|
||||
VALUE_ORDINARY = "ordinary"
|
||||
@@ -523,13 +527,40 @@ def _sync_candidate_query(
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
return query.filter(~state_exists).order_by(
|
||||
Account.overview_queried_at.is_(None).desc(),
|
||||
Account.overview_queried_at.asc(),
|
||||
Account.id.asc(),
|
||||
return (
|
||||
query.filter(
|
||||
~state_exists,
|
||||
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
|
||||
assigned_to_pool_filter(),
|
||||
)
|
||||
.order_by(
|
||||
case((active_lease_exists(), 1), else_=0),
|
||||
case((active_collab_order_exists(), 1), else_=0),
|
||||
case((role_change_ready_filter(now), 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(),
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def _account_sync_queue_target_pending() -> int:
|
||||
raw = os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")
|
||||
try:
|
||||
total = max(1, int(raw))
|
||||
except ValueError:
|
||||
total = 60
|
||||
# 通用供给补货只占低水位;剩余水位留给有明确订单的履约商品,
|
||||
# 防止注册顺序靠前的普通补货把待分配订单的紧急刷新挤到下一轮。
|
||||
raw_supply = os.getenv("ACCOUNT_SUPPLY_QUEUE_TARGET_PENDING", "20")
|
||||
try:
|
||||
supply = max(1, int(raw_supply))
|
||||
except ValueError:
|
||||
supply = 20
|
||||
return min(total, supply)
|
||||
|
||||
|
||||
def enqueue_supply_replenishment(
|
||||
db: Session,
|
||||
*,
|
||||
@@ -541,10 +572,17 @@ def enqueue_supply_replenishment(
|
||||
inventories = evaluate_supply_targets(db, now=moment)
|
||||
queued = 0
|
||||
shortages = 0
|
||||
pending = active_account_work_count(
|
||||
db,
|
||||
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
||||
)
|
||||
headroom = max(0, _account_sync_queue_target_pending() - pending)
|
||||
for inventory in inventories:
|
||||
if not inventory.needs_replenishment:
|
||||
continue
|
||||
shortages += 1
|
||||
if headroom <= 0:
|
||||
continue
|
||||
target = db.get(AccountSupplyTarget, inventory.target_id)
|
||||
if target is None:
|
||||
continue
|
||||
@@ -553,7 +591,7 @@ def enqueue_supply_replenishment(
|
||||
inventory.available_shortage,
|
||||
inventory.allocatable_shortage,
|
||||
)
|
||||
limit = max(1, min(int(max_per_target), missing))
|
||||
limit = max(1, min(int(max_per_target), missing, headroom))
|
||||
accounts = (
|
||||
_sync_candidate_query(
|
||||
db,
|
||||
@@ -576,6 +614,7 @@ def enqueue_supply_replenishment(
|
||||
),
|
||||
)
|
||||
queued += 1
|
||||
headroom -= 1
|
||||
if queued:
|
||||
db.commit()
|
||||
return SupplyReplenishmentResult(
|
||||
|
||||
@@ -5,10 +5,43 @@ from __future__ import annotations
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import AccountWorkItem
|
||||
from ..models import Account, AccountWorkItem
|
||||
|
||||
|
||||
def active_account_work_exists(*, work_type: str):
|
||||
"""SQL 子查询:同一账号已有同类型的待执行或运行中工作。"""
|
||||
return (
|
||||
select(AccountWorkItem.id)
|
||||
.where(
|
||||
AccountWorkItem.account_id == Account.id,
|
||||
AccountWorkItem.work_type == work_type,
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def active_account_work_count(
|
||||
db: Session,
|
||||
*,
|
||||
work_types: tuple[str, ...],
|
||||
) -> int:
|
||||
"""当前账号同步队列中尚未结束的工作数。"""
|
||||
if not work_types:
|
||||
return 0
|
||||
return int(
|
||||
db.query(func.count(AccountWorkItem.id))
|
||||
.filter(
|
||||
AccountWorkItem.work_type.in_(work_types),
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
|
||||
def enqueue_account_work(
|
||||
@@ -247,6 +280,8 @@ def fail_account_work_type(
|
||||
|
||||
|
||||
__all__ = [
|
||||
"active_account_work_count",
|
||||
"active_account_work_exists",
|
||||
"claim_account_work",
|
||||
"complete_account_work",
|
||||
"complete_account_work_type",
|
||||
|
||||
@@ -127,8 +127,11 @@ def register_default_jobs() -> None:
|
||||
key=ACCOUNT_CAPABILITY_REFRESH_KEY,
|
||||
title="履约商品能力补充刷新",
|
||||
# 只刷新有 pending 履约需求且能力快照已过期的账号。
|
||||
# 2026-11-22:作业本身只做本地候选筛选,外部请求交给独立
|
||||
# account_sync_queue。每分钟补到有界水位,避免需求补货被通用
|
||||
# 供给作业挤占,也避免一次塞入数百条积压。
|
||||
interval_seconds=3600,
|
||||
run_interval_seconds=5 * 60,
|
||||
run_interval_seconds=60,
|
||||
batch_size=100,
|
||||
concurrency=3,
|
||||
run=run_account_capability_refresh,
|
||||
@@ -218,5 +221,6 @@ def register_default_jobs() -> None:
|
||||
batch_size=500,
|
||||
concurrency=1,
|
||||
run=run_platform_fulfillment_lease_reaper,
|
||||
default_enabled=True,
|
||||
)
|
||||
)
|
||||
|
||||
@@ -10,34 +10,37 @@ from __future__ import annotations
|
||||
from datetime import UTC, datetime
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import or_, select
|
||||
from sqlalchemy import case, func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import (
|
||||
Account,
|
||||
AccountCommodityState,
|
||||
AutomationJob,
|
||||
PlatformFulfillmentSetting,
|
||||
PlatformFulfillmentTask,
|
||||
)
|
||||
from ...account_query_filters import (
|
||||
active_collab_order_exists,
|
||||
active_lease_exists,
|
||||
assigned_to_pool_filter,
|
||||
automatable_account_filter,
|
||||
cookie_present_filter,
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
from ...account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
|
||||
from ...account_work_queue import (
|
||||
active_account_work_count,
|
||||
enqueue_account_work,
|
||||
)
|
||||
from ...platform_fulfillment.account_selector import (
|
||||
capability_refresh_candidate_accounts,
|
||||
)
|
||||
from ...account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ...account_work_queue import enqueue_account_work
|
||||
|
||||
JOB_KEY = "account_capability_refresh"
|
||||
|
||||
|
||||
def _base_query(db: Session, now: datetime):
|
||||
def _pending_demand_rows(
|
||||
db: Session,
|
||||
now: datetime,
|
||||
) -> list[tuple[str, int]]:
|
||||
"""返回待分配商品及订单数,按缺口大小排序。"""
|
||||
pending_commodities_query = (
|
||||
select(PlatformFulfillmentTask.commodity_id)
|
||||
select(
|
||||
PlatformFulfillmentTask.commodity_id,
|
||||
func.count(PlatformFulfillmentTask.id).label("waiting"),
|
||||
)
|
||||
.where(
|
||||
PlatformFulfillmentTask.execution_status == "ready",
|
||||
PlatformFulfillmentTask.account_id.is_(None),
|
||||
@@ -45,7 +48,6 @@ def _base_query(db: Session, now: datetime):
|
||||
PlatformFulfillmentTask.target_platform == "douyu",
|
||||
PlatformFulfillmentTask.scope == "elite",
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
match_started_at = (
|
||||
db.query(PlatformFulfillmentSetting.order_match_started_at)
|
||||
@@ -56,34 +58,59 @@ def _base_query(db: Session, now: datetime):
|
||||
pending_commodities_query = pending_commodities_query.where(
|
||||
PlatformFulfillmentTask.paid_at >= match_started_at
|
||||
)
|
||||
pending_commodities = pending_commodities_query
|
||||
# JOIN 比相关 EXISTS 更容易使用商品能力索引。当前生产账号池约 3 万,
|
||||
# 原写法会按账号逐个探测商品状态,慢查询实测扫描 40 万行。
|
||||
statement = (
|
||||
pending_commodities_query
|
||||
.group_by(PlatformFulfillmentTask.commodity_id)
|
||||
.order_by(func.count(PlatformFulfillmentTask.id).desc())
|
||||
)
|
||||
return list(db.execute(statement).all())
|
||||
|
||||
|
||||
def _queue_target_pending() -> int:
|
||||
import os
|
||||
|
||||
try:
|
||||
return max(1, int(os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")))
|
||||
except ValueError:
|
||||
return 60
|
||||
|
||||
|
||||
def _build_select_query(
|
||||
db: Session,
|
||||
job: AutomationJob,
|
||||
now: datetime | None = None,
|
||||
*,
|
||||
batch_limit: int | None = None,
|
||||
):
|
||||
moment = now or datetime.now(UTC)
|
||||
demand_rows = _pending_demand_rows(db, moment)
|
||||
commodity_ids = [str(row[0]) for row in demand_rows if row[0]]
|
||||
if not commodity_ids:
|
||||
return db.query(Account).filter(Account.id == -1)
|
||||
batch = max(
|
||||
1,
|
||||
min(
|
||||
int(batch_limit if batch_limit is not None else job.batch_size or 100),
|
||||
100,
|
||||
),
|
||||
)
|
||||
candidates = capability_refresh_candidate_accounts(
|
||||
db,
|
||||
commodity_ids=commodity_ids,
|
||||
scope="elite",
|
||||
limit=batch,
|
||||
now=moment,
|
||||
)
|
||||
if not candidates:
|
||||
return db.query(Account).filter(Account.id == -1)
|
||||
rank = {int(account.id): index for index, account in enumerate(candidates)}
|
||||
return (
|
||||
db.query(Account)
|
||||
.join(
|
||||
AccountCommodityState,
|
||||
AccountCommodityState.account_id == Account.id,
|
||||
.filter(Account.id.in_(tuple(rank)))
|
||||
.order_by(
|
||||
case(rank, value=Account.id),
|
||||
Account.id.asc(),
|
||||
)
|
||||
.filter(
|
||||
AccountCommodityState.platform == "douyu",
|
||||
AccountCommodityState.scope == "elite",
|
||||
AccountCommodityState.commodity_id.in_(pending_commodities),
|
||||
or_(
|
||||
AccountCommodityState.valid_until.is_(None),
|
||||
AccountCommodityState.valid_until <= now,
|
||||
AccountCommodityState.next_check_at.is_(None),
|
||||
AccountCommodityState.next_check_at <= now,
|
||||
),
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
role_change_ready_filter(now),
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
|
||||
|
||||
@@ -92,17 +119,7 @@ def build_select_query(
|
||||
job: AutomationJob,
|
||||
now: datetime | None = None,
|
||||
):
|
||||
moment = now or datetime.now(UTC)
|
||||
batch = max(1, int(job.batch_size or 100))
|
||||
return (
|
||||
_base_query(db, moment)
|
||||
.order_by(
|
||||
Account.overview_queried_at.is_(None).desc(),
|
||||
Account.overview_queried_at.asc(),
|
||||
Account.id.asc(),
|
||||
)
|
||||
.limit(batch)
|
||||
)
|
||||
return _build_select_query(db, job, now)
|
||||
|
||||
|
||||
def run_account_capability_refresh(
|
||||
@@ -110,14 +127,41 @@ def run_account_capability_refresh(
|
||||
job: AutomationJob,
|
||||
) -> dict[str, Any]:
|
||||
now = datetime.now(UTC)
|
||||
accounts = build_select_query(db, job, now).all()
|
||||
pending = active_account_work_count(
|
||||
db,
|
||||
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
||||
)
|
||||
headroom = max(0, _queue_target_pending() - pending)
|
||||
if headroom <= 0:
|
||||
return {
|
||||
"scanned": 0,
|
||||
"success": 0,
|
||||
"invalid": 0,
|
||||
"skipped": pending,
|
||||
"message": (
|
||||
f"账号同步队列仍有 {pending} 条待处理,本轮不继续补货,"
|
||||
"先交给独立同步槽消化"
|
||||
),
|
||||
"detail": {"queued": 0, "pending": pending, "headroom": 0},
|
||||
}
|
||||
demand_rows = _pending_demand_rows(db, now)
|
||||
commodity_ids = [str(row[0]) for row in demand_rows if row[0]]
|
||||
accounts = _build_select_query(
|
||||
db,
|
||||
job,
|
||||
now,
|
||||
batch_limit=headroom,
|
||||
).all()
|
||||
for account in accounts:
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
work_type=WORK_STOREHOME_SYNC,
|
||||
priority=40,
|
||||
reason="履约商品能力过期,等待补货刷新",
|
||||
priority=10,
|
||||
reason=(
|
||||
"待履约商品补货: "
|
||||
+ ",".join(commodity_ids[:5])
|
||||
)[:255],
|
||||
due_at=now,
|
||||
)
|
||||
db.commit()
|
||||
@@ -128,11 +172,16 @@ def run_account_capability_refresh(
|
||||
"invalid": 0,
|
||||
"skipped": 0,
|
||||
"message": (
|
||||
f"本轮筛选 {len(accounts)} 个到期账号,新入队 {queued} 个"
|
||||
f"本轮筛选 {len(accounts)} 个待补货账号,新入队 {queued} 个"
|
||||
if accounts
|
||||
else "当前履约商品没有到期待刷新的账号能力"
|
||||
else "当前履约商品没有可补充的新鲜 CK 候选账号"
|
||||
),
|
||||
"detail": {"queued": queued},
|
||||
"detail": {
|
||||
"queued": queued,
|
||||
"pending": pending,
|
||||
"headroom": headroom,
|
||||
"commodities": commodity_ids[:20],
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,15 +2,16 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import AccountLease, AutomationJob, PlatformFulfillmentTask
|
||||
from ....models import Account, AccountLease, AutomationJob, PlatformFulfillmentTask
|
||||
from ...platform_fulfillment.account_selector import (
|
||||
expire_platform_fulfillment_leases,
|
||||
)
|
||||
from ...platform_fulfillment.service import binding_failed_lease_ttl_minutes
|
||||
|
||||
JOB_KEY = "platform_fulfillment_lease_reaper"
|
||||
|
||||
@@ -22,6 +23,36 @@ def run_platform_fulfillment_lease_reaper(
|
||||
now: datetime | None = None,
|
||||
) -> dict[str, Any]:
|
||||
moment = now or datetime.now(UTC)
|
||||
stale_binding_cutoff = moment - timedelta(
|
||||
minutes=binding_failed_lease_ttl_minutes()
|
||||
)
|
||||
stale_binding_leases = (
|
||||
db.query(AccountLease)
|
||||
.join(
|
||||
PlatformFulfillmentTask,
|
||||
PlatformFulfillmentTask.lease_id == AccountLease.id,
|
||||
)
|
||||
.join(Account, Account.id == AccountLease.account_id)
|
||||
.filter(
|
||||
AccountLease.purpose == "platform_fulfillment",
|
||||
AccountLease.status == "active",
|
||||
AccountLease.expires_at.is_(None),
|
||||
PlatformFulfillmentTask.execution_status == "manual_required",
|
||||
PlatformFulfillmentTask.issue_code == "binding_failed",
|
||||
PlatformFulfillmentTask.updated_at <= stale_binding_cutoff,
|
||||
Account.role_bind_status == "unbound",
|
||||
)
|
||||
.order_by(AccountLease.id.asc())
|
||||
.limit(max(1, int(job.batch_size or 500)))
|
||||
.all()
|
||||
)
|
||||
for lease in stale_binding_leases:
|
||||
# 2026-11-22:扫码超时不等于正常人工处理。账号只保留一段可刷新
|
||||
# 二维码的宽限期,否则未绑定账号会被历史任务永久占住。
|
||||
lease.expires_at = moment
|
||||
lease.updated_at = moment
|
||||
if stale_binding_leases:
|
||||
db.flush()
|
||||
candidate_leases = (
|
||||
db.query(AccountLease)
|
||||
.filter(
|
||||
@@ -87,6 +118,9 @@ def run_platform_fulfillment_lease_reaper(
|
||||
for task in rows:
|
||||
if task.execution_status in {"completed", "cancelled"}:
|
||||
continue
|
||||
task.account_id = None
|
||||
task.lease_id = None
|
||||
task.bind_task_id = None
|
||||
task.execution_status = "manual_required"
|
||||
task.issue_code = "lease_expired"
|
||||
task.last_error = "账号租约已过期,请重新分配账号"
|
||||
@@ -97,6 +131,7 @@ def run_platform_fulfillment_lease_reaper(
|
||||
return {
|
||||
"scanned": expired,
|
||||
"expired": expired,
|
||||
"stale_binding_recovered": len(stale_binding_leases),
|
||||
"affected_tasks": affected,
|
||||
"message": f"直播账号履约租约回收 {expired} 条",
|
||||
}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -11,7 +11,7 @@ from typing import Any
|
||||
|
||||
from fastapi import HTTPException
|
||||
from loguru import logger
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy import func, or_, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
@@ -43,6 +43,7 @@ from ..account_capability_service import (
|
||||
AVAILABILITY_LIMITED,
|
||||
AVAILABILITY_NOT_OFFERED,
|
||||
AVAILABILITY_NOT_OPEN,
|
||||
OFFER_STATE_ACTIVE,
|
||||
OFFER_STATE_BEFORE,
|
||||
OFFER_STATE_NOT_OFFERED,
|
||||
OFFER_STATE_UNKNOWN,
|
||||
@@ -53,14 +54,19 @@ from ..account_capability_service import (
|
||||
record_account_commodity_state,
|
||||
)
|
||||
from ..account_query_filters import (
|
||||
active_collab_order_exists,
|
||||
active_lease_exists,
|
||||
assigned_to_pool_filter,
|
||||
automatable_account_filter,
|
||||
cookie_present_filter,
|
||||
role_change_ready_filter,
|
||||
valid_ck_filter,
|
||||
)
|
||||
from ..account_sync_runner import WORK_STOREHOME_SYNC
|
||||
from ..account_work_queue import enqueue_account_work
|
||||
from ..account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
|
||||
from ..account_work_queue import (
|
||||
active_account_work_count,
|
||||
enqueue_account_work,
|
||||
)
|
||||
from ..business_events import record_account_query, record_douyu_task_started
|
||||
from ..douyu_account_probe import (
|
||||
CAPABILITY_OK,
|
||||
@@ -87,6 +93,7 @@ from .account_selector import (
|
||||
CapabilityCandidate,
|
||||
capability_candidate_accounts,
|
||||
capability_candidate_rows,
|
||||
capability_refresh_candidate_accounts,
|
||||
has_recent_exchange,
|
||||
lease_account,
|
||||
live_candidate_accounts,
|
||||
@@ -114,6 +121,21 @@ DEFAULT_LIVE_CANDIDATE_LIMIT = 20
|
||||
LOCAL_STOCK_RETRY_SECONDS = 5 * 60
|
||||
LOCAL_CACHE_MISS_RETRY_SECONDS = 2 * 60
|
||||
LOCAL_LEASE_CONFLICT_RETRY_SECONDS = 5
|
||||
DEFAULT_BINDING_FAILED_LEASE_TTL_MINUTES = 120
|
||||
|
||||
|
||||
def binding_failed_lease_ttl_minutes() -> int:
|
||||
"""扫码超时任务的账号保留时间;过期后允许重新进入选号池。"""
|
||||
try:
|
||||
value = int(
|
||||
os.getenv(
|
||||
"PLATFORM_BINDING_FAILED_LEASE_TTL_MINUTES",
|
||||
str(DEFAULT_BINDING_FAILED_LEASE_TTL_MINUTES),
|
||||
)
|
||||
)
|
||||
except ValueError:
|
||||
return DEFAULT_BINDING_FAILED_LEASE_TTL_MINUTES
|
||||
return max(1, value)
|
||||
|
||||
|
||||
def _matching_mode() -> str:
|
||||
@@ -610,6 +632,11 @@ def _lease_policy_for_task(
|
||||
if status == "cancelled":
|
||||
return "cancelled", 0
|
||||
if status == "manual_required":
|
||||
if task.issue_code == "binding_failed":
|
||||
return (
|
||||
"binding_failed_cleanup",
|
||||
binding_failed_lease_ttl_minutes(),
|
||||
)
|
||||
return (
|
||||
"manual_required",
|
||||
settings.get("manualRequiredLeaseTtlMinutes"),
|
||||
@@ -2364,7 +2391,7 @@ def _capability_availability_counts(
|
||||
commodity_id: str,
|
||||
scope: str,
|
||||
now: datetime,
|
||||
) -> dict[str, int]:
|
||||
) -> dict[str, Any]:
|
||||
base = db.query(AccountCommodityState).filter(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == scope,
|
||||
@@ -2404,6 +2431,47 @@ def _capability_availability_counts(
|
||||
.group_by(AccountCommodityState.stock_state)
|
||||
.all()
|
||||
)
|
||||
eligible_account_ids = (
|
||||
select(AccountCommodityState.account_id)
|
||||
.where(
|
||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||
AccountCommodityState.scope == scope,
|
||||
AccountCommodityState.commodity_id == commodity_id,
|
||||
AccountCommodityState.account_eligibility == "eligible",
|
||||
AccountCommodityState.offer_state.in_((OFFER_STATE_ACTIVE, OFFER_STATE_BEFORE)),
|
||||
AccountCommodityState.valid_until.isnot(None),
|
||||
AccountCommodityState.valid_until > now,
|
||||
)
|
||||
.distinct()
|
||||
)
|
||||
account_base = db.query(Account).filter(Account.id.in_(eligible_account_ids))
|
||||
eligible_accounts = int(account_base.count())
|
||||
role_ready_accounts = int(
|
||||
account_base.filter(role_change_ready_filter(now)).count()
|
||||
)
|
||||
leased_accounts = int(account_base.filter(active_lease_exists()).count())
|
||||
collab_accounts = int(
|
||||
account_base.filter(active_collab_order_exists()).count()
|
||||
)
|
||||
allocatable_accounts = int(
|
||||
account_base.filter(
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
role_change_ready_filter(now),
|
||||
~active_lease_exists(),
|
||||
~active_collab_order_exists(),
|
||||
).count()
|
||||
)
|
||||
next_role_ready_at = (
|
||||
account_base.filter(
|
||||
Account.role_change_available_at.isnot(None),
|
||||
Account.role_change_available_at > now,
|
||||
)
|
||||
.with_entities(func.min(Account.role_change_available_at))
|
||||
.scalar()
|
||||
)
|
||||
return {
|
||||
**{
|
||||
f"eligibility:{key}": int(count or 0)
|
||||
@@ -2411,6 +2479,13 @@ def _capability_availability_counts(
|
||||
},
|
||||
**{f"offer:{key}": int(count or 0) for key, count in offer_rows},
|
||||
**{f"stock:{key}": int(count or 0) for key, count in stock_rows},
|
||||
"account:eligible": eligible_accounts,
|
||||
"account:roleReady": role_ready_accounts,
|
||||
"account:roleCooldown": max(0, eligible_accounts - role_ready_accounts),
|
||||
"account:leased": leased_accounts,
|
||||
"account:activeCollab": collab_accounts,
|
||||
"account:allocatable": allocatable_accounts,
|
||||
"account:nextRoleReadyAt": _jsonable(next_role_ready_at),
|
||||
}
|
||||
|
||||
|
||||
@@ -2444,9 +2519,27 @@ def _local_allocation_retry_at(
|
||||
return next_open_at + timedelta(seconds=5), "commodity_not_open"
|
||||
if availability_counts.get(f"offer:{OFFER_STATE_BEFORE}"):
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "commodity_not_open"
|
||||
if availability_counts:
|
||||
if availability_counts.get("eligibility:eligible"):
|
||||
return now + timedelta(seconds=LOCAL_LEASE_CONFLICT_RETRY_SECONDS), "lease_conflict"
|
||||
has_capability_rows = any(
|
||||
int(value or 0)
|
||||
for key, value in availability_counts.items()
|
||||
if key.startswith(("eligibility:", "offer:", "stock:"))
|
||||
)
|
||||
if has_capability_rows:
|
||||
if int(availability_counts.get("account:allocatable") or 0) > 0:
|
||||
return (
|
||||
now + timedelta(seconds=LOCAL_LEASE_CONFLICT_RETRY_SECONDS),
|
||||
"lease_conflict",
|
||||
)
|
||||
if int(availability_counts.get("account:roleCooldown") or 0) > 0:
|
||||
return (
|
||||
now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS),
|
||||
"account_role_cooldown",
|
||||
)
|
||||
if int(availability_counts.get("account:eligible") or 0) > 0:
|
||||
return (
|
||||
now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS),
|
||||
"account_pool_busy",
|
||||
)
|
||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "no_eligible_account"
|
||||
return now + timedelta(seconds=LOCAL_CACHE_MISS_RETRY_SECONDS), "capability_cache_miss"
|
||||
|
||||
@@ -2457,7 +2550,7 @@ def _seed_capability_refresh(
|
||||
task: PlatformFulfillmentTask,
|
||||
now: datetime,
|
||||
) -> int:
|
||||
"""能力池缺失时只入队独立同步槽,不在分配线程里请求官网。"""
|
||||
"""能力池不足时只入队独立同步槽,不在分配线程里请求官网。"""
|
||||
try:
|
||||
seed_limit = int(
|
||||
os.getenv("PLATFORM_FULFILLMENT_CACHE_REFRESH_SEED_COUNT", "20")
|
||||
@@ -2465,44 +2558,31 @@ def _seed_capability_refresh(
|
||||
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,
|
||||
cookie_present_filter(),
|
||||
valid_ck_filter(),
|
||||
automatable_account_filter(),
|
||||
assigned_to_pool_filter(),
|
||||
~active_lease_exists(),
|
||||
)
|
||||
.order_by(
|
||||
AccountCommodityState.observed_at.is_(None).desc(),
|
||||
AccountCommodityState.observed_at.asc(),
|
||||
AccountCommodityState.account_id.asc(),
|
||||
)
|
||||
.limit(seed_limit)
|
||||
.all()
|
||||
try:
|
||||
target_pending = max(
|
||||
1,
|
||||
int(os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")),
|
||||
)
|
||||
]
|
||||
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):
|
||||
except ValueError:
|
||||
target_pending = 60
|
||||
pending = active_account_work_count(
|
||||
db,
|
||||
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
||||
)
|
||||
headroom = max(0, target_pending - pending)
|
||||
if headroom <= 0:
|
||||
return 0
|
||||
accounts = capability_refresh_candidate_accounts(
|
||||
db,
|
||||
commodity_ids=[task.commodity_id],
|
||||
scope=task.scope or SCOPE_ELITE,
|
||||
limit=min(seed_limit, headroom),
|
||||
now=now,
|
||||
)
|
||||
for account in accounts:
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=account_id,
|
||||
account_id=int(account.id),
|
||||
work_type=WORK_STOREHOME_SYNC,
|
||||
priority=20,
|
||||
due_at=now,
|
||||
@@ -2512,7 +2592,7 @@ def _seed_capability_refresh(
|
||||
"platform_task_id": int(task.id),
|
||||
},
|
||||
)
|
||||
return len(set(account_ids))
|
||||
return len(accounts)
|
||||
|
||||
|
||||
def _allocate_task_from_capability(
|
||||
@@ -2649,7 +2729,12 @@ def _allocate_task_from_capability(
|
||||
)
|
||||
refresh_queued = (
|
||||
_seed_capability_refresh(db, task=task, now=moment)
|
||||
if retry_reason == "capability_cache_miss"
|
||||
if retry_reason
|
||||
in {
|
||||
"capability_cache_miss",
|
||||
"account_role_cooldown",
|
||||
"account_pool_busy",
|
||||
}
|
||||
else 0
|
||||
)
|
||||
if retry_reason == "commodity_not_open":
|
||||
@@ -2661,6 +2746,18 @@ def _allocate_task_from_capability(
|
||||
elif retry_reason == "lease_conflict":
|
||||
task.issue_code = "account_lease_conflict"
|
||||
task.last_error = "已找到候选账号,但刚被其他任务占用"
|
||||
elif retry_reason == "account_role_cooldown":
|
||||
task.issue_code = "account_role_cooldown"
|
||||
task.last_error = (
|
||||
"已有具备兑换资格的账号,但都处于斗鱼换绑冷却期;"
|
||||
"后台正在继续补充未绑定角色的可用账号"
|
||||
)
|
||||
elif retry_reason == "account_pool_busy":
|
||||
task.issue_code = "account_pool_busy"
|
||||
task.last_error = (
|
||||
"已有具备兑换资格的账号,但当前被租约或外部协作单占用;"
|
||||
"后台正在继续补充可用账号"
|
||||
)
|
||||
else:
|
||||
task.issue_code = "no_eligible_account"
|
||||
task.last_error = "本地能力池暂无可用账号"
|
||||
@@ -4121,6 +4218,7 @@ def release_task_to_manual(
|
||||
__all__ = [
|
||||
"allocate_task",
|
||||
"archive_mapping",
|
||||
"binding_failed_lease_ttl_minutes",
|
||||
"cancel_task",
|
||||
"complete_manual_exchange",
|
||||
"confirm_binding",
|
||||
|
||||
Reference in New Issue
Block a user