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
+50 -2
View File
@@ -23,6 +23,7 @@ from .account_sync_service import (
sync_storehome_snapshot,
)
from .account_work_queue import (
SUPPLY_REASON_PREFIX,
claim_account_work,
complete_account_work,
fail_account_work,
@@ -299,6 +300,50 @@ def execute_account_work_item(
session.close()
def _supply_queue_quota(limit: int) -> int:
"""每批给通用供给补货保留的槽位数,避免被订单补货永久饿死。"""
import os
try:
configured = max(1, int(os.getenv("ACCOUNT_SUPPLY_QUEUE_QUOTA", "8")))
except ValueError:
configured = 8
# 配置按 20 条满批理解;可用槽位变小时按比例缩,避免只剩 3 个槽位时
# 供给全拿走、订单补货完全饿死。
scaled = max(1, round(int(limit) * configured / 20))
return max(1, min(int(limit), scaled))
def claim_sync_work_batch(
db: Session,
*,
limit: int,
now: datetime,
) -> tuple[list[AccountWorkItem], list[AccountWorkItem]]:
"""按固定配额领取供给工作和订单/其他同步工作。
供给任务优先占 ``ACCOUNT_SUPPLY_QUEUE_QUOTA`` 个槽位;剩余槽位再给
订单能力补货。年龄超过 30 分钟的任务仍然由 claim 内部的 aging 规则优先。
"""
safe_limit = max(1, int(limit))
supply_quota = _supply_queue_quota(safe_limit)
supply_items = claim_account_work(
db,
work_types=ACCOUNT_SYNC_WORK_TYPES,
limit=supply_quota,
now=now,
reason_prefix=SUPPLY_REASON_PREFIX,
)
normal_items = claim_account_work(
db,
work_types=ACCOUNT_SYNC_WORK_TYPES,
limit=max(0, safe_limit - len(supply_items)),
now=now,
exclude_reason_prefix=SUPPLY_REASON_PREFIX,
)
return supply_items, normal_items
def run_account_sync_queue(
db: Session,
*,
@@ -320,12 +365,12 @@ def run_account_sync_queue(
"message": "账号同步槽已满,本轮跳过",
}
limit = max(1, min(int(batch_size), capacity))
items = claim_account_work(
supply_items, normal_items = claim_sync_work_batch(
db,
work_types=ACCOUNT_SYNC_WORK_TYPES,
limit=limit,
now=moment,
)
items = [*supply_items, *normal_items]
db.commit()
if not items:
return {
@@ -371,8 +416,11 @@ def run_account_sync_queue(
"success": success,
"failed": failed,
"skipped": skipped,
"supply": len(supply_items),
"order": len(normal_items),
"message": (
f"账号同步完成 {success} 条"
+ f"(供给 {len(supply_items)} / 订单 {len(normal_items)})"
+ (f",失败 {failed} 条" if failed else "")
+ (f",跳过 {skipped} 条" if skipped else "")
),