feat(douyu): 接入重登工作队列与平台状态观测
This commit is contained in:
@@ -34,6 +34,7 @@ def enqueue_account_work(
|
||||
)
|
||||
if existing is not None:
|
||||
existing.priority = min(existing.priority, priority)
|
||||
existing.max_attempts = max(existing.max_attempts, max(1, int(max_attempts)))
|
||||
if due_at is not None and due_at < existing.due_at:
|
||||
existing.due_at = due_at
|
||||
db.flush()
|
||||
@@ -69,15 +70,23 @@ def claim_account_work(
|
||||
work_types: tuple[str, ...],
|
||||
limit: int = 10,
|
||||
now: datetime | None = None,
|
||||
account_ids: tuple[int, ...] | None = None,
|
||||
) -> list[AccountWorkItem]:
|
||||
moment = now or datetime.now(UTC)
|
||||
rows = (
|
||||
query = (
|
||||
db.query(AccountWorkItem)
|
||||
.filter(
|
||||
AccountWorkItem.work_type.in_(work_types),
|
||||
AccountWorkItem.status == "pending",
|
||||
AccountWorkItem.due_at <= moment,
|
||||
)
|
||||
)
|
||||
if account_ids is not None:
|
||||
if not account_ids:
|
||||
return []
|
||||
query = query.filter(AccountWorkItem.account_id.in_(account_ids))
|
||||
rows = (
|
||||
query
|
||||
.order_by(
|
||||
AccountWorkItem.priority.asc(),
|
||||
AccountWorkItem.due_at.asc(),
|
||||
@@ -179,11 +188,71 @@ def recover_running_account_work(
|
||||
return len(items)
|
||||
|
||||
|
||||
def reschedule_account_work_type(
|
||||
db: Session,
|
||||
*,
|
||||
account_id: int,
|
||||
work_type: str,
|
||||
due_at: datetime,
|
||||
reason: str = "",
|
||||
) -> int:
|
||||
"""把自动重登失败的工作项按账号退避时间重新排队。"""
|
||||
moment = (due_at if due_at.tzinfo else due_at.replace(tzinfo=UTC)).replace(
|
||||
microsecond=0
|
||||
)
|
||||
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:
|
||||
item.status = "pending"
|
||||
item.due_at = moment
|
||||
item.last_error = reason[:2000]
|
||||
item.updated_at = datetime.now(UTC)
|
||||
db.flush()
|
||||
return len(items)
|
||||
|
||||
|
||||
def fail_account_work_type(
|
||||
db: Session,
|
||||
*,
|
||||
account_id: int,
|
||||
work_type: str,
|
||||
reason: 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:
|
||||
item.status = "failed"
|
||||
item.finished_at = moment
|
||||
item.last_error = reason[:2000]
|
||||
item.updated_at = moment
|
||||
db.flush()
|
||||
return len(items)
|
||||
|
||||
|
||||
__all__ = [
|
||||
"claim_account_work",
|
||||
"complete_account_work",
|
||||
"complete_account_work_type",
|
||||
"enqueue_account_work",
|
||||
"fail_account_work",
|
||||
"fail_account_work_type",
|
||||
"recover_running_account_work",
|
||||
"reschedule_account_work_type",
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user