fix(douyu): 补全账号能力目录并回收失效租约
This commit is contained in:
@@ -5,10 +5,43 @@ from __future__ import annotations
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from ..models import AccountWorkItem
|
||||
from ..models import Account, AccountWorkItem
|
||||
|
||||
|
||||
def active_account_work_exists(*, work_type: str):
|
||||
"""SQL 子查询:同一账号已有同类型的待执行或运行中工作。"""
|
||||
return (
|
||||
select(AccountWorkItem.id)
|
||||
.where(
|
||||
AccountWorkItem.account_id == Account.id,
|
||||
AccountWorkItem.work_type == work_type,
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
)
|
||||
.exists()
|
||||
)
|
||||
|
||||
|
||||
def active_account_work_count(
|
||||
db: Session,
|
||||
*,
|
||||
work_types: tuple[str, ...],
|
||||
) -> int:
|
||||
"""当前账号同步队列中尚未结束的工作数。"""
|
||||
if not work_types:
|
||||
return 0
|
||||
return int(
|
||||
db.query(func.count(AccountWorkItem.id))
|
||||
.filter(
|
||||
AccountWorkItem.work_type.in_(work_types),
|
||||
AccountWorkItem.status.in_(("pending", "running")),
|
||||
)
|
||||
.scalar()
|
||||
or 0
|
||||
)
|
||||
|
||||
|
||||
def enqueue_account_work(
|
||||
@@ -247,6 +280,8 @@ def fail_account_work_type(
|
||||
|
||||
|
||||
__all__ = [
|
||||
"active_account_work_count",
|
||||
"active_account_work_exists",
|
||||
"claim_account_work",
|
||||
"complete_account_work",
|
||||
"complete_account_work_type",
|
||||
|
||||
Reference in New Issue
Block a user