fix(automation): 修复账号供给补货队列饥饿
- 拆分订单补货和通用供给水位,供给保留独立 pending 配额 - 每批固定供给槽位,避免订单补货占满队列 - 只补覆盖/可兑换缺口,可分配缺口不再无效入队 - 重登工作超过 24h 自动转人工,清理待处理积压 - 补货阻塞告警和队列诊断信息 - 增加供给公平调度/过期清理回归测试
This commit is contained in:
@@ -13,6 +13,11 @@ from ..models import Account, AccountWorkItem
|
||||
|
||||
WORK_AGING_SECONDS = 30 * 60
|
||||
|
||||
#: 供给补货任务的 reason 前缀;队列靠它给通用供给保留固定处理配额。
|
||||
SUPPLY_REASON_PREFIX = "供给短缺:"
|
||||
#: 履约商品能力补货任务的 reason 前缀;队列把它归入订单补货一侧。
|
||||
ORDER_REFRESH_REASON_PREFIX = "待履约商品补货:"
|
||||
|
||||
|
||||
def active_account_work_exists(*, work_type: str):
|
||||
"""SQL 子查询:同一账号已有同类型的待执行或运行中工作。"""
|
||||
@@ -31,19 +36,23 @@ def active_account_work_count(
|
||||
db: Session,
|
||||
*,
|
||||
work_types: tuple[str, ...],
|
||||
reason_prefix: str | None = None,
|
||||
exclude_reason_prefix: str | None = None,
|
||||
) -> int:
|
||||
"""当前账号同步队列中尚未结束的工作数。"""
|
||||
"""当前账号同步队列中尚未结束的工作数(可按 reason 分类)。"""
|
||||
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
|
||||
query = db.query(func.count(AccountWorkItem.id)).filter(
|
||||
AccountWorkItem.work_type.in_(work_types),
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
)
|
||||
if reason_prefix:
|
||||
query = query.filter(AccountWorkItem.reason.like(f"{reason_prefix}%"))
|
||||
if exclude_reason_prefix:
|
||||
query = query.filter(
|
||||
~AccountWorkItem.reason.like(f"{exclude_reason_prefix}%")
|
||||
)
|
||||
return int(query.scalar() or 0)
|
||||
|
||||
|
||||
def enqueue_account_work(
|
||||
@@ -106,6 +115,8 @@ def claim_account_work(
|
||||
limit: int = 10,
|
||||
now: datetime | None = None,
|
||||
account_ids: tuple[int, ...] | None = None,
|
||||
reason_prefix: str | None = None,
|
||||
exclude_reason_prefix: str | None = None,
|
||||
) -> list[AccountWorkItem]:
|
||||
moment = now or datetime.now(UTC)
|
||||
query = (
|
||||
@@ -120,6 +131,12 @@ def claim_account_work(
|
||||
if not account_ids:
|
||||
return []
|
||||
query = query.filter(AccountWorkItem.account_id.in_(account_ids))
|
||||
if reason_prefix:
|
||||
query = query.filter(AccountWorkItem.reason.like(f"{reason_prefix}%"))
|
||||
if exclude_reason_prefix:
|
||||
query = query.filter(
|
||||
~AccountWorkItem.reason.like(f"{exclude_reason_prefix}%")
|
||||
)
|
||||
rows = (
|
||||
query
|
||||
.order_by(
|
||||
@@ -233,6 +250,52 @@ def recover_running_account_work(
|
||||
return len(items)
|
||||
|
||||
|
||||
def expire_stale_login_work(
|
||||
db: Session,
|
||||
*,
|
||||
now: datetime | None = None,
|
||||
ttl_seconds: int | None = None,
|
||||
) -> int:
|
||||
"""关闭长时间未执行的重登工作,避免暂停作业后永久挂在待处理。
|
||||
|
||||
重登作业因 100% 失败自动暂停时,队列里会残留 pending 的
|
||||
``login_relogin``;它们既不会执行,也会在页面上冒充“待处理”。超过 TTL
|
||||
后置为 failed,转人工处理,不再占用队列观测。
|
||||
"""
|
||||
import os
|
||||
|
||||
moment = now or datetime.now(UTC)
|
||||
if ttl_seconds is None:
|
||||
try:
|
||||
ttl_seconds = max(
|
||||
60,
|
||||
int(os.getenv("ACCOUNT_RELOGIN_WORK_TTL_SECONDS", "86400")),
|
||||
)
|
||||
except ValueError:
|
||||
ttl_seconds = 86400
|
||||
cutoff = (moment - timedelta(seconds=ttl_seconds)).replace(microsecond=0)
|
||||
items = (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(
|
||||
AccountWorkItem.work_type == "login_relogin",
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
AccountWorkItem.due_at < cutoff,
|
||||
)
|
||||
.all()
|
||||
)
|
||||
for item in items:
|
||||
item.status = "failed"
|
||||
item.finished_at = moment
|
||||
item.last_error = (
|
||||
f"重登工作超过 {ttl_seconds // 3600} 小时未执行"
|
||||
"(重登作业可能已暂停),已转人工处理"
|
||||
)
|
||||
item.updated_at = moment
|
||||
if items:
|
||||
db.flush()
|
||||
return len(items)
|
||||
|
||||
|
||||
def reschedule_account_work_type(
|
||||
db: Session,
|
||||
*,
|
||||
|
||||
Reference in New Issue
Block a user