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
@@ -9,7 +9,7 @@
from __future__ import annotations
from concurrent.futures import ThreadPoolExecutor, as_completed
from concurrent.futures import as_completed
from datetime import UTC, datetime, timedelta
from typing import Any
@@ -34,6 +34,7 @@ from ...douyu_account_probe import (
probe_session,
)
from ...douyu_service import cookie_holding_filter, cookie_operable_filter
from ...task_runtime import TaskLane, submit
JOB_KEY = "ck_health"
FLUSH_EVERY = 25
@@ -146,11 +147,14 @@ def run_ck_health(db: Session, job: AutomationJob) -> dict[str, Any]:
results: dict[int, dict[str, Any]] = {}
concurrency = max(min(int(job.concurrency or 1), 16), 1)
with ThreadPoolExecutor(max_workers=concurrency) as pool:
futures = {pool.submit(_detect, account): account for account in accounts}
for future in as_completed(futures):
account = futures[future]
results[account.id] = future.result()
futures = {}
for account in accounts:
future = submit(TaskLane.ACCOUNT_CHECK, _detect, account)
if future is not None:
futures[future] = account
for future in as_completed(futures):
account = futures[future]
results[account.id] = future.result()
# 只写检测结果三件套;cookie / status / attempt_* 一律不动。
valid = invalid = errors = 0
@@ -20,7 +20,7 @@ from typing import Any
from sqlalchemy import func, or_
from sqlalchemy.orm import Session
from ....database import SessionLocal
from ....database import WorkerSessionLocal
from ....models import Account, AutomationJob, LoginTask, User
from ....models import ProxyConfig as ProxyConfigModel
from ...account_events_service import EVENT_RELOGIN_FAILED, emit
@@ -148,7 +148,7 @@ def settle_fresh_results(
退避时间,其中 2 条还被标成「极验」进了待人工队列。SQLite 的读一致性与 MySQL
不同,这类问题在 SQLite 上复现不出来。
"""
session = SessionLocal()
session = WorkerSessionLocal()
try:
fresh, order = _load_batch_state(session, batch_id)
counters = settle_results(
@@ -241,7 +241,7 @@ def run_ck_relogin(db: Session, job: AutomationJob) -> dict[str, Any]:
_, max_total_time = get_relogin_limits()
# 批次用独立会话(runner 会在自己的线程/会话里写库),跑完再回来读结果。
runner_db = SessionLocal()
runner_db = WorkerSessionLocal()
runner = LoginBatchRunner(
db=runner_db,
account_ids=account_ids,
+6 -6
View File
@@ -24,7 +24,7 @@ from typing import Any
from loguru import logger
from sqlalchemy.orm import Session
from ...database import SessionLocal
from ...database import WorkerSessionLocal
from ...models import AutomationJob, AutomationRun
# 调度轮询间隔,也是作业的运行节奏:每个 tick 跑一轮。
@@ -71,7 +71,7 @@ _REGISTRY: dict[str, JobSpec] = {}
#: 同时最多几个作业在跑。作业之间互不依赖,并行是为了**慢作业不挡住快作业**
#: (以前串行:一个作业跑 4 分钟,CK 巡检就得等 4 分钟)。
_JOB_MAX_WORKERS = 4
_JOB_MAX_WORKERS = 2
#: 运行间隔的下限:太短会变成忙轮询,没有意义
MIN_RUN_INTERVAL_SECONDS = 30
@@ -190,7 +190,7 @@ def _run_job_isolated(job_key: str) -> dict[str, Any] | None:
多个作业共用一个 session 会互相踩事务(而他们现在是真的并行了)。
重新按 key 取行,是因为调度线程里的那行可能已经过期。
"""
db = SessionLocal()
db = WorkerSessionLocal()
try:
job = db.query(AutomationJob).filter(AutomationJob.key == job_key).first()
spec = _REGISTRY.get(job_key)
@@ -337,7 +337,7 @@ def _run_one(db: Session, job: AutomationJob, spec: JobSpec) -> dict[str, Any]:
db.rollback()
logger.exception("自动化作业 {} 提交状态失败,尝试独立连接回写", job.key)
try:
rescue_db = SessionLocal()
rescue_db = WorkerSessionLocal()
try:
r = (
rescue_db.query(AutomationRun)
@@ -412,7 +412,7 @@ def run_job_now(db: Session, key: str) -> dict[str, Any]:
def _loop() -> None:
db = SessionLocal()
db = WorkerSessionLocal()
try:
ensure_jobs(db)
except Exception: # noqa: BLE001 初始化失败不应让线程静默退出
@@ -424,7 +424,7 @@ def _loop() -> None:
"自动化调度器已启动:心跳 {}s,作业周期 {}s", TICK_SECONDS, JOB_INTERVAL_SECONDS
)
while not _stop_event.wait(TICK_SECONDS):
db = SessionLocal()
db = WorkerSessionLocal()
try:
# wait=False:派发完就返回 —— tick 不再被慢作业拖住
run_due_jobs(db, wait=False)