perf(automation): 持久队列驱动并优化履约热路径

This commit is contained in:
yml
2026-11-23 15:07:31 +08:00
parent 11c2a0dba7
commit 2549511598
31 changed files with 1658 additions and 235 deletions
+13 -1
View File
@@ -5,12 +5,14 @@ from __future__ import annotations
from datetime import UTC, datetime, timedelta
from typing import Any
from sqlalchemy import func, select
from sqlalchemy import case, func, select
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import Session
from ..models import Account, AccountWorkItem
WORK_AGING_SECONDS = 30 * 60
def active_account_work_exists(*, work_type: str):
"""SQL 子查询:同一账号已有同类型的待执行或运行中工作。"""
@@ -121,6 +123,16 @@ def claim_account_work(
rows = (
query
.order_by(
# 到期超过 30 分钟的工作优先出队,避免固定业务优先级造成永久饥饿。
# due_at 是工作项真正可执行的起点;未来重试项不会命中这个分支。
case(
(
AccountWorkItem.due_at
<= moment - timedelta(seconds=WORK_AGING_SECONDS),
0,
),
else_=1,
),
AccountWorkItem.priority.asc(),
AccountWorkItem.due_at.asc(),
AccountWorkItem.id.asc(),