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
+54 -19
View File
@@ -31,6 +31,7 @@ from .account_query_filters import (
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 (
SUPPLY_REASON_PREFIX,
active_account_work_count,
active_account_work_exists,
enqueue_account_work,
@@ -93,6 +94,14 @@ class SupplyReplenishmentResult:
targets: int
queued: int
shortages: int
refreshable_shortages: int = 0
capacity_shortages: int = 0
pending_sync: int = 0
pending_supply: int = 0
total_headroom: int = 0
supply_headroom: int = 0
total_cap: int = 0
supply_cap: int = 0
def _value_filter(value_class: str, *, now: datetime):
@@ -664,20 +673,20 @@ def _sync_candidate_query(
)
def _account_sync_queue_target_pending() -> int:
raw = os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")
def _sync_total_target() -> int:
"""同步队列总水位;订单补货和通用供给共享的硬上限。"""
try:
total = max(1, int(raw))
return max(1, int(os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "120")))
except ValueError:
total = 60
# 通用供给补货只占低水位;剩余水位留给有明确订单的履约商品,
# 防止注册顺序靠前的普通补货把待分配订单的紧急刷新挤到下一轮。
raw_supply = os.getenv("ACCOUNT_SUPPLY_QUEUE_TARGET_PENDING", "20")
return 120
def _supply_queue_target() -> int:
"""通用供给补货自己的保留水位,不被订单补货占用。"""
try:
supply = max(1, int(raw_supply))
return max(1, int(os.getenv("ACCOUNT_SUPPLY_QUEUE_TARGET_PENDING", "50")))
except ValueError:
supply = 20
return min(total, supply)
return 50
def enqueue_supply_replenishment(
@@ -689,27 +698,45 @@ def enqueue_supply_replenishment(
"""按本地目标缺口定向入队 storehome 同步,不发起外部请求。"""
moment = now or datetime.now(UTC)
inventories = load_supply_snapshot_inventories(db, now=moment)
queued = 0
shortages = 0
pending = active_account_work_count(
total_cap = _sync_total_target()
supply_cap = _supply_queue_target()
total_pending = active_account_work_count(
db,
work_types=ACCOUNT_SYNC_WORK_TYPES,
)
headroom = max(0, _account_sync_queue_target_pending() - pending)
supply_pending = active_account_work_count(
db,
work_types=ACCOUNT_SYNC_WORK_TYPES,
reason_prefix=SUPPLY_REASON_PREFIX,
)
# 供给有自己的保留水位,同时不能突破队列总水位。
# 旧逻辑用“总 pending”去减供给的 20,订单补货一忙,供给永远排队 0。
total_headroom = max(0, total_cap - total_pending)
supply_headroom = max(0, supply_cap - supply_pending)
headroom = min(total_headroom, supply_headroom)
queued = 0
shortages = 0
refreshable_shortages = 0
capacity_shortages = 0
for inventory in inventories:
if not inventory.needs_replenishment:
continue
shortages += 1
# 覆盖/可兑换缺口可以靠刷新账号能力解决;只有可分配缺口
# 是租约/协作/冷却造成的,刷新能力解决不了,不能占补货队列。
missing = max(
inventory.covered_shortage,
inventory.available_shortage,
)
if missing <= 0:
capacity_shortages += 1
continue
refreshable_shortages += 1
if headroom <= 0:
continue
target = db.get(AccountSupplyTarget, inventory.target_id)
if target is None:
continue
missing = max(
inventory.covered_shortage,
inventory.available_shortage,
inventory.allocatable_shortage,
)
limit = max(1, min(int(max_per_target), missing, headroom))
account_ids = [
int(account_id)
@@ -744,6 +771,14 @@ def enqueue_supply_replenishment(
targets=len(inventories),
queued=queued,
shortages=shortages,
refreshable_shortages=refreshable_shortages,
capacity_shortages=capacity_shortages,
pending_sync=total_pending,
pending_supply=supply_pending,
total_headroom=total_headroom,
supply_headroom=supply_headroom,
total_cap=total_cap,
supply_cap=supply_cap,
)