perf(runtime): 修复 WebSocket 中间件与慢查询

This commit is contained in:
yml
2026-11-23 16:55:00 +08:00
parent 38ae5c867c
commit b30b16294f
6 changed files with 145 additions and 36 deletions
+11 -7
View File
@@ -651,11 +651,11 @@ def _sync_candidate_query(
~state_exists,
~active_account_work_exists(work_type=WORK_STOREHOME_SYNC),
assigned_to_pool_filter(),
~active_lease_exists(),
~active_collab_order_exists(),
role_change_ready_filter(now),
)
.order_by(
case((active_lease_exists(), 1), else_=0),
case((active_collab_order_exists(), 1), else_=0),
case((role_change_ready_filter(now), 0), else_=1),
case((Account.role_bind_status == "unbound", 0), else_=1),
Account.overview_queried_at.is_(None).desc(),
Account.overview_queried_at.asc(),
@@ -711,19 +711,23 @@ def enqueue_supply_replenishment(
inventory.allocatable_shortage,
)
limit = max(1, min(int(max_per_target), missing, headroom))
accounts = (
account_ids = [
int(account_id)
for (account_id,) in (
_sync_candidate_query(
db,
target=target,
now=moment,
)
.with_entities(Account.id)
.limit(limit)
.all()
)
for account in accounts:
)
]
for account_id in account_ids:
enqueue_account_work(
db,
account_id=int(account.id),
account_id=account_id,
work_type=WORK_STOREHOME_SYNC,
priority=35,
due_at=moment,
@@ -80,6 +80,16 @@ DEFAULT_MAX_BATCHES = 50
#: 在途任务绝不可删:批次线程正拿着这些行。
IN_FLIGHT_STATUSES = ("planned", "pending", "running")
#: 只清理明确结束的状态。未知/新增状态默认保留,避免未来状态机扩展后被误删。
TERMINAL_STATUSES = (
"success",
"failed",
"error",
"stopped",
"timeout",
"cancelled",
)
def _load_config(job: AutomationJob) -> dict[str, Any]:
config = job.config
@@ -127,7 +137,7 @@ def _delete_batch(
)
query = db.query(DouyuTask.id, DouyuTask.created_at).filter(
type_filter,
~DouyuTask.status.in_(IN_FLIGHT_STATUSES),
DouyuTask.status.in_(TERMINAL_STATUSES),
DouyuTask.created_at < cutoff,
)
if cursor_created_at is not None: