修复(执行队列): 登录批次不再把斗鱼履约批次饿死

现象:新建斗鱼任务报「斗鱼任务繁忙:当前有 10 个批次运行中,上限 10 个」。

根因:登录任务页(JOB_LOGIN_BATCH)和 CK 重登页(JOB_RELOGIN_BATCH)走
execution_jobs 的 auth lane,斗鱼履约/绑定批次(JOB_DOUYU_BATCH)走 fulfillment
lane,但两条 lane 共用同一个 ThreadPoolExecutor(EXECUTION_WORKER_MAX_JOBS=2)。
斗鱼邮箱验证码是攒批投递的,等一封验证码最长 25 分钟/账号,登录批次长时间持槽,
履约批次被挤到排队,堆满 DOUYU_MAX_ACTIVE_BATCHES(10) 后新建任务直接被挡。

改动:

- 执行 worker 改为 lane 隔离:每条 lane 独立线程池,总槽位不变,但 auth 最多
  占 total-1、fulfillment 最多占 total,且履约优先领取 —— 登录再慢也饿不死履约
- 单个登录批次最多占槽 LOGIN_BATCH_MAX_SECONDS(默认 1800 秒):每个账号开始
  执行前检查批次时长,超时后本轮剩余账号记「未执行」(不是失败),留待下一轮
- 新增 EXECUTION_WORKER_AUTH_JOBS / EXECUTION_WORKER_FULFILLMENT_JOBS 旋钮

验证:1732 passed / 6 skipped,pyright 生产代码 0 错误
This commit is contained in:
yml
2026-11-25 20:53:29 +08:00
parent da85373884
commit 0943b7a362
6 changed files with 387 additions and 31 deletions
+78
View File
@@ -2,13 +2,17 @@
from __future__ import annotations
import threading
import time
from datetime import UTC, datetime, timedelta
from web.backend.models import ExecutionJob, User
from web.backend.services import execution_worker
from web.backend.services.execution_jobs import (
JOB_LOGIN_BATCH,
JOB_DOUYU_BATCH,
LANE_AUTH,
LANE_FULFILLMENT,
STATUS_CANCELLED,
STATUS_RUNNING,
claim_execution_jobs,
@@ -164,3 +168,77 @@ def test_execution_worker_runs_claimed_login_batch(db, monkeypatch):
assert refreshed.status == "succeeded"
assert seen["ran"] is True
assert seen["batch_id"] == "exec-run"
def test_lane_slots_reserve_capacity_for_fulfillment(monkeypatch):
"""登录批次再慢也不能占满整个池子:履约 lane 必须留得住槽。"""
monkeypatch.setenv("EXECUTION_WORKER_MAX_JOBS", "2")
monkeypatch.setenv("EXECUTION_WORKER_LANES", "auth,fulfillment")
monkeypatch.delenv("EXECUTION_WORKER_AUTH_JOBS", raising=False)
monkeypatch.delenv("EXECUTION_WORKER_FULFILLMENT_JOBS", raising=False)
limits = execution_worker._lane_slot_limits()
assert limits[LANE_AUTH] == 1
assert limits[LANE_FULFILLMENT] == 2
# 领取顺序:履约优先,登录只用剩下的槽
assert execution_worker._lane_claim_order()[0] == LANE_FULFILLMENT
def test_auth_batches_cannot_starve_fulfillment_lane(db, monkeypatch):
"""两个登录批次在跑时,履约批次仍然能拿到执行槽(总槽位不变)。"""
user = _user(db, "lane-isolation")
for index in range(2):
enqueue_execution_job(
db,
job_type=JOB_LOGIN_BATCH,
lane_scope=LANE_AUTH,
batch_id=f"lane-auth-{index}",
payload={"account_ids": [1], "mode": "login", "concurrency": 1},
created_by=int(user.id),
)
enqueue_execution_job(
db,
job_type=JOB_DOUYU_BATCH,
lane_scope=LANE_FULFILLMENT,
batch_id="lane-fulfillment-1",
payload={"task_type": "elite_exchange", "payload": {}},
created_by=int(user.id),
)
db.commit()
monkeypatch.setenv("EXECUTION_WORKER_MAX_JOBS", "2")
monkeypatch.setenv("EXECUTION_WORKER_LANES", "auth,fulfillment")
monkeypatch.delenv("EXECUTION_WORKER_AUTH_JOBS", raising=False)
monkeypatch.delenv("EXECUTION_WORKER_FULFILLMENT_JOBS", raising=False)
release = threading.Event()
worker = execution_worker.ExecutionJobWorker()
def fake_run_job(_job_id: int) -> None:
release.wait(10)
monkeypatch.setattr(worker, "_run_job", fake_run_job)
worker.start()
try:
deadline = time.monotonic() + 10
lanes: list[str] = []
while time.monotonic() < deadline:
# 结束当前事务再查:worker 在**另一个连接**上把任务改成 running,
# 旧快照看不到(MySQL REPEATABLE READ)。
db.rollback()
rows = (
db.query(ExecutionJob)
.filter(ExecutionJob.status == STATUS_RUNNING)
.all()
)
lanes = [row.lane_scope for row in rows]
if LANE_FULFILLMENT in lanes and LANE_AUTH in lanes:
break
time.sleep(0.2)
assert LANE_AUTH in lanes
assert LANE_FULFILLMENT in lanes, "履约批次被登录批次饿死了"
# auth lane 最多占 1 个槽,第二个登录批次必须留在队列里
assert lanes.count(LANE_AUTH) == 1
finally:
release.set()
worker.stop()