fix(automation): 修复账号供给补货队列饥饿

- 拆分订单补货和通用供给水位,供给保留独立 pending 配额
- 每批固定供给槽位,避免订单补货占满队列
- 只补覆盖/可兑换缺口,可分配缺口不再无效入队
- 重登工作超过 24h 自动转人工,清理待处理积压
- 补货阻塞告警和队列诊断信息
- 增加供给公平调度/过期清理回归测试
This commit is contained in:
yml
2026-11-24 13:38:19 +08:00
parent 5b6d146f93
commit eda6115ca3
10 changed files with 462 additions and 42 deletions
+77 -1
View File
@@ -42,7 +42,11 @@ from web.backend.services.account_supply_service import (
refresh_supply_snapshots,
upsert_supply_target,
)
from web.backend.services.account_work_queue import enqueue_account_work
from web.backend.services.account_work_queue import (
ORDER_REFRESH_REASON_PREFIX,
SUPPLY_REASON_PREFIX,
enqueue_account_work,
)
NOW = datetime.now(UTC).replace(microsecond=0)
@@ -811,3 +815,75 @@ def test_account_platform_status_alerts_on_persistently_low_trends(db):
"login_success_rate_low",
"relogin_failure_trend_high",
} <= codes
def test_supply_replenishment_queues_when_order_queue_is_busy(db):
# 60 个订单补货工作把旧版“总待处理 60 - 供给水位 20”占满;
# 新版供给有独立水位,仍应至少排入一个账号。
accounts = [_account(f"supply-busy-{index}", points=400) for index in range(65)]
db.add_all(accounts)
db.flush()
for account in accounts[:60]:
enqueue_account_work(
db,
account_id=account.id,
work_type="storehome_sync",
priority=10,
reason=f"{ORDER_REFRESH_REASON_PREFIX} C",
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=0,
)
)
db.commit()
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
assert result.pending_sync == 60
assert result.pending_supply == 0
assert result.total_cap == 120
assert result.supply_cap == 50
assert result.queued == 1
supply_work = (
db.query(AccountWorkItem)
.filter(AccountWorkItem.reason.like(f"{SUPPLY_REASON_PREFIX}%"))
.one()
)
assert supply_work.account_id in {account.id for account in accounts[60:]}
def test_supply_replenishment_skips_allocatable_only_shortage(db):
account = _account("supply-allocatable-only", points=400)
db.add(account)
db.flush()
db.add_all(
[
_state(account),
AccountSupplyTarget(
platform="douyu",
scope="elite",
commodity_id="C",
value_class=VALUE_HIGH,
target_covered=1,
target_available=1,
target_allocatable=2,
),
]
)
db.commit()
result = enqueue_supply_replenishment(db, now=NOW, max_per_target=10)
assert result.shortages == 1
assert result.refreshable_shortages == 0
assert result.capacity_shortages == 1
assert result.queued == 0
assert db.query(AccountWorkItem).count() == 0