feat(automation): 持久执行队列与重复充值确认
This commit is contained in:
@@ -18,7 +18,7 @@ CK 从 `login_tasks.cookie` 迁到 `accounts.cookie`,因此本页面的**记
|
||||
import csv
|
||||
import hashlib
|
||||
import io
|
||||
import threading
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from datetime import UTC, datetime
|
||||
|
||||
@@ -27,11 +27,10 @@ from fastapi.responses import StreamingResponse
|
||||
from sqlalchemy import case, func, or_
|
||||
from sqlalchemy.orm import Session, defer, joinedload
|
||||
|
||||
from ..database import WorkerSessionLocal, get_db
|
||||
from ..database import get_db
|
||||
from ..deps import get_current_user, require_permission
|
||||
from ..models import Account, AuditLog, LoginTask, User
|
||||
from ..models import ProxyConfig as ProxyConfigModel
|
||||
from ..permissions import get_user_permissions, user_has_permission
|
||||
from ..permissions import user_has_permission
|
||||
from ..schemas import CookieReloginRequest
|
||||
from ..services.account_lifecycle import on_ck_cleared
|
||||
from ..services.cookie_check_service import check_douyu_cookie
|
||||
@@ -41,7 +40,15 @@ from ..services.douyu_service import (
|
||||
cookie_holding_filter,
|
||||
cookie_operable_filter,
|
||||
)
|
||||
from ..services.login_service import BatchRegistry, LoginBatchRunner, get_relogin_limits
|
||||
from ..services.execution_jobs import (
|
||||
JOB_RELOGIN_BATCH,
|
||||
LANE_AUTH,
|
||||
TERMINAL_STATUSES,
|
||||
enqueue_execution_job,
|
||||
get_execution_job,
|
||||
request_execution_cancel,
|
||||
)
|
||||
from ..services.login_service import BatchRegistry, get_relogin_limits
|
||||
|
||||
|
||||
def _fmt_dt(dt) -> str | None:
|
||||
@@ -648,23 +655,26 @@ def _start_relogin_accounts(
|
||||
status_code=400, detail="没有可重登的账号,请检查账号凭据或当前重登状态"
|
||||
)
|
||||
|
||||
proxy = db.query(ProxyConfigModel).first()
|
||||
thread_db = WorkerSessionLocal()
|
||||
max_login_retries, max_total_time = get_relogin_limits()
|
||||
runner = LoginBatchRunner(
|
||||
db=thread_db,
|
||||
account_ids=account_ids,
|
||||
created_by=current.id,
|
||||
creator_permissions=get_user_permissions(current),
|
||||
max_login_retries=max_login_retries,
|
||||
max_total_time=max_total_time,
|
||||
proxy_config=proxy,
|
||||
log_queue=None,
|
||||
loop=None,
|
||||
concurrency=3,
|
||||
mode="relogin",
|
||||
batch_id = uuid.uuid4().hex[:12]
|
||||
enqueue_execution_job(
|
||||
db,
|
||||
job_type=JOB_RELOGIN_BATCH,
|
||||
lane_scope=LANE_AUTH,
|
||||
batch_id=batch_id,
|
||||
payload={
|
||||
"account_ids": account_ids,
|
||||
"max_login_retries": max_login_retries,
|
||||
"max_total_time": max_total_time,
|
||||
"concurrency": 3,
|
||||
"api_strategy": "wgapi",
|
||||
"mode": "relogin",
|
||||
"trigger_source": "manual",
|
||||
},
|
||||
created_by=int(current.id),
|
||||
priority=15,
|
||||
max_attempts=2,
|
||||
)
|
||||
batch_id = runner.batch_id
|
||||
db.add(
|
||||
AuditLog(
|
||||
user_id=current.id,
|
||||
@@ -675,21 +685,6 @@ def _start_relogin_accounts(
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
cookie_relogin_registry.register(batch_id, None, None, runner, owner_id=current.id)
|
||||
|
||||
def run_relogin_batch():
|
||||
try:
|
||||
runner.run()
|
||||
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
|
||||
runner._push_log("error", f"批量重新登录异常: {exc}")
|
||||
message = f"重新登录异常: {exc}(旧 Cookie 已保留)"
|
||||
for account_id in account_ids:
|
||||
runner._mark_account_relogin_stopped(account_id, message)
|
||||
runner.db.close()
|
||||
finally:
|
||||
cookie_relogin_registry.pop(batch_id)
|
||||
|
||||
threading.Thread(target=run_relogin_batch, daemon=True).start()
|
||||
return {
|
||||
"batch_id": batch_id,
|
||||
"count": len(account_ids),
|
||||
@@ -708,15 +703,24 @@ def stop_cookie_relogin(
|
||||
_require_cookie_operation_perm(current)
|
||||
batch = cookie_relogin_registry.get(batch_id)
|
||||
if not batch:
|
||||
raise HTTPException(
|
||||
status_code=404, detail="重登批次不存在、已结束或服务已重启"
|
||||
)
|
||||
if batch.get("owner_id") != current.id and not user_has_permission(
|
||||
current, "login:view_all"
|
||||
):
|
||||
raise HTTPException(status_code=403, detail="无权限停止该重登批次")
|
||||
|
||||
batch["runner"].stop()
|
||||
job = get_execution_job(db, batch_id=batch_id)
|
||||
if job is None:
|
||||
raise HTTPException(
|
||||
status_code=404, detail="重登批次不存在、已结束或服务已重启"
|
||||
)
|
||||
if job.created_by != current.id and not user_has_permission(
|
||||
current, "login:view_all"
|
||||
):
|
||||
raise HTTPException(status_code=403, detail="无权限停止该重登批次")
|
||||
if job.status in TERMINAL_STATUSES:
|
||||
raise HTTPException(status_code=404, detail="重登批次已结束")
|
||||
request_execution_cancel(db, batch_id=batch_id)
|
||||
else:
|
||||
if batch.get("owner_id") != current.id and not user_has_permission(
|
||||
current, "login:view_all"
|
||||
):
|
||||
raise HTTPException(status_code=403, detail="无权限停止该重登批次")
|
||||
batch["runner"].stop()
|
||||
db.add(
|
||||
AuditLog(
|
||||
user_id=current.id,
|
||||
|
||||
Reference in New Issue
Block a user