fix(automation): 修复账号供给补货队列饥饿
- 拆分订单补货和通用供给水位,供给保留独立 pending 配额 - 每批固定供给槽位,避免订单补货占满队列 - 只补覆盖/可兑换缺口,可分配缺口不再无效入队 - 重登工作超过 24h 自动转人工,清理待处理积压 - 补货阻塞告警和队列诊断信息 - 增加供给公平调度/过期清理回归测试
This commit is contained in:
@@ -21,6 +21,7 @@ from ....models import (
|
||||
)
|
||||
from ...account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, WORK_STOREHOME_SYNC
|
||||
from ...account_work_queue import (
|
||||
SUPPLY_REASON_PREFIX,
|
||||
active_account_work_count,
|
||||
enqueue_account_work,
|
||||
)
|
||||
@@ -66,13 +67,17 @@ def _pending_demand_rows(
|
||||
return list(db.execute(statement).all())
|
||||
|
||||
|
||||
def _queue_target_pending() -> int:
|
||||
def _order_queue_target_pending() -> int:
|
||||
"""订单能力补货自己的水位;通用供给有自己的保留水位。"""
|
||||
import os
|
||||
|
||||
raw = os.getenv("ACCOUNT_ORDER_QUEUE_TARGET_PENDING")
|
||||
if raw is None:
|
||||
raw = os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "70")
|
||||
try:
|
||||
return max(1, int(os.getenv("ACCOUNT_SYNC_QUEUE_TARGET_PENDING", "60")))
|
||||
return max(1, int(raw))
|
||||
except ValueError:
|
||||
return 60
|
||||
return 70
|
||||
|
||||
|
||||
def _build_select_query(
|
||||
@@ -130,8 +135,9 @@ def run_account_capability_refresh(
|
||||
pending = active_account_work_count(
|
||||
db,
|
||||
work_types=ACCOUNT_SYNC_WORK_TYPES,
|
||||
exclude_reason_prefix=SUPPLY_REASON_PREFIX,
|
||||
)
|
||||
headroom = max(0, _queue_target_pending() - pending)
|
||||
headroom = max(0, _order_queue_target_pending() - pending)
|
||||
if headroom <= 0:
|
||||
return {
|
||||
"scanned": 0,
|
||||
@@ -179,6 +185,7 @@ def run_account_capability_refresh(
|
||||
"detail": {
|
||||
"queued": queued,
|
||||
"pending": pending,
|
||||
"order_cap": _order_queue_target_pending(),
|
||||
"headroom": headroom,
|
||||
"commodities": commodity_ids[:20],
|
||||
},
|
||||
|
||||
@@ -24,20 +24,43 @@ def run_account_supply_replenishment(
|
||||
db,
|
||||
max_per_target=max(1, int(job.batch_size or 50)),
|
||||
)
|
||||
blocked = bool(
|
||||
result.refreshable_shortages
|
||||
and result.queued == 0
|
||||
and result.pending_supply == 0
|
||||
and result.total_headroom == 0
|
||||
)
|
||||
message = (
|
||||
f"评估 {result.targets} 个目标;"
|
||||
f"覆盖/可兑换短缺 {result.refreshable_shortages} 个,"
|
||||
f"仅可分配短缺 {result.capacity_shortages} 个(需容量);"
|
||||
f"队列待处理 {result.pending_sync}"
|
||||
f"(供给 {result.pending_supply}/{result.supply_cap},"
|
||||
f"总量 {result.pending_sync}/{result.total_cap});"
|
||||
f"本轮新排队 {result.queued}"
|
||||
if result.targets
|
||||
else "未配置账号供给目标"
|
||||
)
|
||||
if blocked:
|
||||
message += ";供给补货被订单队列阻塞"
|
||||
return {
|
||||
"scanned": result.targets,
|
||||
"success": result.queued,
|
||||
"invalid": 0,
|
||||
"skipped": max(0, result.targets - result.shortages),
|
||||
"message": (
|
||||
f"评估 {result.targets} 个供给目标,"
|
||||
f"短缺 {result.shortages} 个,新排队 {result.queued} 个账号同步"
|
||||
if result.targets
|
||||
else "未配置账号供给目标"
|
||||
),
|
||||
"skipped": result.capacity_shortages,
|
||||
"message": message,
|
||||
"detail": {
|
||||
"shortages": result.shortages,
|
||||
"refreshable_shortages": result.refreshable_shortages,
|
||||
"capacity_shortages": result.capacity_shortages,
|
||||
"queued": result.queued,
|
||||
"pending_sync": result.pending_sync,
|
||||
"pending_supply": result.pending_supply,
|
||||
"total_headroom": result.total_headroom,
|
||||
"supply_headroom": result.supply_headroom,
|
||||
"total_cap": result.total_cap,
|
||||
"supply_cap": result.supply_cap,
|
||||
"blocked": blocked,
|
||||
"snapshots": len(snapshots),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ from sqlalchemy.orm import Session
|
||||
|
||||
from ....models import AccountWorkItem, AutomationJob
|
||||
from ...account_sync_runner import ACCOUNT_SYNC_WORK_TYPES, run_account_sync_queue
|
||||
from ...account_work_queue import expire_stale_login_work
|
||||
|
||||
JOB_KEY = "account_sync_queue"
|
||||
|
||||
@@ -31,12 +32,22 @@ def run_account_sync_queue_job(
|
||||
db: Session,
|
||||
job: AutomationJob,
|
||||
) -> dict[str, Any]:
|
||||
moment = datetime.now(UTC)
|
||||
expired = expire_stale_login_work(db, now=moment)
|
||||
if expired:
|
||||
db.commit()
|
||||
result = run_account_sync_queue(
|
||||
db,
|
||||
batch_size=max(1, int(job.batch_size or 20)),
|
||||
)
|
||||
if result.get("scanned"):
|
||||
result["remaining"] = count_pending_work(db)
|
||||
if expired:
|
||||
result["expired_login"] = expired
|
||||
result["message"] = (
|
||||
f"{result.get('message') or '账号同步完成'},"
|
||||
f"过期重登转人工 {expired} 条"
|
||||
)
|
||||
return result
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user