fix(douyu): 补全账号能力目录并回收失效租约

This commit is contained in:
yml
2026-11-22 20:36:40 +08:00
parent 1906f924bc
commit af05c54c3f
18 changed files with 839 additions and 121 deletions
+5
View File
@@ -100,6 +100,9 @@ TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=6
TASK_LANE_LOGIN_WORKERS=4
TASK_LANE_ACCOUNT_CHECK_WORKERS=4
TASK_LANE_ACCOUNT_SYNC_WORKERS=3
# 账号同步队列待处理水位。通用供给只能占低水位,剩余预留给有明确订单的补货。
ACCOUNT_SYNC_QUEUE_TARGET_PENDING=60
ACCOUNT_SUPPLY_QUEUE_TARGET_PENDING=20
TASK_LANE_OVERVIEW_QUERY_WORKERS=5
TASK_LANE_QUERY_BATCH_WORKERS=3
TASK_LANE_PAYMENT_QR_WORKERS=10
@@ -215,6 +218,8 @@ PLATFORM_FULFILLMENT_MATCH_MODE=hybrid
PLATFORM_FULFILLMENT_AUTO_ALLOCATE_BATCH=20
# 能力池缺失时,每次最多向独立账号同步槽补入多少个账号
PLATFORM_FULFILLMENT_CACHE_REFRESH_SEED_COUNT=20
# 扫码超时任务保留账号多久;超过后释放未绑定账号,避免历史任务永久占号。
PLATFORM_BINDING_FAILED_LEASE_TTL_MINUTES=120
# 订单 Webhook 连续唤醒时保持最小选号间隔,避免忙轮询压满数据库。
PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=5
# 客户绑定二维码有效期
@@ -244,6 +244,25 @@ shortage = max(0, target + safety_buffer - effective_available)
15 分钟 TTL 只适合“能否立即分配”,不等于数据库覆盖。长期覆盖应使用“最后
已知状态 + 新鲜度标记”,不能因为 TTL 过期就从账号数据库删除或忽略状态。
### 2026-11-22 生产修正:目录覆盖与选号分离
生产只读统计暴露了两个被混在一起的口径:
- 5,940 个有效 CK 中,只有 202 个有新鲜 `storehome` 快照;
- 3,861 个从未同步过商品目录,1,877 个目录已过期;
- 当前有商品资格快照的 366 个账号又全部处于换绑冷却,空闲可分配为 0。
因此能力补货不能只扫描“当前空闲、未冷却、无租约”的几百个账号。正确边界是:
1. 商品目录补货:覆盖全部有效 CK,包括已被租约、协作单占用和换绑冷却的账号;
这些账号未来仍会回到选号池;
2. 自动选号:只筛选当前无租约、无活动协作单、换绑冷却已结束、CK 有效且商品
资格快照新鲜的账号;
3. 队列必须有界:需求补货优先,通用供给只占低水位,不能让补货生产速度长期
高于独立同步槽的消费速度;
4. 扫码超时转入 `manual_required/binding_failed` 的未绑定账号,默认只保留
120 分钟供刷新二维码;超时后由租约回收作业释放,不能无限占号。
## 每一次查询都必须同步
查询任务结果不能只保存在 `douyu_tasks.result`。任务有保留期,清理后会导致账号
+98 -3
View File
@@ -8,6 +8,7 @@ from web.backend.models import (
Account,
AccountCommodityState,
AccountLease,
AccountSyncState,
AccountWorkItem,
AutomationJob,
PlatformFulfillmentSetting,
@@ -61,7 +62,7 @@ def _state(
return row
def test_selects_only_stale_demand_accounts(db):
def test_selects_stale_and_never_scanned_demand_accounts(db):
user = User(
username="cap-refresh-admin",
password_hash="h",
@@ -111,7 +112,7 @@ def test_selects_only_stale_demand_accounts(db):
selected = job.build_select_query(db, automation_job, NOW).all()
assert [row.id for row in selected] == [stale.id]
assert [row.id for row in selected] == [stale.id, other.id, leased.id]
def test_no_pending_task_means_no_refresh(db):
@@ -177,6 +178,16 @@ def test_refresh_ignores_tasks_before_match_cutoff(db):
commodity_id="NEW",
valid_until=NOW - timedelta(minutes=1),
)
db.add(
AccountSyncState(
account_id=old_account.id,
platform="douyu",
domain="storehome",
status="success",
last_success_at=NOW,
valid_until=NOW + timedelta(hours=1),
)
)
db.commit()
automation_job = AutomationJob(
key=job.JOB_KEY,
@@ -225,7 +236,91 @@ def test_run_enqueues_stale_accounts_without_external_query(db):
assert result["scanned"] == 1
assert result["success"] == 1
assert work.status == "pending"
assert work.priority == 40
assert work.priority == 10
def test_refresh_skips_accounts_with_active_sync_work(db):
db.add(
PlatformFulfillmentTask(
source_system="order_site",
source_order_id="3",
source_order_item_id="3",
internal_sku_key="skin",
commodity_id="C",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=NOW,
)
)
active = _account(db, "cap-active-work")
next_account = _account(db, "cap-next-work")
_state(db, active, valid_until=NOW - timedelta(minutes=1))
_state(db, next_account, valid_until=NOW - timedelta(minutes=1))
db.add(
AccountWorkItem(
account_id=active.id,
work_type="storehome_sync",
priority=10,
status="pending",
due_at=NOW,
dedupe_key=f"storehome_sync:{active.id}",
)
)
db.commit()
automation_job = AutomationJob(
key=job.JOB_KEY,
title="履约商品能力补充刷新",
interval_seconds=3600,
batch_size=10,
concurrency=3,
)
selected = job.build_select_query(db, automation_job, NOW).all()
assert [row.id for row in selected] == [next_account.id]
def test_refresh_stops_at_queue_target(db, monkeypatch):
monkeypatch.setenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "1")
db.add(
PlatformFulfillmentTask(
source_system="order_site",
source_order_id="4",
source_order_item_id="4",
internal_sku_key="skin",
commodity_id="C",
execution_status="ready",
binding_status="not_started",
exchange_status="not_started",
paid_at=NOW,
)
)
account = _account(db, "cap-queue-full")
_state(db, account, valid_until=NOW - timedelta(minutes=1))
db.add(
AccountWorkItem(
account_id=account.id,
work_type="storehome_sync",
priority=10,
status="pending",
due_at=NOW,
dedupe_key=f"storehome_sync:{account.id}",
)
)
db.commit()
automation_job = AutomationJob(
key=job.JOB_KEY,
title="履约商品能力补充刷新",
interval_seconds=3600,
batch_size=10,
concurrency=3,
)
result = job.run_account_capability_refresh(db, automation_job)
assert result["scanned"] == 0
assert db.query(AccountWorkItem).count() == 1
def test_default_enabled_job_is_created_enabled(db, monkeypatch):
+74
View File
@@ -295,6 +295,80 @@ def test_supply_shortage_enqueues_missing_coverage(db):
assert work.priority == 35
def test_supply_shortage_does_not_reselect_account_with_active_sync(db):
first = _account("supply-active-work", points=400)
second = _account("supply-next-work", points=400)
db.add_all([first, second])
db.flush()
enqueue_account_work(
db,
account_id=first.id,
work_type="storehome_sync",
priority=10,
due_at=NOW,
)
db.add(
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=1,
target_available=1,
target_allocatable=1,
)
)
db.commit()
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
queued = (
db.query(AccountWorkItem)
.filter(AccountWorkItem.work_type == "storehome_sync")
.order_by(AccountWorkItem.account_id.asc())
.all()
)
assert result.queued == 1
assert [row.account_id for row in queued] == [first.id, second.id]
def test_supply_replenishment_respects_sync_queue_target(db, monkeypatch):
monkeypatch.setenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "1")
queued_account = _account("supply-queue-full", points=400)
missing = _account("supply-wait-next-round", points=400)
db.add_all([queued_account, missing])
db.flush()
enqueue_account_work(
db,
account_id=queued_account.id,
work_type="storehome_sync",
priority=10,
due_at=NOW,
)
db.add(
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=1,
target_available=1,
target_allocatable=1,
)
)
db.commit()
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
assert result.queued == 0
assert (
db.query(AccountWorkItem)
.filter(AccountWorkItem.work_type == "storehome_sync")
.count()
== 1
)
def test_supply_target_configuration_is_idempotent(db):
first = upsert_supply_target(
db,
+1 -1
View File
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = TESTS_DIR.parent
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
HEAD_REVISION = "20261122_0115"
HEAD_REVISION = "20261122_0116"
# 迁移新增、但不声明在模型里的复合查询索引。
EXTRA_INDEXES = (
+93
View File
@@ -1118,6 +1118,39 @@ class TestAccountSelection(_Base):
assert detail["task"]["executionStatus"] == "allocated"
self.refresh_mock.assert_not_called()
def test_allocation_reports_role_cooldown_without_fast_retry_loop(
self,
monkeypatch,
):
self.account(
"capability-role-cooldown",
points=2000,
role_available_at=NOW + timedelta(days=2),
)
task = self.task()
monkeypatch.setenv("PLATFORM_FULFILLMENT_MATCH_MODE", "capability")
detail = service.allocate_task(
self.session,
task_id=task.id,
current=self.admin,
)
self.session.refresh(task)
assert detail["task"]["account"] is None
assert detail["task"]["issueCode"] == "account_role_cooldown"
assert task.next_allocate_at is not None
assert task.next_allocate_at >= NOW + timedelta(minutes=4)
event = (
self.session.query(PlatformFulfillmentEvent)
.filter(PlatformFulfillmentEvent.event_type == "account_allocation_failed")
.order_by(PlatformFulfillmentEvent.id.desc())
.first()
)
assert event.detail_json["retryReason"] == "account_role_cooldown"
assert event.detail_json["availabilityCounts"]["account:eligible"] == 1
assert event.detail_json["availabilityCounts"]["account:roleCooldown"] == 1
def test_hybrid_without_capability_does_not_fall_back_to_live_scan(
self,
monkeypatch,
@@ -2863,6 +2896,66 @@ class TestBindingAndCompletion(_Base):
assert task.execution_status == "manual_required"
assert task.issue_code == "lease_expired"
def test_binding_failed_lease_uses_bounded_cleanup_ttl(self):
task = self._allocated()
account = self.session.get(Account, task.account_id)
account.role_bind_status = "unbound"
bind_task = DouyuTask(
batch_id="bind-timeout-cleanup",
account_id=task.account_id,
account_assigned_to=self.admin_id,
task_type="get_bind_qr",
handbook_scope="elite",
status="failed",
message="绑定二维码已过期,未识别到新扫码角色",
result={"bind_phase": "role_timeout"},
created_by=self.admin_id,
)
self.session.add(bind_task)
self.session.flush()
task.bind_task_id = bind_task.id
task.binding_status = "qr_pending"
task.execution_status = "waiting_bind"
self.session.commit()
service.refresh_binding(self.session, task_id=task.id)
lease = self.session.query(AccountLease).one()
self.session.refresh(task)
assert task.issue_code == "binding_failed"
assert lease.expires_at is not None
assert lease.expires_at >= datetime.now(UTC) + timedelta(minutes=119)
assert lease.expires_at <= datetime.now(UTC) + timedelta(minutes=121)
def test_reaper_recovers_stale_binding_failed_unbound_lease(self):
task = self._allocated()
lease = self.session.query(AccountLease).one()
account = self.session.get(Account, task.account_id)
account.role_bind_status = "unbound"
task.execution_status = "manual_required"
task.binding_status = "failed"
task.issue_code = "binding_failed"
task.updated_at = NOW - timedelta(hours=3)
lease.expires_at = None
self.session.commit()
job = AutomationJob(key=reaper.JOB_KEY, batch_size=100)
result = reaper.run_platform_fulfillment_lease_reaper(
self.session,
job,
now=NOW,
)
self.session.refresh(task)
self.session.refresh(lease)
assert result["stale_binding_recovered"] == 1
assert result["expired"] == 1
assert lease.status == "expired"
assert task.account_id is None
assert task.lease_id is None
assert task.bind_task_id is None
assert task.issue_code == "lease_expired"
def test_unlimited_lease_is_ignored_by_reaper(self):
task = self._allocated()
lease = self.session.query(AccountLease).one()
@@ -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
+46 -7
View File
@@ -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(
+36 -1
View File
@@ -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",
+5 -1
View File
@@ -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",
+1 -1
View File
@@ -870,7 +870,7 @@ export default function AccountDetailPage() {
<Space wrap>
<span>商品兑换能力</span>
<Text type="secondary" style={{ fontSize: 12, fontWeight: 'normal' }}>
可兑换 {commoditySummary.fresh_available ?? 0} / 共 {commoditySummary.total ?? 0}
可兑换资格 {commoditySummary.fresh_eligible ?? 0} / 共 {commoditySummary.total ?? 0}
</Text>
</Space>
}
@@ -144,6 +144,9 @@ const EVENT_LABELS: Record<string, string> = {
const ISSUE_LABELS: Record<string, string> = {
mapping_missing: '商品映射缺失',
no_eligible_account: '没有可分配账号',
capability_cache_miss: '账号商品能力待补货',
account_lease_conflict: '候选账号正被其他任务占用',
account_pool_busy: '资格账号暂时全部占用',
points_short: '积分不足',
elite_not_opened: '精英宝典未开通',
commodity_limit_reached: '商品已达限兑上限',
@@ -258,7 +261,24 @@ function eventDescription(event: { eventType: string; detail: Record<string, unk
const reasons = Array.isArray(row.reasons) ? row.reasons.map(String) : [];
return `${name}:${reasons.join('、') || '未满足分配条件'}`;
});
return lines.join(';') || '本批账号均不满足当前分配条件';
if (lines.length) return lines.join(';');
const counts =
detail.availabilityCounts && typeof detail.availabilityCounts === 'object'
? (detail.availabilityCounts as Record<string, unknown>)
: {};
const eligible = Number(counts['account:eligible'] || 0);
const roleCooldown = Number(counts['account:roleCooldown'] || 0);
const leased = Number(counts['account:leased'] || 0);
const collab = Number(counts['account:activeCollab'] || 0);
const summary = [
eligible ? `具备资格账号 ${eligible}` : '',
roleCooldown ? `换绑冷却 ${roleCooldown}` : '',
leased ? `任务租约占用 ${leased}` : '',
collab ? `协作单占用 ${collab}` : '',
].filter(Boolean);
return summary.length
? `${summary.join(';')},当前没有空闲账号`
: '本批账号均不满足当前分配条件';
}
if (event.eventType === 'account_allocated') {
const reasons = Array.isArray(detail.softReasons) ? detail.softReasons.map(String) : [];
@@ -547,7 +547,7 @@ export default function AccountPlatformStatusPanel() {
<Row gutter={[16, 16]}>
<Col xs={12} md={6}><Statistic title="CK 有效" value={status ? status.accounts.ck_valid : '--'} /></Col>
<Col xs={12} md={6}><Statistic title="24h 预计到期" value={status ? status.accounts.ck_expiring_24h : '--'} /></Col>
<Col xs={12} md={6}><Statistic title="新鲜可兑换商品" value={status ? status.commodities.fresh_available : '--'} /></Col>
<Col xs={12} md={6}><Statistic title="新鲜兑换资格商品" value={status ? status.commodities.fresh_eligible : '--'} /></Col>
<Col xs={12} md={6}><Statistic title="待处理账号工作" value={status ? pendingWork : '--'} /></Col>
</Row>