perf(douyu): 拆分任务槽并改造绑定扫码调度
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import inspect
|
||||
import threading
|
||||
import time
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
import pytest
|
||||
from fastapi import HTTPException
|
||||
@@ -13,11 +14,18 @@ from web.backend.main import health
|
||||
from web.backend.models import Account, DouyuTask, User
|
||||
from web.backend.routers import douyu as douyu_router
|
||||
from web.backend.schemas import DouyuTaskBatchRequest
|
||||
from web.backend.services import business_events, douyu_concurrency, douyu_service
|
||||
from web.backend.services import (
|
||||
binding_poll_scheduler,
|
||||
business_events,
|
||||
douyu_concurrency,
|
||||
douyu_service,
|
||||
)
|
||||
from web.backend.services.douyu_runner import DouyuBatchRunner
|
||||
from web.backend.services.douyu_runner_core import (
|
||||
DouyuBatchRunnerCore,
|
||||
slim_douyu_task_result_for_storage,
|
||||
)
|
||||
from web.backend.services.task_runtime import TaskLane, douyu_lane_for_task
|
||||
|
||||
|
||||
def _fixtures(db, *, status: str = "planned") -> tuple[User, Account, DouyuTask]:
|
||||
@@ -156,6 +164,176 @@ def test_cleanup_commits_stopped_status_before_account_event(db, monkeypatch):
|
||||
assert observed == ["stopped"]
|
||||
|
||||
|
||||
def test_cleanup_preserves_recoverable_binding_poll(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = datetime.now(UTC) + timedelta(seconds=5)
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
db.commit()
|
||||
|
||||
cleaned = douyu_service.cleanup_orphan_douyu_tasks(
|
||||
db,
|
||||
active_batch_ids=set(),
|
||||
statuses=("running",),
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert cleaned == 0
|
||||
assert task.status == "running"
|
||||
assert task.queue_lane == "binding_poll"
|
||||
|
||||
|
||||
def test_binding_poll_scheduler_claims_due_task_without_worker_wait(db):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
due_at = datetime.now(UTC) - timedelta(seconds=1)
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = due_at
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
db.commit()
|
||||
|
||||
try:
|
||||
claimed = binding_poll_scheduler._claim_due_binding_tasks(10)
|
||||
db.refresh(task)
|
||||
assert claimed == [task.id]
|
||||
assert task.next_run_at is not None
|
||||
assert task.next_run_at > due_at
|
||||
finally:
|
||||
with binding_poll_scheduler._in_flight_lock:
|
||||
binding_poll_scheduler._in_flight.discard(task.id)
|
||||
|
||||
|
||||
def test_bind_qr_schedules_poll_and_releases_worker(db, monkeypatch):
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
info = {
|
||||
"act_alias": "bind-act",
|
||||
"role_name": "old-role",
|
||||
"is_bound_act": True,
|
||||
}
|
||||
snapshot = {
|
||||
"role_name": "old-role",
|
||||
"area_name": "",
|
||||
"plat_name": "",
|
||||
"game_id": "",
|
||||
"game_name": "",
|
||||
"game_channel": "",
|
||||
"change_role_wait_time": None,
|
||||
"can_change_time": None,
|
||||
"can_change_role": True,
|
||||
"is_bound_act": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_account": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": False,
|
||||
}
|
||||
client = type(
|
||||
"Client",
|
||||
(),
|
||||
{"get_bind_qr": lambda self, _alias: {"url": "https://example.test/qr"}},
|
||||
)()
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", lambda *_args: [info])
|
||||
monkeypatch.setattr(runner, "_pick_baseline_bind_info", lambda *_args: info)
|
||||
monkeypatch.setattr(runner, "_pick_change_wait_bind_info", lambda *_args: None)
|
||||
monkeypatch.setattr(runner, "_pick_bind_info", lambda *_args, **_kwargs: info)
|
||||
monkeypatch.setattr(runner, "_bind_snapshot", lambda _info: snapshot)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_wait_bind_role_result",
|
||||
lambda *_args, **_kwargs: (_ for _ in ()).throw(
|
||||
AssertionError("scan wait must not block the worker")
|
||||
),
|
||||
)
|
||||
|
||||
runner._execute_get_bind_qr(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{
|
||||
"bind_act_alias": "bind-act",
|
||||
"legacy_act_alias": "legacy-act",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "running"
|
||||
assert task.queue_lane == "binding_poll"
|
||||
assert task.next_run_at is not None
|
||||
assert task.run_deadline_at is not None
|
||||
assert task.result["url"] == "https://example.test/qr"
|
||||
|
||||
|
||||
def test_binding_poll_once_finishes_when_role_is_found(db, monkeypatch):
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = datetime.now(UTC)
|
||||
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
|
||||
task.result = {
|
||||
"url": "https://example.test/qr",
|
||||
"query_act_aliases": ["bind-act"],
|
||||
"baseline_role_name": "old-role",
|
||||
"bind_polling": True,
|
||||
}
|
||||
db.commit()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
info = {"act_alias": "bind-act", "role_name": "new-role"}
|
||||
snapshot = {
|
||||
"role_name": "new-role",
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓",
|
||||
"game_id": "",
|
||||
"game_name": "",
|
||||
"game_channel": "",
|
||||
"change_role_wait_time": None,
|
||||
"can_change_time": None,
|
||||
"can_change_role": True,
|
||||
"is_bound_act": False,
|
||||
"is_bound_role": True,
|
||||
"is_bound_account": False,
|
||||
"need_bind_act": True,
|
||||
"need_bind_role": True,
|
||||
}
|
||||
client = type("Client", (), {})()
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", lambda *_args: [info])
|
||||
monkeypatch.setattr(runner, "_pick_bind_info", lambda *_args, **_kwargs: info)
|
||||
monkeypatch.setattr(runner, "_bind_snapshot", lambda _info: snapshot)
|
||||
monkeypatch.setattr(runner, "_is_pending_role", lambda *_args, **_kwargs: True)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_format_bind_summary",
|
||||
lambda *_args, **_kwargs: "role=new-role",
|
||||
)
|
||||
|
||||
runner.poll_bind_role_once(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
{
|
||||
"bind_act_alias": "bind-act",
|
||||
"legacy_act_alias": "legacy-act",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.queue_lane == ""
|
||||
assert task.next_run_at is None
|
||||
assert task.result["role_name"] == "new-role"
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
|
||||
|
||||
def test_active_task_budget_rejects_new_batch(db, monkeypatch):
|
||||
user = User(username="budget-user", password_hash="hash", role="operation")
|
||||
db.add(user)
|
||||
@@ -211,6 +389,13 @@ def test_batch_task_concurrency_is_capped_at_five():
|
||||
assert douyu_concurrency.clamp_batch_task_concurrency(20) == 5
|
||||
|
||||
|
||||
def test_douyu_lanes_separate_interactive_query_payment_and_writes():
|
||||
assert douyu_lane_for_task("get_bind_qr") == TaskLane.INTERACTIVE_BINDING
|
||||
assert douyu_lane_for_task("query_account_overview") == TaskLane.QUERY_BATCH
|
||||
assert douyu_lane_for_task("create_gold_qr") == TaskLane.PAYMENT_WAIT
|
||||
assert douyu_lane_for_task("exchange_goods") == TaskLane.OPS_WRITE
|
||||
|
||||
|
||||
def test_overview_limiter_blocks_extra_workers(monkeypatch):
|
||||
slots = threading.BoundedSemaphore(1)
|
||||
monkeypatch.setattr(douyu_concurrency, "_overview_slots", slots)
|
||||
|
||||
Reference in New Issue
Block a user