fix(douyu): 补全账号能力目录并回收失效租约
This commit is contained in:
@@ -100,6 +100,9 @@ TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=6
|
|||||||
TASK_LANE_LOGIN_WORKERS=4
|
TASK_LANE_LOGIN_WORKERS=4
|
||||||
TASK_LANE_ACCOUNT_CHECK_WORKERS=4
|
TASK_LANE_ACCOUNT_CHECK_WORKERS=4
|
||||||
TASK_LANE_ACCOUNT_SYNC_WORKERS=3
|
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_OVERVIEW_QUERY_WORKERS=5
|
||||||
TASK_LANE_QUERY_BATCH_WORKERS=3
|
TASK_LANE_QUERY_BATCH_WORKERS=3
|
||||||
TASK_LANE_PAYMENT_QR_WORKERS=10
|
TASK_LANE_PAYMENT_QR_WORKERS=10
|
||||||
@@ -215,6 +218,8 @@ PLATFORM_FULFILLMENT_MATCH_MODE=hybrid
|
|||||||
PLATFORM_FULFILLMENT_AUTO_ALLOCATE_BATCH=20
|
PLATFORM_FULFILLMENT_AUTO_ALLOCATE_BATCH=20
|
||||||
# 能力池缺失时,每次最多向独立账号同步槽补入多少个账号
|
# 能力池缺失时,每次最多向独立账号同步槽补入多少个账号
|
||||||
PLATFORM_FULFILLMENT_CACHE_REFRESH_SEED_COUNT=20
|
PLATFORM_FULFILLMENT_CACHE_REFRESH_SEED_COUNT=20
|
||||||
|
# 扫码超时任务保留账号多久;超过后释放未绑定账号,避免历史任务永久占号。
|
||||||
|
PLATFORM_BINDING_FAILED_LEASE_TTL_MINUTES=120
|
||||||
# 订单 Webhook 连续唤醒时保持最小选号间隔,避免忙轮询压满数据库。
|
# 订单 Webhook 连续唤醒时保持最小选号间隔,避免忙轮询压满数据库。
|
||||||
PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=5
|
PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=5
|
||||||
# 客户绑定二维码有效期
|
# 客户绑定二维码有效期
|
||||||
|
|||||||
@@ -244,6 +244,25 @@ shortage = max(0, target + safety_buffer - effective_available)
|
|||||||
15 分钟 TTL 只适合“能否立即分配”,不等于数据库覆盖。长期覆盖应使用“最后
|
15 分钟 TTL 只适合“能否立即分配”,不等于数据库覆盖。长期覆盖应使用“最后
|
||||||
已知状态 + 新鲜度标记”,不能因为 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`。任务有保留期,清理后会导致账号
|
查询任务结果不能只保存在 `douyu_tasks.result`。任务有保留期,清理后会导致账号
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ from web.backend.models import (
|
|||||||
Account,
|
Account,
|
||||||
AccountCommodityState,
|
AccountCommodityState,
|
||||||
AccountLease,
|
AccountLease,
|
||||||
|
AccountSyncState,
|
||||||
AccountWorkItem,
|
AccountWorkItem,
|
||||||
AutomationJob,
|
AutomationJob,
|
||||||
PlatformFulfillmentSetting,
|
PlatformFulfillmentSetting,
|
||||||
@@ -61,7 +62,7 @@ def _state(
|
|||||||
return row
|
return row
|
||||||
|
|
||||||
|
|
||||||
def test_selects_only_stale_demand_accounts(db):
|
def test_selects_stale_and_never_scanned_demand_accounts(db):
|
||||||
user = User(
|
user = User(
|
||||||
username="cap-refresh-admin",
|
username="cap-refresh-admin",
|
||||||
password_hash="h",
|
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()
|
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):
|
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",
|
commodity_id="NEW",
|
||||||
valid_until=NOW - timedelta(minutes=1),
|
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()
|
db.commit()
|
||||||
automation_job = AutomationJob(
|
automation_job = AutomationJob(
|
||||||
key=job.JOB_KEY,
|
key=job.JOB_KEY,
|
||||||
@@ -225,7 +236,91 @@ def test_run_enqueues_stale_accounts_without_external_query(db):
|
|||||||
assert result["scanned"] == 1
|
assert result["scanned"] == 1
|
||||||
assert result["success"] == 1
|
assert result["success"] == 1
|
||||||
assert work.status == "pending"
|
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):
|
def test_default_enabled_job_is_created_enabled(db, monkeypatch):
|
||||||
|
|||||||
@@ -295,6 +295,80 @@ def test_supply_shortage_enqueues_missing_coverage(db):
|
|||||||
assert work.priority == 35
|
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):
|
def test_supply_target_configuration_is_idempotent(db):
|
||||||
first = upsert_supply_target(
|
first = upsert_supply_target(
|
||||||
db,
|
db,
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
|||||||
PROJECT_ROOT = TESTS_DIR.parent
|
PROJECT_ROOT = TESTS_DIR.parent
|
||||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||||
|
|
||||||
HEAD_REVISION = "20261122_0115"
|
HEAD_REVISION = "20261122_0116"
|
||||||
|
|
||||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||||
EXTRA_INDEXES = (
|
EXTRA_INDEXES = (
|
||||||
|
|||||||
@@ -1118,6 +1118,39 @@ class TestAccountSelection(_Base):
|
|||||||
assert detail["task"]["executionStatus"] == "allocated"
|
assert detail["task"]["executionStatus"] == "allocated"
|
||||||
self.refresh_mock.assert_not_called()
|
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(
|
def test_hybrid_without_capability_does_not_fall_back_to_live_scan(
|
||||||
self,
|
self,
|
||||||
monkeypatch,
|
monkeypatch,
|
||||||
@@ -2863,6 +2896,66 @@ class TestBindingAndCompletion(_Base):
|
|||||||
assert task.execution_status == "manual_required"
|
assert task.execution_status == "manual_required"
|
||||||
assert task.issue_code == "lease_expired"
|
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):
|
def test_unlimited_lease_is_ignored_by_reaper(self):
|
||||||
task = self._allocated()
|
task = self._allocated()
|
||||||
lease = self.session.query(AccountLease).one()
|
lease = self.session.query(AccountLease).one()
|
||||||
|
|||||||
@@ -461,6 +461,10 @@
|
|||||||
"20261122_0115_account_commodity_eligibility_split.py": {
|
"20261122_0115_account_commodity_eligibility_split.py": {
|
||||||
"revision": "20261122_0115",
|
"revision": "20261122_0115",
|
||||||
"sha256": "a66dba263e6b2e9b10b2bd52316ef630890b470a3c2b08f8ab0a07ef0521d0fa"
|
"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,
|
role_change_ready_filter,
|
||||||
valid_ck_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_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_HIGH = "high_value"
|
||||||
VALUE_ORDINARY = "ordinary"
|
VALUE_ORDINARY = "ordinary"
|
||||||
@@ -523,13 +527,40 @@ def _sync_candidate_query(
|
|||||||
)
|
)
|
||||||
.exists()
|
.exists()
|
||||||
)
|
)
|
||||||
return query.filter(~state_exists).order_by(
|
return (
|
||||||
Account.overview_queried_at.is_(None).desc(),
|
query.filter(
|
||||||
Account.overview_queried_at.asc(),
|
~state_exists,
|
||||||
Account.id.asc(),
|
~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(
|
def enqueue_supply_replenishment(
|
||||||
db: Session,
|
db: Session,
|
||||||
*,
|
*,
|
||||||
@@ -541,10 +572,17 @@ def enqueue_supply_replenishment(
|
|||||||
inventories = evaluate_supply_targets(db, now=moment)
|
inventories = evaluate_supply_targets(db, now=moment)
|
||||||
queued = 0
|
queued = 0
|
||||||
shortages = 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:
|
for inventory in inventories:
|
||||||
if not inventory.needs_replenishment:
|
if not inventory.needs_replenishment:
|
||||||
continue
|
continue
|
||||||
shortages += 1
|
shortages += 1
|
||||||
|
if headroom <= 0:
|
||||||
|
continue
|
||||||
target = db.get(AccountSupplyTarget, inventory.target_id)
|
target = db.get(AccountSupplyTarget, inventory.target_id)
|
||||||
if target is None:
|
if target is None:
|
||||||
continue
|
continue
|
||||||
@@ -553,7 +591,7 @@ def enqueue_supply_replenishment(
|
|||||||
inventory.available_shortage,
|
inventory.available_shortage,
|
||||||
inventory.allocatable_shortage,
|
inventory.allocatable_shortage,
|
||||||
)
|
)
|
||||||
limit = max(1, min(int(max_per_target), missing))
|
limit = max(1, min(int(max_per_target), missing, headroom))
|
||||||
accounts = (
|
accounts = (
|
||||||
_sync_candidate_query(
|
_sync_candidate_query(
|
||||||
db,
|
db,
|
||||||
@@ -576,6 +614,7 @@ def enqueue_supply_replenishment(
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
queued += 1
|
queued += 1
|
||||||
|
headroom -= 1
|
||||||
if queued:
|
if queued:
|
||||||
db.commit()
|
db.commit()
|
||||||
return SupplyReplenishmentResult(
|
return SupplyReplenishmentResult(
|
||||||
|
|||||||
@@ -5,10 +5,43 @@ from __future__ import annotations
|
|||||||
from datetime import UTC, datetime, timedelta
|
from datetime import UTC, datetime, timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from sqlalchemy import func, select
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm import Session
|
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(
|
def enqueue_account_work(
|
||||||
@@ -247,6 +280,8 @@ def fail_account_work_type(
|
|||||||
|
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
|
"active_account_work_count",
|
||||||
|
"active_account_work_exists",
|
||||||
"claim_account_work",
|
"claim_account_work",
|
||||||
"complete_account_work",
|
"complete_account_work",
|
||||||
"complete_account_work_type",
|
"complete_account_work_type",
|
||||||
|
|||||||
@@ -127,8 +127,11 @@ def register_default_jobs() -> None:
|
|||||||
key=ACCOUNT_CAPABILITY_REFRESH_KEY,
|
key=ACCOUNT_CAPABILITY_REFRESH_KEY,
|
||||||
title="履约商品能力补充刷新",
|
title="履约商品能力补充刷新",
|
||||||
# 只刷新有 pending 履约需求且能力快照已过期的账号。
|
# 只刷新有 pending 履约需求且能力快照已过期的账号。
|
||||||
|
# 2026-11-22:作业本身只做本地候选筛选,外部请求交给独立
|
||||||
|
# account_sync_queue。每分钟补到有界水位,避免需求补货被通用
|
||||||
|
# 供给作业挤占,也避免一次塞入数百条积压。
|
||||||
interval_seconds=3600,
|
interval_seconds=3600,
|
||||||
run_interval_seconds=5 * 60,
|
run_interval_seconds=60,
|
||||||
batch_size=100,
|
batch_size=100,
|
||||||
concurrency=3,
|
concurrency=3,
|
||||||
run=run_account_capability_refresh,
|
run=run_account_capability_refresh,
|
||||||
@@ -218,5 +221,6 @@ def register_default_jobs() -> None:
|
|||||||
batch_size=500,
|
batch_size=500,
|
||||||
concurrency=1,
|
concurrency=1,
|
||||||
run=run_platform_fulfillment_lease_reaper,
|
run=run_platform_fulfillment_lease_reaper,
|
||||||
|
default_enabled=True,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -10,34 +10,37 @@ from __future__ import annotations
|
|||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from sqlalchemy import or_, select
|
from sqlalchemy import case, func, select
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ....models import (
|
from ....models import (
|
||||||
Account,
|
Account,
|
||||||
AccountCommodityState,
|
|
||||||
AutomationJob,
|
AutomationJob,
|
||||||
PlatformFulfillmentSetting,
|
PlatformFulfillmentSetting,
|
||||||
PlatformFulfillmentTask,
|
PlatformFulfillmentTask,
|
||||||
)
|
)
|
||||||
from ...account_query_filters import (
|
from ...account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
|
||||||
active_collab_order_exists,
|
from ...account_work_queue import (
|
||||||
active_lease_exists,
|
active_account_work_count,
|
||||||
assigned_to_pool_filter,
|
enqueue_account_work,
|
||||||
automatable_account_filter,
|
)
|
||||||
cookie_present_filter,
|
from ...platform_fulfillment.account_selector import (
|
||||||
role_change_ready_filter,
|
capability_refresh_candidate_accounts,
|
||||||
valid_ck_filter,
|
|
||||||
)
|
)
|
||||||
from ...account_sync_runner import WORK_STOREHOME_SYNC
|
|
||||||
from ...account_work_queue import enqueue_account_work
|
|
||||||
|
|
||||||
JOB_KEY = "account_capability_refresh"
|
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 = (
|
pending_commodities_query = (
|
||||||
select(PlatformFulfillmentTask.commodity_id)
|
select(
|
||||||
|
PlatformFulfillmentTask.commodity_id,
|
||||||
|
func.count(PlatformFulfillmentTask.id).label("waiting"),
|
||||||
|
)
|
||||||
.where(
|
.where(
|
||||||
PlatformFulfillmentTask.execution_status == "ready",
|
PlatformFulfillmentTask.execution_status == "ready",
|
||||||
PlatformFulfillmentTask.account_id.is_(None),
|
PlatformFulfillmentTask.account_id.is_(None),
|
||||||
@@ -45,7 +48,6 @@ def _base_query(db: Session, now: datetime):
|
|||||||
PlatformFulfillmentTask.target_platform == "douyu",
|
PlatformFulfillmentTask.target_platform == "douyu",
|
||||||
PlatformFulfillmentTask.scope == "elite",
|
PlatformFulfillmentTask.scope == "elite",
|
||||||
)
|
)
|
||||||
.distinct()
|
|
||||||
)
|
)
|
||||||
match_started_at = (
|
match_started_at = (
|
||||||
db.query(PlatformFulfillmentSetting.order_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(
|
pending_commodities_query = pending_commodities_query.where(
|
||||||
PlatformFulfillmentTask.paid_at >= match_started_at
|
PlatformFulfillmentTask.paid_at >= match_started_at
|
||||||
)
|
)
|
||||||
pending_commodities = pending_commodities_query
|
statement = (
|
||||||
# JOIN 比相关 EXISTS 更容易使用商品能力索引。当前生产账号池约 3 万,
|
pending_commodities_query
|
||||||
# 原写法会按账号逐个探测商品状态,慢查询实测扫描 40 万行。
|
.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 (
|
return (
|
||||||
db.query(Account)
|
db.query(Account)
|
||||||
.join(
|
.filter(Account.id.in_(tuple(rank)))
|
||||||
AccountCommodityState,
|
.order_by(
|
||||||
AccountCommodityState.account_id == Account.id,
|
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,
|
job: AutomationJob,
|
||||||
now: datetime | None = None,
|
now: datetime | None = None,
|
||||||
):
|
):
|
||||||
moment = now or datetime.now(UTC)
|
return _build_select_query(db, job, now)
|
||||||
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)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def run_account_capability_refresh(
|
def run_account_capability_refresh(
|
||||||
@@ -110,14 +127,41 @@ def run_account_capability_refresh(
|
|||||||
job: AutomationJob,
|
job: AutomationJob,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
now = datetime.now(UTC)
|
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:
|
for account in accounts:
|
||||||
enqueue_account_work(
|
enqueue_account_work(
|
||||||
db,
|
db,
|
||||||
account_id=int(account.id),
|
account_id=int(account.id),
|
||||||
work_type=WORK_STOREHOME_SYNC,
|
work_type=WORK_STOREHOME_SYNC,
|
||||||
priority=40,
|
priority=10,
|
||||||
reason="履约商品能力过期,等待补货刷新",
|
reason=(
|
||||||
|
"待履约商品补货: "
|
||||||
|
+ ",".join(commodity_ids[:5])
|
||||||
|
)[:255],
|
||||||
due_at=now,
|
due_at=now,
|
||||||
)
|
)
|
||||||
db.commit()
|
db.commit()
|
||||||
@@ -128,11 +172,16 @@ def run_account_capability_refresh(
|
|||||||
"invalid": 0,
|
"invalid": 0,
|
||||||
"skipped": 0,
|
"skipped": 0,
|
||||||
"message": (
|
"message": (
|
||||||
f"本轮筛选 {len(accounts)} 个到期账号,新入队 {queued} 个"
|
f"本轮筛选 {len(accounts)} 个待补货账号,新入队 {queued} 个"
|
||||||
if accounts
|
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 __future__ import annotations
|
||||||
|
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime, timedelta
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from ....models import AccountLease, AutomationJob, PlatformFulfillmentTask
|
from ....models import Account, AccountLease, AutomationJob, PlatformFulfillmentTask
|
||||||
from ...platform_fulfillment.account_selector import (
|
from ...platform_fulfillment.account_selector import (
|
||||||
expire_platform_fulfillment_leases,
|
expire_platform_fulfillment_leases,
|
||||||
)
|
)
|
||||||
|
from ...platform_fulfillment.service import binding_failed_lease_ttl_minutes
|
||||||
|
|
||||||
JOB_KEY = "platform_fulfillment_lease_reaper"
|
JOB_KEY = "platform_fulfillment_lease_reaper"
|
||||||
|
|
||||||
@@ -22,6 +23,36 @@ def run_platform_fulfillment_lease_reaper(
|
|||||||
now: datetime | None = None,
|
now: datetime | None = None,
|
||||||
) -> dict[str, Any]:
|
) -> dict[str, Any]:
|
||||||
moment = now or datetime.now(UTC)
|
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 = (
|
candidate_leases = (
|
||||||
db.query(AccountLease)
|
db.query(AccountLease)
|
||||||
.filter(
|
.filter(
|
||||||
@@ -87,6 +118,9 @@ def run_platform_fulfillment_lease_reaper(
|
|||||||
for task in rows:
|
for task in rows:
|
||||||
if task.execution_status in {"completed", "cancelled"}:
|
if task.execution_status in {"completed", "cancelled"}:
|
||||||
continue
|
continue
|
||||||
|
task.account_id = None
|
||||||
|
task.lease_id = None
|
||||||
|
task.bind_task_id = None
|
||||||
task.execution_status = "manual_required"
|
task.execution_status = "manual_required"
|
||||||
task.issue_code = "lease_expired"
|
task.issue_code = "lease_expired"
|
||||||
task.last_error = "账号租约已过期,请重新分配账号"
|
task.last_error = "账号租约已过期,请重新分配账号"
|
||||||
@@ -97,6 +131,7 @@ def run_platform_fulfillment_lease_reaper(
|
|||||||
return {
|
return {
|
||||||
"scanned": expired,
|
"scanned": expired,
|
||||||
"expired": expired,
|
"expired": expired,
|
||||||
|
"stale_binding_recovered": len(stale_binding_leases),
|
||||||
"affected_tasks": affected,
|
"affected_tasks": affected,
|
||||||
"message": f"直播账号履约租约回收 {expired} 条",
|
"message": f"直播账号履约租约回收 {expired} 条",
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ from __future__ import annotations
|
|||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
from datetime import UTC, datetime, timedelta
|
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.exc import IntegrityError
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
@@ -18,6 +18,7 @@ from ...models import (
|
|||||||
AccountCommodityState,
|
AccountCommodityState,
|
||||||
AccountEvent,
|
AccountEvent,
|
||||||
AccountLease,
|
AccountLease,
|
||||||
|
AccountSyncState,
|
||||||
)
|
)
|
||||||
from ..account_capability_service import OFFER_STATE_ACTIVE
|
from ..account_capability_service import OFFER_STATE_ACTIVE
|
||||||
from ..account_events_service import (
|
from ..account_events_service import (
|
||||||
@@ -36,6 +37,8 @@ from ..account_query_filters import (
|
|||||||
role_change_ready_filter,
|
role_change_ready_filter,
|
||||||
valid_ck_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
|
VALIDATION_LEASE_MINUTES = 5
|
||||||
BINDING_LEASE_MINUTES = 120
|
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(
|
def has_recent_exchange(
|
||||||
db: Session,
|
db: Session,
|
||||||
*,
|
*,
|
||||||
@@ -590,6 +683,7 @@ __all__ = [
|
|||||||
"acquire_account_lease",
|
"acquire_account_lease",
|
||||||
"active_lease_for_account",
|
"active_lease_for_account",
|
||||||
"capability_candidate_accounts",
|
"capability_candidate_accounts",
|
||||||
|
"capability_refresh_candidate_accounts",
|
||||||
"eligible_accounts",
|
"eligible_accounts",
|
||||||
"expire_platform_fulfillment_leases",
|
"expire_platform_fulfillment_leases",
|
||||||
"has_recent_exchange",
|
"has_recent_exchange",
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ from typing import Any
|
|||||||
|
|
||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from sqlalchemy import func, or_
|
from sqlalchemy import func, or_, select
|
||||||
from sqlalchemy.exc import IntegrityError
|
from sqlalchemy.exc import IntegrityError
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from starlette.concurrency import run_in_threadpool
|
from starlette.concurrency import run_in_threadpool
|
||||||
@@ -43,6 +43,7 @@ from ..account_capability_service import (
|
|||||||
AVAILABILITY_LIMITED,
|
AVAILABILITY_LIMITED,
|
||||||
AVAILABILITY_NOT_OFFERED,
|
AVAILABILITY_NOT_OFFERED,
|
||||||
AVAILABILITY_NOT_OPEN,
|
AVAILABILITY_NOT_OPEN,
|
||||||
|
OFFER_STATE_ACTIVE,
|
||||||
OFFER_STATE_BEFORE,
|
OFFER_STATE_BEFORE,
|
||||||
OFFER_STATE_NOT_OFFERED,
|
OFFER_STATE_NOT_OFFERED,
|
||||||
OFFER_STATE_UNKNOWN,
|
OFFER_STATE_UNKNOWN,
|
||||||
@@ -53,14 +54,19 @@ from ..account_capability_service import (
|
|||||||
record_account_commodity_state,
|
record_account_commodity_state,
|
||||||
)
|
)
|
||||||
from ..account_query_filters import (
|
from ..account_query_filters import (
|
||||||
|
active_collab_order_exists,
|
||||||
active_lease_exists,
|
active_lease_exists,
|
||||||
assigned_to_pool_filter,
|
assigned_to_pool_filter,
|
||||||
automatable_account_filter,
|
automatable_account_filter,
|
||||||
cookie_present_filter,
|
cookie_present_filter,
|
||||||
|
role_change_ready_filter,
|
||||||
valid_ck_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_work_queue import enqueue_account_work
|
from ..account_work_queue import (
|
||||||
|
active_account_work_count,
|
||||||
|
enqueue_account_work,
|
||||||
|
)
|
||||||
from ..business_events import record_account_query, record_douyu_task_started
|
from ..business_events import record_account_query, record_douyu_task_started
|
||||||
from ..douyu_account_probe import (
|
from ..douyu_account_probe import (
|
||||||
CAPABILITY_OK,
|
CAPABILITY_OK,
|
||||||
@@ -87,6 +93,7 @@ from .account_selector import (
|
|||||||
CapabilityCandidate,
|
CapabilityCandidate,
|
||||||
capability_candidate_accounts,
|
capability_candidate_accounts,
|
||||||
capability_candidate_rows,
|
capability_candidate_rows,
|
||||||
|
capability_refresh_candidate_accounts,
|
||||||
has_recent_exchange,
|
has_recent_exchange,
|
||||||
lease_account,
|
lease_account,
|
||||||
live_candidate_accounts,
|
live_candidate_accounts,
|
||||||
@@ -114,6 +121,21 @@ DEFAULT_LIVE_CANDIDATE_LIMIT = 20
|
|||||||
LOCAL_STOCK_RETRY_SECONDS = 5 * 60
|
LOCAL_STOCK_RETRY_SECONDS = 5 * 60
|
||||||
LOCAL_CACHE_MISS_RETRY_SECONDS = 2 * 60
|
LOCAL_CACHE_MISS_RETRY_SECONDS = 2 * 60
|
||||||
LOCAL_LEASE_CONFLICT_RETRY_SECONDS = 5
|
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:
|
def _matching_mode() -> str:
|
||||||
@@ -610,6 +632,11 @@ def _lease_policy_for_task(
|
|||||||
if status == "cancelled":
|
if status == "cancelled":
|
||||||
return "cancelled", 0
|
return "cancelled", 0
|
||||||
if status == "manual_required":
|
if status == "manual_required":
|
||||||
|
if task.issue_code == "binding_failed":
|
||||||
|
return (
|
||||||
|
"binding_failed_cleanup",
|
||||||
|
binding_failed_lease_ttl_minutes(),
|
||||||
|
)
|
||||||
return (
|
return (
|
||||||
"manual_required",
|
"manual_required",
|
||||||
settings.get("manualRequiredLeaseTtlMinutes"),
|
settings.get("manualRequiredLeaseTtlMinutes"),
|
||||||
@@ -2364,7 +2391,7 @@ def _capability_availability_counts(
|
|||||||
commodity_id: str,
|
commodity_id: str,
|
||||||
scope: str,
|
scope: str,
|
||||||
now: datetime,
|
now: datetime,
|
||||||
) -> dict[str, int]:
|
) -> dict[str, Any]:
|
||||||
base = db.query(AccountCommodityState).filter(
|
base = db.query(AccountCommodityState).filter(
|
||||||
AccountCommodityState.platform == TARGET_PLATFORM,
|
AccountCommodityState.platform == TARGET_PLATFORM,
|
||||||
AccountCommodityState.scope == scope,
|
AccountCommodityState.scope == scope,
|
||||||
@@ -2404,6 +2431,47 @@ def _capability_availability_counts(
|
|||||||
.group_by(AccountCommodityState.stock_state)
|
.group_by(AccountCommodityState.stock_state)
|
||||||
.all()
|
.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 {
|
return {
|
||||||
**{
|
**{
|
||||||
f"eligibility:{key}": int(count or 0)
|
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"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},
|
**{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"
|
return next_open_at + timedelta(seconds=5), "commodity_not_open"
|
||||||
if availability_counts.get(f"offer:{OFFER_STATE_BEFORE}"):
|
if availability_counts.get(f"offer:{OFFER_STATE_BEFORE}"):
|
||||||
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "commodity_not_open"
|
return now + timedelta(seconds=LOCAL_STOCK_RETRY_SECONDS), "commodity_not_open"
|
||||||
if availability_counts:
|
has_capability_rows = any(
|
||||||
if availability_counts.get("eligibility:eligible"):
|
int(value or 0)
|
||||||
return now + timedelta(seconds=LOCAL_LEASE_CONFLICT_RETRY_SECONDS), "lease_conflict"
|
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_STOCK_RETRY_SECONDS), "no_eligible_account"
|
||||||
return now + timedelta(seconds=LOCAL_CACHE_MISS_RETRY_SECONDS), "capability_cache_miss"
|
return now + timedelta(seconds=LOCAL_CACHE_MISS_RETRY_SECONDS), "capability_cache_miss"
|
||||||
|
|
||||||
@@ -2457,7 +2550,7 @@ def _seed_capability_refresh(
|
|||||||
task: PlatformFulfillmentTask,
|
task: PlatformFulfillmentTask,
|
||||||
now: datetime,
|
now: datetime,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""能力池缺失时只入队独立同步槽,不在分配线程里请求官网。"""
|
"""能力池不足时只入队独立同步槽,不在分配线程里请求官网。"""
|
||||||
try:
|
try:
|
||||||
seed_limit = int(
|
seed_limit = int(
|
||||||
os.getenv("PLATFORM_FULFILLMENT_CACHE_REFRESH_SEED_COUNT", "20")
|
os.getenv("PLATFORM_FULFILLMENT_CACHE_REFRESH_SEED_COUNT", "20")
|
||||||
@@ -2465,44 +2558,31 @@ def _seed_capability_refresh(
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
seed_limit = 20
|
seed_limit = 20
|
||||||
seed_limit = max(1, min(seed_limit, 100))
|
seed_limit = max(1, min(seed_limit, 100))
|
||||||
account_ids = [
|
try:
|
||||||
int(account_id)
|
target_pending = max(
|
||||||
for (account_id,) in (
|
1,
|
||||||
db.query(AccountCommodityState.account_id)
|
int(os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")),
|
||||||
.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()
|
|
||||||
)
|
)
|
||||||
]
|
except ValueError:
|
||||||
if not account_ids:
|
target_pending = 60
|
||||||
account_ids = [
|
pending = active_account_work_count(
|
||||||
int(account.id)
|
db,
|
||||||
for account in live_candidate_accounts(
|
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
||||||
db,
|
)
|
||||||
limit=seed_limit,
|
headroom = max(0, target_pending - pending)
|
||||||
required_points=int(task.required_points or 0),
|
if headroom <= 0:
|
||||||
commodity_id=task.commodity_id,
|
return 0
|
||||||
)
|
accounts = capability_refresh_candidate_accounts(
|
||||||
]
|
db,
|
||||||
for account_id in dict.fromkeys(account_ids):
|
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(
|
enqueue_account_work(
|
||||||
db,
|
db,
|
||||||
account_id=account_id,
|
account_id=int(account.id),
|
||||||
work_type=WORK_STOREHOME_SYNC,
|
work_type=WORK_STOREHOME_SYNC,
|
||||||
priority=20,
|
priority=20,
|
||||||
due_at=now,
|
due_at=now,
|
||||||
@@ -2512,7 +2592,7 @@ def _seed_capability_refresh(
|
|||||||
"platform_task_id": int(task.id),
|
"platform_task_id": int(task.id),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
return len(set(account_ids))
|
return len(accounts)
|
||||||
|
|
||||||
|
|
||||||
def _allocate_task_from_capability(
|
def _allocate_task_from_capability(
|
||||||
@@ -2649,7 +2729,12 @@ def _allocate_task_from_capability(
|
|||||||
)
|
)
|
||||||
refresh_queued = (
|
refresh_queued = (
|
||||||
_seed_capability_refresh(db, task=task, now=moment)
|
_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
|
else 0
|
||||||
)
|
)
|
||||||
if retry_reason == "commodity_not_open":
|
if retry_reason == "commodity_not_open":
|
||||||
@@ -2661,6 +2746,18 @@ def _allocate_task_from_capability(
|
|||||||
elif retry_reason == "lease_conflict":
|
elif retry_reason == "lease_conflict":
|
||||||
task.issue_code = "account_lease_conflict"
|
task.issue_code = "account_lease_conflict"
|
||||||
task.last_error = "已找到候选账号,但刚被其他任务占用"
|
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:
|
else:
|
||||||
task.issue_code = "no_eligible_account"
|
task.issue_code = "no_eligible_account"
|
||||||
task.last_error = "本地能力池暂无可用账号"
|
task.last_error = "本地能力池暂无可用账号"
|
||||||
@@ -4121,6 +4218,7 @@ def release_task_to_manual(
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
"allocate_task",
|
"allocate_task",
|
||||||
"archive_mapping",
|
"archive_mapping",
|
||||||
|
"binding_failed_lease_ttl_minutes",
|
||||||
"cancel_task",
|
"cancel_task",
|
||||||
"complete_manual_exchange",
|
"complete_manual_exchange",
|
||||||
"confirm_binding",
|
"confirm_binding",
|
||||||
|
|||||||
@@ -870,7 +870,7 @@ export default function AccountDetailPage() {
|
|||||||
<Space wrap>
|
<Space wrap>
|
||||||
<span>商品兑换能力</span>
|
<span>商品兑换能力</span>
|
||||||
<Text type="secondary" style={{ fontSize: 12, fontWeight: 'normal' }}>
|
<Text type="secondary" style={{ fontSize: 12, fontWeight: 'normal' }}>
|
||||||
可兑换 {commoditySummary.fresh_available ?? 0} / 共 {commoditySummary.total ?? 0}
|
可兑换资格 {commoditySummary.fresh_eligible ?? 0} / 共 {commoditySummary.total ?? 0}
|
||||||
</Text>
|
</Text>
|
||||||
</Space>
|
</Space>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -144,6 +144,9 @@ const EVENT_LABELS: Record<string, string> = {
|
|||||||
const ISSUE_LABELS: Record<string, string> = {
|
const ISSUE_LABELS: Record<string, string> = {
|
||||||
mapping_missing: '商品映射缺失',
|
mapping_missing: '商品映射缺失',
|
||||||
no_eligible_account: '没有可分配账号',
|
no_eligible_account: '没有可分配账号',
|
||||||
|
capability_cache_miss: '账号商品能力待补货',
|
||||||
|
account_lease_conflict: '候选账号正被其他任务占用',
|
||||||
|
account_pool_busy: '资格账号暂时全部占用',
|
||||||
points_short: '积分不足',
|
points_short: '积分不足',
|
||||||
elite_not_opened: '精英宝典未开通',
|
elite_not_opened: '精英宝典未开通',
|
||||||
commodity_limit_reached: '商品已达限兑上限',
|
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) : [];
|
const reasons = Array.isArray(row.reasons) ? row.reasons.map(String) : [];
|
||||||
return `${name}:${reasons.join('、') || '未满足分配条件'}`;
|
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') {
|
if (event.eventType === 'account_allocated') {
|
||||||
const reasons = Array.isArray(detail.softReasons) ? detail.softReasons.map(String) : [];
|
const reasons = Array.isArray(detail.softReasons) ? detail.softReasons.map(String) : [];
|
||||||
|
|||||||
@@ -547,7 +547,7 @@ export default function AccountPlatformStatusPanel() {
|
|||||||
<Row gutter={[16, 16]}>
|
<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="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="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>
|
<Col xs={12} md={6}><Statistic title="待处理账号工作" value={status ? pendingWork : '--'} /></Col>
|
||||||
</Row>
|
</Row>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user