fix: 采集账号失败就换号,都不可用时任务留待再试

This commit is contained in:
2026-11-10 11:36:58 +08:00
parent 9309d25420
commit 0fab517f2e
3 changed files with 118 additions and 38 deletions
+12 -3
View File
@@ -12,7 +12,14 @@ from app.models import NqiAccount, utcnow
_lock = threading.Lock()
def borrow_account() -> tuple[int, list] | None:
class AccountUnavailable(Exception):
def __init__(self, message: str, *, invalid: bool) -> None:
super().__init__(message)
self.invalid = invalid
def borrow_account(exclude: set[int] | None = None) -> tuple[int, list] | None:
skipped = exclude or set()
with _lock:
with session_scope() as session:
rows = session.scalars(
@@ -25,7 +32,7 @@ def borrow_account() -> tuple[int, list] | None:
NqiAccount.id,
)
).all()
account = next((row for row in rows if row.cookies), None)
account = next((row for row in rows if row.id not in skipped and row.cookies), None)
if account is None:
return None
account.active_tasks += 1
@@ -35,13 +42,15 @@ def borrow_account() -> tuple[int, list] | None:
return account.id, list(account.cookies or [])
def release_account(account_id: int) -> None:
def release_account(account_id: int, *, invalid: bool = False) -> None:
with _lock:
with session_scope() as session:
account = session.get(NqiAccount, account_id)
if account is None:
return
account.active_tasks = max(0, account.active_tasks - 1)
if invalid:
account.credential_valid = False
def reset_active_tasks() -> None: