163 lines
4.2 KiB
Python
163 lines
4.2 KiB
Python
"""账号内部维护工作队列。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime, timedelta
|
|
from typing import Any
|
|
|
|
from sqlalchemy.exc import IntegrityError
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..models import AccountWorkItem
|
|
|
|
|
|
def enqueue_account_work(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
work_type: str,
|
|
priority: int = 100,
|
|
due_at: datetime | None = None,
|
|
max_attempts: int = 3,
|
|
reason: str = "",
|
|
payload: dict[str, Any] | None = None,
|
|
) -> AccountWorkItem:
|
|
dedupe_key = f"{work_type}:{account_id}"
|
|
existing = (
|
|
db.query(AccountWorkItem)
|
|
.filter(
|
|
AccountWorkItem.account_id == account_id,
|
|
AccountWorkItem.work_type == work_type,
|
|
AccountWorkItem.status.in_(("pending", "running")),
|
|
)
|
|
.first()
|
|
)
|
|
if existing is not None:
|
|
existing.priority = min(existing.priority, priority)
|
|
if due_at is not None and due_at < existing.due_at:
|
|
existing.due_at = due_at
|
|
db.flush()
|
|
return existing
|
|
item = AccountWorkItem(
|
|
account_id=account_id,
|
|
work_type=work_type[:32],
|
|
priority=int(priority),
|
|
due_at=due_at or datetime.now(UTC),
|
|
max_attempts=max(1, int(max_attempts)),
|
|
dedupe_key=dedupe_key[:191],
|
|
reason=reason[:255],
|
|
payload_json=payload,
|
|
)
|
|
try:
|
|
with db.begin_nested():
|
|
db.add(item)
|
|
db.flush()
|
|
return item
|
|
except IntegrityError:
|
|
return (
|
|
db.query(AccountWorkItem)
|
|
.filter(AccountWorkItem.active_dedupe_key == dedupe_key[:191])
|
|
.one()
|
|
)
|
|
|
|
|
|
def claim_account_work(
|
|
db: Session,
|
|
*,
|
|
work_types: tuple[str, ...],
|
|
limit: int = 10,
|
|
now: datetime | None = None,
|
|
) -> list[AccountWorkItem]:
|
|
moment = now or datetime.now(UTC)
|
|
rows = (
|
|
db.query(AccountWorkItem)
|
|
.filter(
|
|
AccountWorkItem.work_type.in_(work_types),
|
|
AccountWorkItem.status == "pending",
|
|
AccountWorkItem.due_at <= moment,
|
|
)
|
|
.order_by(
|
|
AccountWorkItem.priority.asc(),
|
|
AccountWorkItem.due_at.asc(),
|
|
AccountWorkItem.id.asc(),
|
|
)
|
|
.with_for_update(skip_locked=True)
|
|
.limit(max(1, int(limit)))
|
|
.all()
|
|
)
|
|
for row in rows:
|
|
row.status = "running"
|
|
row.started_at = moment
|
|
row.attempt_count = int(row.attempt_count or 0) + 1
|
|
row.updated_at = moment
|
|
if rows:
|
|
db.flush()
|
|
return rows
|
|
|
|
|
|
def complete_account_work(
|
|
db: Session,
|
|
item: AccountWorkItem,
|
|
*,
|
|
now: datetime | None = None,
|
|
) -> None:
|
|
moment = now or datetime.now(UTC)
|
|
item.status = "success"
|
|
item.finished_at = moment
|
|
item.last_error = ""
|
|
item.updated_at = moment
|
|
db.flush()
|
|
|
|
|
|
def complete_account_work_type(
|
|
db: Session,
|
|
*,
|
|
account_id: int,
|
|
work_type: str,
|
|
now: datetime | None = None,
|
|
) -> int:
|
|
"""完成某个账号指定类型的待办;成功后旧待办不再阻塞后续工作。"""
|
|
moment = now or datetime.now(UTC)
|
|
items = (
|
|
db.query(AccountWorkItem)
|
|
.filter(
|
|
AccountWorkItem.account_id == account_id,
|
|
AccountWorkItem.work_type == work_type,
|
|
AccountWorkItem.status.in_(("pending", "running")),
|
|
)
|
|
.all()
|
|
)
|
|
for item in items:
|
|
complete_account_work(db, item, now=moment)
|
|
return len(items)
|
|
|
|
|
|
def fail_account_work(
|
|
db: Session,
|
|
item: AccountWorkItem,
|
|
*,
|
|
error: BaseException | str,
|
|
retry_after_seconds: int = 300,
|
|
now: datetime | None = None,
|
|
) -> None:
|
|
moment = now or datetime.now(UTC)
|
|
message = str(error)[:2000]
|
|
item.last_error = message
|
|
item.updated_at = moment
|
|
if int(item.attempt_count or 0) >= int(item.max_attempts or 1):
|
|
item.status = "failed"
|
|
item.finished_at = moment
|
|
else:
|
|
item.status = "pending"
|
|
item.due_at = moment + timedelta(seconds=max(1, retry_after_seconds))
|
|
db.flush()
|
|
|
|
|
|
__all__ = [
|
|
"claim_account_work",
|
|
"complete_account_work",
|
|
"complete_account_work_type",
|
|
"enqueue_account_work",
|
|
"fail_account_work",
|
|
]
|