- 拆分订单补货和通用供给水位,供给保留独立 pending 配额 - 每批固定供给槽位,避免订单补货占满队列 - 只补覆盖/可兑换缺口,可分配缺口不再无效入队 - 重登工作超过 24h 自动转人工,清理待处理积压 - 补货阻塞告警和队列诊断信息 - 增加供给公平调度/过期清理回归测试
59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
"""账号内部同步队列作业。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import UTC, datetime
|
||
from typing import Any
|
||
|
||
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"
|
||
|
||
|
||
def count_pending_work(db: Session, *, now: datetime | None = None) -> int:
|
||
moment = now or datetime.now(UTC)
|
||
return int(
|
||
db.query(AccountWorkItem.id)
|
||
.filter(
|
||
AccountWorkItem.work_type.in_(ACCOUNT_SYNC_WORK_TYPES),
|
||
AccountWorkItem.status == "pending",
|
||
AccountWorkItem.due_at <= moment,
|
||
)
|
||
.count()
|
||
or 0
|
||
)
|
||
|
||
|
||
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
|
||
|
||
|
||
__all__ = [
|
||
"JOB_KEY",
|
||
"count_pending_work",
|
||
"run_account_sync_queue_job",
|
||
]
|