48 lines
1.1 KiB
Python
48 lines
1.1 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
|
|
|
|
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]:
|
|
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)
|
|
return result
|
|
|
|
|
|
__all__ = [
|
|
"JOB_KEY",
|
|
"count_pending_work",
|
|
"run_account_sync_queue_job",
|
|
]
|