perf(douyu): 拆分任务槽并改造绑定扫码调度

This commit is contained in:
yml
2026-11-20 22:03:20 +08:00
parent 8869e427d2
commit 071d45ad1d
40 changed files with 1635 additions and 245 deletions
+12
View File
@@ -92,12 +92,24 @@ def list_jobs(
# 巡检与重登是两个作业,但通过数据库联动:巡检把行标成 invalid,重登作业的
# 候选条件正是这个字段。这里把「当前有多少条等着重登」一并返回,页面才能把
# 这层上下游关系显示出来,而不是让人以为两个开关互不相干。
from ..database import worker_engine
from ..services.automation.jobs.ck_relogin import count_pending_relogin
from ..services.binding_poll_scheduler import (
scheduler_status as binding_poll_status,
)
from ..services.task_runtime import lane_stats
pending_relogin = count_pending_relogin(db)
return {
"scheduler": scheduler_status(),
"runtime": {
"task_lanes": lane_stats(),
"worker_db_pool": getattr(worker_engine.pool, "status", lambda: "")()
if callable(getattr(worker_engine.pool, "status", None))
else "",
"binding_poll": binding_poll_status(),
},
"pending_relogin": pending_relogin,
"jobs": [
{
+2 -2
View File
@@ -27,7 +27,7 @@ from fastapi.responses import StreamingResponse
from sqlalchemy import case, func, or_
from sqlalchemy.orm import Session, defer, joinedload
from ..database import SessionLocal, get_db
from ..database import WorkerSessionLocal, get_db
from ..deps import get_current_user, require_permission
from ..models import Account, AuditLog, LoginTask, User
from ..models import ProxyConfig as ProxyConfigModel
@@ -649,7 +649,7 @@ def _start_relogin_accounts(
)
proxy = db.query(ProxyConfigModel).first()
thread_db = SessionLocal()
thread_db = WorkerSessionLocal()
max_login_retries, max_total_time = get_relogin_limits()
runner = LoginBatchRunner(
db=thread_db,
+2 -2
View File
@@ -28,7 +28,7 @@ from core.douyu import (
FishFinRechargeConfigError,
)
from ..database import SessionLocal, get_db
from ..database import WorkerSessionLocal, get_db
from ..deps import authenticate_websocket, get_current_user, require_permission
from ..models import (
Account,
@@ -1321,7 +1321,7 @@ async def ws_douyu_logs(websocket: WebSocket, batch_id: str):
if not user_has_permission(user, "douyu:task"):
await websocket.close(code=1008, reason="无权限")
return
db = SessionLocal()
db = WorkerSessionLocal()
try:
_require_batch_owner(db, user, batch_id)
except HTTPException:
+1 -1
View File
@@ -45,7 +45,7 @@ from core.huya.device_profile import (
)
from core.sms_provider import parse_sms_lines
from ...database import SessionLocal, get_db
from ...database import get_db
from ...deps import authenticate_websocket, get_current_user, require_permission
from ...models import (
HuyaAccount,
+3 -2
View File
@@ -5,6 +5,7 @@ from fastapi import APIRouter, Response
from core.huya.cookie_utils import cookie_value
from core.huya.http_client import HuyaHttpClient
from ...database import WorkerSessionLocal
from ...schemas import HuyaWorkbenchTransferToCollab
from ...services.huya_points_center import (
fetch_points_center_catalog,
@@ -339,7 +340,7 @@ async def create_task_batch(
log_queue = asyncio.Queue()
loop = asyncio.get_running_loop()
thread_db = SessionLocal()
thread_db = WorkerSessionLocal()
runner = HuyaBatchRunner(
db=thread_db,
batch_id=batch_id,
@@ -494,7 +495,7 @@ async def ws_huya_logs(websocket: WebSocket, batch_id: str):
if not _has_huya_perm(user, "huya:task"):
await websocket.close(code=1008, reason="无权限")
return
db = SessionLocal()
db = WorkerSessionLocal()
try:
_require_huya_batch_owner(db, user, batch_id)
except HTTPException:
+3 -3
View File
@@ -17,7 +17,7 @@ from fastapi.responses import StreamingResponse
from sqlalchemy import and_, func, or_
from sqlalchemy.orm import Session
from ..database import SessionLocal, get_db
from ..database import WorkerSessionLocal, get_db
from ..deps import authenticate_websocket, get_current_user, require_permission
from ..models import Account, LoginTask, User
from ..models import ProxyConfig as ProxyConfigModel
@@ -182,7 +182,7 @@ async def create_batch(
loop = asyncio.get_running_loop()
# 创建执行器(用独立的 DB 会话,因为在线程中运行)
thread_db = SessionLocal()
thread_db = WorkerSessionLocal()
runner = LoginBatchRunner(
db=thread_db,
account_ids=valid_ids,
@@ -687,7 +687,7 @@ async def ws_login_logs(websocket: WebSocket, batch_id: str):
# 归属校验:非 view_all 只能订阅自己创建的批次(与虎牙批次一致)。
if not user_has_permission(user, "login:view_all"):
check_db = SessionLocal()
check_db = WorkerSessionLocal()
try:
owned = (
check_db.query(LoginTask.id)
@@ -9,6 +9,7 @@ from hmac import compare_digest
from typing import Any
from fastapi import APIRouter, Depends, Header, HTTPException, Request
from fastapi.responses import JSONResponse
from sqlalchemy.orm import Session
from starlette.requests import ClientDisconnect
@@ -132,12 +133,16 @@ async def start_customer_binding(
task_id: int,
req: PlatformFulfillmentCustomerBindingAction,
db: Session = Depends(get_db),
) -> dict[str, Any]:
return await service.customer_start_binding(
) -> JSONResponse:
payload = await service.customer_start_binding(
db,
task_id=task_id,
force_refresh=req.force_refresh,
)
return JSONResponse(
status_code=202 if payload.get("accepted") else 200,
content=payload,
)
@router.get(