perf(douyu): 拆分支付轮询并扩展查询并发

This commit is contained in:
yml
2026-11-21 10:38:39 +08:00
parent 743fa69630
commit 3987464035
21 changed files with 1151 additions and 531 deletions
+13 -2
View File
@@ -77,9 +77,20 @@ DB_LOCK_WAIT_TIMEOUT=10
# 斗鱼批次创建并发与全局活跃量背压。
DOUYU_BATCH_CREATE_CONCURRENCY=2
DOUYU_BATCH_TASK_CONCURRENCY=5
DOUYU_PAYMENT_QR_CONCURRENCY=10
DOUYU_MAX_ACTIVE_BATCHES=10
# 所有批次共享的执行槽与一键查询槽,避免单个批次占满数据库连接。
DOUYU_MAX_ACTIVE_OVERVIEW_TASKS=2
# 一键查询使用独立任务槽;普通查询仍为 3 个 worker。
DOUYU_MAX_ACTIVE_OVERVIEW_TASKS=5
# 业务域任务槽。支付出码与到账轮询分离后,各自按实际耗时独立限流。
TASK_LANE_INTERACTIVE_BINDING_WORKERS=3
TASK_LANE_BINDING_POLL_WORKERS=2
TASK_LANE_LOGIN_WORKERS=4
TASK_LANE_ACCOUNT_CHECK_WORKERS=4
TASK_LANE_OVERVIEW_QUERY_WORKERS=5
TASK_LANE_QUERY_BATCH_WORKERS=3
TASK_LANE_PAYMENT_QR_WORKERS=10
TASK_LANE_PAYMENT_POLL_WORKERS=3
TASK_LANE_OPS_WRITE_WORKERS=2
DOUYU_MAX_ACTIVE_TASKS=250
DOUYU_PROBE_CACHE_SECONDS=30
DOUYU_COLLAB_VERIFY_BATCH_INTERVAL_SECONDS=0.2
+11 -1
View File
@@ -46,8 +46,18 @@ services:
- WORKER_DB_POOL_TIMEOUT=${WORKER_DB_POOL_TIMEOUT:-30}
- DOUYU_BATCH_CREATE_CONCURRENCY=${DOUYU_BATCH_CREATE_CONCURRENCY:-2}
- DOUYU_BATCH_TASK_CONCURRENCY=${DOUYU_BATCH_TASK_CONCURRENCY:-5}
- DOUYU_PAYMENT_QR_CONCURRENCY=${DOUYU_PAYMENT_QR_CONCURRENCY:-10}
- DOUYU_MAX_ACTIVE_BATCHES=${DOUYU_MAX_ACTIVE_BATCHES:-10}
- DOUYU_MAX_ACTIVE_OVERVIEW_TASKS=${DOUYU_MAX_ACTIVE_OVERVIEW_TASKS:-2}
- DOUYU_MAX_ACTIVE_OVERVIEW_TASKS=${DOUYU_MAX_ACTIVE_OVERVIEW_TASKS:-5}
- TASK_LANE_INTERACTIVE_BINDING_WORKERS=${TASK_LANE_INTERACTIVE_BINDING_WORKERS:-3}
- TASK_LANE_BINDING_POLL_WORKERS=${TASK_LANE_BINDING_POLL_WORKERS:-2}
- TASK_LANE_LOGIN_WORKERS=${TASK_LANE_LOGIN_WORKERS:-4}
- TASK_LANE_ACCOUNT_CHECK_WORKERS=${TASK_LANE_ACCOUNT_CHECK_WORKERS:-4}
- TASK_LANE_OVERVIEW_QUERY_WORKERS=${TASK_LANE_OVERVIEW_QUERY_WORKERS:-5}
- TASK_LANE_QUERY_BATCH_WORKERS=${TASK_LANE_QUERY_BATCH_WORKERS:-3}
- TASK_LANE_PAYMENT_QR_WORKERS=${TASK_LANE_PAYMENT_QR_WORKERS:-10}
- TASK_LANE_PAYMENT_POLL_WORKERS=${TASK_LANE_PAYMENT_POLL_WORKERS:-3}
- TASK_LANE_OPS_WRITE_WORKERS=${TASK_LANE_OPS_WRITE_WORKERS:-2}
- DOUYU_MAX_ACTIVE_TASKS=${DOUYU_MAX_ACTIVE_TASKS:-250}
- DOUYU_PROBE_CACHE_SECONDS=${DOUYU_PROBE_CACHE_SECONDS:-30}
- DOUYU_COLLAB_VERIFY_BATCH_INTERVAL_SECONDS=${DOUYU_COLLAB_VERIFY_BATCH_INTERVAL_SECONDS:-0.2}
+14 -2
View File
@@ -175,8 +175,10 @@ live-hub 创建新的绑定任务。
- `binding_poll`:每次只执行一次扫码角色查询;
- `login`、`account_check`:登录、检测与重登;
- `automation`:定时自动化作业;
- `query_batch`:账号概览、商品和角色查询;
- `payment_wait`:二维码生成后的支付/到账等待;
- `overview_query`:一键账号查询,独立 5 个 worker;
- `query_batch`:普通商品和角色查询;
- `payment_qr`:支付二维码生成,最多 10 个 worker;
- `payment_poll`:支付到账持久轮询,独立 3 个 worker;
- `ops_write`:兑换、支付、锁单和送礼;
- `huya_batch`、`huya_register`、`integration`、`maintenance`:各自独立。
@@ -204,6 +206,16 @@ live-hub 创建新的绑定任务。
在状态事务内写 Outbox,由 order_site Webhook 和 SSE 推动页面重读;
- “刷新状态”不启动绑定,“刷新二维码”先读取权威状态,有效期内直接复用。
支付二维码同样拆成两个阶段:
1. `create_*_qr` 只执行状态预检、记录余额/积分基线和生成支付码,完成后把任务
写入 `queue_lane=payment_poll`、`next_run_at` 和 `run_deadline_at`,立即释放 worker;
2. `payment-poll-scheduler` 从数据库领取到期任务,低频查询余额、积分或供应商订单,
到账后收口成功,超过截止时间才转失败。
因此二维码生成吞吐和支付等待时长互不占用:支付出码默认 10 个并发,到账查询默认
3 个 worker。进程重启后未到期的支付任务继续由数据库调度。
## 权限
- `platform_fulfillment:view`
+91 -36
View File
@@ -12,7 +12,6 @@ import pytest
from tests.support.db import get_test_engine, make_test_session
from web.backend.models import Account, DouyuTask, User
from web.backend.services import douyu_runner_manual
from web.backend.services.douyu_runner import DouyuBatchRunner
from web.backend.services.douyu_service import (
DOUYU_HANDBOOK_TASK_TYPES,
@@ -64,6 +63,14 @@ class TestEliteHandbookOpenFlow:
"confirm_act_alias": "20260120QYOOB",
}
def _esports_config(self) -> dict:
return {
"esports_act_alias": "20260623KDQFH",
"esports_manual_id": "46195",
"esports_amount": 3000,
"room_id": "9263298",
}
def test_task_type_registered_for_elite_scope(self):
assert SUPPORTED_DOUYU_TASK_TYPES["query_elite_status"] == "查询精英宝典状态"
assert "query_elite_status" in DOUYU_HANDBOOK_TASK_TYPES["elite"]
@@ -165,10 +172,7 @@ class TestEliteHandbookOpenFlow:
assert self.account.elite_opened is True
assert self.account.bind_status != "elite_opened"
def test_create_elite_qr_does_not_skip_for_expired_elite(self, monkeypatch):
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_SECONDS", 0.05)
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_INTERVAL", 0.01)
def test_create_elite_qr_does_not_skip_for_expired_elite(self):
task = self._task("create_elite_qr")
runner = self._runner("create_elite_qr")
client = Mock()
@@ -188,15 +192,14 @@ class TestEliteHandbookOpenFlow:
self.session.refresh(task)
self.session.refresh(self.account)
assert task.status == "failed"
assert task.status == "running"
assert task.queue_lane == "payment_poll"
assert task.next_run_at is not None
assert self.account.elite_opened is False
client.create_elite_qr.assert_called_once()
def test_create_elite_qr_uses_points_baseline(self, monkeypatch):
def test_create_elite_qr_uses_points_baseline(self):
"""账上已有 680 积分时不能立刻判成功,必须等积分真的增长。"""
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_SECONDS", 0.05)
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_INTERVAL", 0.01)
task = self._task("create_elite_qr")
runner = self._runner("create_elite_qr")
client = Mock()
@@ -217,15 +220,13 @@ class TestEliteHandbookOpenFlow:
self.session.refresh(task)
# 积分没有增长 -> 不能判成功(旧口径会立刻 success,二维码随即消失)
assert task.status == "failed"
assert task.result["elite_opened"] is False
assert task.status == "running"
assert task.queue_lane == "payment_poll"
assert "elite_opened" not in task.result
assert task.result["elite_points_baseline"] == 680
client.create_elite_qr.assert_called_once()
def test_create_elite_qr_succeeds_when_points_grow(self, monkeypatch):
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_SECONDS", 0.05)
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_INTERVAL", 0.01)
def test_create_elite_qr_succeeds_when_points_grow(self):
task = self._task("create_elite_qr")
runner = self._runner("create_elite_qr")
client = Mock()
@@ -245,6 +246,17 @@ class TestEliteHandbookOpenFlow:
runner._execute_create_elite_qr(
self.session, task, self.account, "acf_uid=100", self._config()
)
self.session.refresh(task)
assert task.status == "running"
assert task.queue_lane == "payment_poll"
runner._poll_create_elite_qr_once(
self.session,
task,
self.account,
"acf_uid=100",
self._config(),
)
self.session.refresh(task)
self.session.refresh(self.account)
@@ -252,48 +264,91 @@ class TestEliteHandbookOpenFlow:
assert task.result["elite_opened"] is True
assert self.account.elite_opened is True
def test_wait_points_falls_back_without_baseline(self, monkeypatch):
def test_poll_points_falls_back_without_baseline(self):
"""拿不到基线时退回旧口径,避免完全无法判定到账。"""
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_SECONDS", 0.05)
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_INTERVAL", 0.01)
task = self._task("create_elite_qr")
task.result = {"elite_points_baseline": None, "payment_ctn": "ctn-1"}
self.session.commit()
runner = self._runner("create_elite_qr")
client = Mock()
runner._refresh_account_points = Mock(return_value={"points": 680})
runner._client = Mock(return_value=client)
opened = runner._wait_points_after_payment(
runner._poll_create_elite_qr_once(
self.session,
task,
self.account,
client,
"acf_uid=100",
"ctn-1",
{},
baseline_points=None,
self._config(),
)
assert opened is True
self.session.refresh(task)
assert task.status == "success"
assert task.result["elite_opened"] is True
@pytest.mark.parametrize("already_points", [300, 680])
def test_wait_points_ignores_existing_points(self, monkeypatch, already_points):
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_SECONDS", 0.05)
monkeypatch.setattr(douyu_runner_manual, "DOUYU_PAYMENT_POLL_INTERVAL", 0.01)
def test_poll_points_ignores_existing_points(self, already_points):
task = self._task("create_elite_qr")
task.result = {
"elite_points_baseline": already_points,
"payment_ctn": "ctn-1",
}
self.session.commit()
runner = self._runner("create_elite_qr")
client = Mock()
runner._refresh_account_points = Mock(return_value={"points": already_points})
runner._client = Mock(return_value=client)
opened = runner._wait_points_after_payment(
runner._poll_create_elite_qr_once(
self.session,
task,
self.account,
client,
"acf_uid=100",
"ctn-1",
{},
baseline_points=already_points,
self._config(),
)
assert opened is False
self.session.refresh(task)
assert task.status == "running"
assert task.queue_lane == "payment_poll"
def test_esports_qr_schedules_and_poll_completes_after_open(self):
task = self._task("create_esports_qr")
runner = self._runner("create_esports_qr")
client = Mock()
client.acf_ccn.return_value = "ctn-1"
client.esports_user_info.side_effect = [
{"manual_type": 0, "manual_score": 0, "expire_time": 0},
{"manual_type": 1, "manual_score": 300, "expire_time": 1799999999},
]
client.create_esports_qr.return_value = {
"pay_url": "https://example.test/esports-pay"
}
runner._client = Mock(return_value=client)
runner._execute_create_esports_qr(
self.session,
task,
self.account,
"acf_uid=100",
self._esports_config(),
)
self.session.refresh(task)
assert task.status == "running"
assert task.queue_lane == "payment_poll"
assert task.result["esports_manual_score_baseline"] == 0
runner._poll_create_esports_qr_once(
self.session,
task,
self.account,
"acf_uid=100",
self._esports_config(),
)
self.session.refresh(task)
self.session.refresh(self.account)
assert task.status == "success"
assert task.queue_lane == ""
assert task.result["esports_opened"] is True
assert self.account.esports_opened is True
+105
View File
@@ -48,6 +48,66 @@ class TestDouyuGoldRechargeChannel:
def teardown_method(self):
self.session.close()
def test_wallet_gold_qr_schedules_poll_and_completes_after_balance_grows(
self,
):
config = {
"gold_recharge_channel": "wechat_qr",
"gold_pay_type": 1,
}
client = Mock()
client.create_gold_qr.return_value = {
"pay_url": "https://example.test/gold-pay",
"token": "gold-token",
}
self.task.result = {"payload": {"amount": 10}}
self.session.commit()
self.runner._client = Mock(return_value=client)
self.runner._refresh_account_gold_balance = Mock(
return_value={
"gold_balance": 100,
"exchange_balance": 0,
"gold": {"gold": 100},
"exchange_balance_query": {"count": 0},
}
)
self.runner._execute_create_gold_qr(
self.session,
self.task,
self.account,
"acf_uid=10001",
config,
)
self.session.refresh(self.task)
assert self.task.status == "running"
assert self.task.queue_lane == "payment_poll"
assert self.task.next_run_at is not None
assert self.task.result["baseline_gold_balance"] == 100
assert self.task.result["pay_url"] == "https://example.test/gold-pay"
self.runner._refresh_account_gold_balance = Mock(
return_value={
"gold_balance": 110,
"exchange_balance": 0,
"gold": {"gold": 110},
"exchange_balance_query": {"count": 0},
}
)
self.runner._poll_create_gold_qr_once(
self.session,
self.task,
self.account,
"acf_uid=10001",
config,
)
self.session.refresh(self.task)
assert self.task.status == "success"
assert self.task.queue_lane == ""
assert self.task.result["gold_recharged"] is True
@patch("web.backend.services.douyu_runner_gold.FishFinRechargeClient")
def test_supplier_channel_creates_order_using_nickname_and_finishes_on_success(
self, client_class
@@ -124,6 +184,51 @@ class TestDouyuGoldRechargeChannel:
assert self.task.status == "failed"
assert "斗鱼昵称" in self.task.message
@patch("web.backend.services.douyu_runner_gold.FishFinRechargeClient")
def test_supplier_pending_order_is_polled_by_durable_scheduler(self, client_class):
supplier = Mock()
supplier.create_order.return_value = {
"code": 200,
"result": {"order_status": 1, "order_id": "supplier-002"},
}
supplier.query_order.return_value = {
"code": 200,
"result": {"order_status": 2, "order_id": "supplier-002"},
}
client_class.return_value = supplier
config = {
"gold_recharge_channel": "supplier_api",
"gold_api_product_id": "gold-product",
"gold_api_account_template_name": "斗鱼账号",
}
self.task.result = {"payload": {"amount": 10}}
self.session.commit()
self.runner._execute_create_gold_qr(
self.session,
self.task,
self.account,
"acf_uid=10001",
config,
)
self.session.refresh(self.task)
assert self.task.status == "running"
assert self.task.queue_lane == "payment_poll"
assert self.task.result["supplier_order_status"] == 1
self.runner._poll_create_gold_supplier_once(
self.session,
self.task,
self.account,
amount=10,
)
self.session.refresh(self.task)
assert self.task.status == "success"
assert self.task.queue_lane == ""
assert self.task.result["supplier_order_status"] == 2
def test_supplier_out_order_id_reuses_persisted_value(self):
self.task.supplier_out_order_id = "DYGFHISTORICAL-001"
assert self.runner._supplier_out_order_id(self.task) == "DYGFHISTORICAL-001"
+15
View File
@@ -120,6 +120,21 @@ class TestDouyuProxyMode:
)
assert runner._proxies_for_task() is None
def test_payment_poll_direct_client_does_not_fetch_proxy(self):
runner = _runner(
_cfg(enabled=True, api_url="https://proxy-api.example/fetch"),
task_type="create_elite_qr",
)
runner._proxy_fetcher = Mock()
runner._proxy_fetcher.fetch_new_proxy.side_effect = AssertionError(
"支付到账轮询不应取代理"
)
client = runner._client("acf_uid=100", direct=True)
assert client.session.proxies == {}
runner._proxy_fetcher.fetch_new_proxy.assert_not_called()
def test_proxy_task_types_covers_all_write_tasks(self):
"""白名单集合应覆盖全部会消耗/下单的写操作任务,且不含查询类。"""
assert "exchange_goods" in DOUYU_PROXY_TASK_TYPES
+104 -32
View File
@@ -1,6 +1,4 @@
import inspect
import threading
import time
from datetime import UTC, datetime, timedelta
import pytest
@@ -19,13 +17,18 @@ from web.backend.services import (
business_events,
douyu_concurrency,
douyu_service,
payment_poll_scheduler,
)
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
from web.backend.services.task_runtime import (
_DEFAULT_WORKERS,
TaskLane,
douyu_lane_for_task,
)
def _fixtures(db, *, status: str = "planned") -> tuple[User, Account, DouyuTask]:
@@ -183,6 +186,26 @@ def test_cleanup_preserves_recoverable_binding_poll(db):
assert task.queue_lane == "binding_poll"
def test_cleanup_preserves_recoverable_payment_poll(db):
_, _, task = _fixtures(db, status="running")
task.task_type = "create_gold_qr"
task.queue_lane = "payment_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 == "payment_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)
@@ -202,6 +225,47 @@ def test_binding_poll_scheduler_claims_due_task_without_worker_wait(db):
binding_poll_scheduler._in_flight.discard(task.id)
def test_payment_poll_scheduler_claims_due_task_without_worker_wait(db):
_, _, task = _fixtures(db, status="running")
due_at = datetime.now(UTC) - timedelta(seconds=1)
task.task_type = "create_gold_qr"
task.queue_lane = "payment_poll"
task.next_run_at = due_at
task.run_deadline_at = datetime.now(UTC) + timedelta(minutes=10)
task.result = {"pay_url": "https://example.test/pay"}
db.commit()
try:
claimed = payment_poll_scheduler._claim_due_payment_tasks(10)
db.refresh(task)
assert claimed == [task.id]
assert task.next_run_at is not None
assert task.next_run_at > due_at
assert task.attempt_count == 1
finally:
with payment_poll_scheduler._in_flight_lock:
payment_poll_scheduler._in_flight.discard(task.id)
def test_payment_poll_marks_expired_task_failed(db, monkeypatch):
_, _, task = _fixtures(db, status="running")
task.task_type = "create_elite_qr"
task.queue_lane = "payment_poll"
task.next_run_at = datetime.now(UTC) - timedelta(seconds=1)
task.run_deadline_at = datetime.now(UTC) - timedelta(seconds=1)
task.result = {"pay_url": "https://example.test/pay"}
db.commit()
monkeypatch.setattr(payment_poll_scheduler, "WorkerSessionLocal", make_test_session)
payment_poll_scheduler.poll_payment_task_once(task.id)
db.rollback()
db.refresh(task)
assert task.status == "failed"
assert task.queue_lane == ""
assert "超时" in task.message
def test_bind_qr_schedules_poll_and_releases_worker(db, monkeypatch):
_, account, task = _fixtures(db, status="running")
runner = DouyuBatchRunner(
@@ -375,49 +439,57 @@ def test_active_batch_budget_returns_429(monkeypatch):
assert exc_info.value.status_code == 429
def test_douyu_batch_request_rejects_more_than_five_workers():
def test_douyu_batch_request_allows_ten_workers_for_payment_qr():
req = DouyuTaskBatchRequest(
account_ids=[1],
task_type="create_gold_qr",
handbook_scope="elite",
concurrency=10,
)
assert req.concurrency == 10
with pytest.raises(ValidationError):
DouyuTaskBatchRequest(
account_ids=[1],
task_type="query_points",
task_type="create_gold_qr",
handbook_scope="elite",
concurrency=6,
concurrency=11,
)
def test_batch_task_concurrency_is_capped_at_five():
assert douyu_concurrency.clamp_batch_task_concurrency(20) == 5
assert douyu_concurrency.clamp_douyu_task_concurrency("query_points", 20) == 5
assert douyu_concurrency.clamp_douyu_task_concurrency("create_gold_qr", 20) == 10
def test_runner_applies_task_specific_concurrency(db):
payment = DouyuBatchRunner(
db,
"payment-batch",
"create_gold_qr",
concurrency=10,
close_db=False,
)
overview = DouyuBatchRunner(
db,
"overview-batch",
"query_account_overview",
concurrency=10,
close_db=False,
)
assert payment.concurrency == 10
assert overview.concurrency == 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("query_account_overview") == TaskLane.OVERVIEW_QUERY
assert douyu_lane_for_task("create_gold_qr") == TaskLane.PAYMENT_QR
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)
acquired: list[bool] = []
started = threading.Event()
assert douyu_concurrency.acquire_overview_slot(lambda: False) is True
def worker():
started.set()
acquired.append(douyu_concurrency.acquire_overview_slot(lambda: False))
thread = threading.Thread(target=worker)
thread.start()
started.wait(timeout=1)
time.sleep(0.05)
assert acquired == []
douyu_concurrency.release_overview_slot()
thread.join(timeout=1)
assert acquired == [True]
douyu_concurrency.release_overview_slot()
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 10
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 3
assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == 5
def test_slim_task_result_removes_debug_payloads():
+6
View File
@@ -109,6 +109,10 @@ async def lifespan(app: FastAPI):
start_integration_outbox_worker,
stop_integration_outbox_worker,
)
from .services.payment_poll_scheduler import (
start_payment_poll_scheduler,
stop_payment_poll_scheduler,
)
from .services.platform_fulfillment.auto_allocator import (
start_platform_fulfillment_auto_allocator,
stop_platform_fulfillment_auto_allocator,
@@ -123,9 +127,11 @@ async def lifespan(app: FastAPI):
start_platform_fulfillment_reconciler()
start_platform_fulfillment_auto_allocator()
start_binding_poll_scheduler()
start_payment_poll_scheduler()
try:
yield
finally:
stop_payment_poll_scheduler()
stop_binding_poll_scheduler()
stop_platform_fulfillment_auto_allocator()
stop_platform_fulfillment_reconciler()
+4
View File
@@ -97,6 +97,9 @@ def list_jobs(
from ..services.binding_poll_scheduler import (
scheduler_status as binding_poll_status,
)
from ..services.payment_poll_scheduler import (
scheduler_status as payment_poll_status,
)
from ..services.task_runtime import lane_stats
pending_relogin = count_pending_relogin(db)
@@ -109,6 +112,7 @@ def list_jobs(
if callable(getattr(worker_engine.pool, "status", None))
else "",
"binding_poll": binding_poll_status(),
"payment_poll": payment_poll_status(),
},
"pending_relogin": pending_relogin,
"jobs": [
+6
View File
@@ -180,6 +180,9 @@ async def supplier_recharge_callback(request: Request, db: Session = Depends(get
task.status = "success"
task.message = "供应商直充成功(异步通知)"
task.finished_at = datetime.now(UTC)
task.queue_lane = ""
task.next_run_at = None
task.run_deadline_at = None
if task.account:
task.account.updated_at = datetime.now(UTC)
elif status in {3, 4}:
@@ -190,6 +193,9 @@ async def supplier_recharge_callback(request: Request, db: Session = Depends(get
task.status = "failed"
task.message = reason[:512]
task.finished_at = datetime.now(UTC)
task.queue_lane = ""
task.next_run_at = None
task.run_deadline_at = None
else:
task.status = "running"
task.message = (
+1 -1
View File
@@ -1624,7 +1624,7 @@ class DouyuTaskBatchRequest(BaseModel):
account_ids: list[int]
task_type: str
handbook_scope: str = Field(..., pattern="^(elite|esports|peace)$")
concurrency: int = Field(3, ge=1, le=5)
concurrency: int = Field(3, ge=1, le=10)
payload: dict[str, Any] = Field(default_factory=dict)
platform_task_id: int | None = Field(None, gt=0)
+2 -2
View File
@@ -11,7 +11,7 @@ import threading
from web.backend.database import WorkerSessionLocal
from .douyu_concurrency import clamp_batch_task_concurrency
from .douyu_concurrency import clamp_douyu_task_concurrency
from .douyu_runner import DouyuBatchRunner, douyu_batch_registry
@@ -56,7 +56,7 @@ def start_douyu_batch(
payload=payload,
log_queue=log_queue,
loop=loop,
concurrency=clamp_batch_task_concurrency(concurrency),
concurrency=clamp_douyu_task_concurrency(task_type, concurrency),
)
douyu_batch_registry.register(batch_id, log_queue, loop, runner)
threading.Thread(target=runner.run, daemon=True).start()
+24 -26
View File
@@ -1,14 +1,12 @@
"""斗鱼批次级并发限制。
"""斗鱼批次与任务类型级并发限制。
Process-wide execution is owned by ``task_runtime`` lanes; this module only
keeps the per-batch cap and the overview sub-limit.
Process-wide execution is owned by ``task_runtime`` lanes; this module keeps
per-task-type admission caps before work reaches those lanes.
"""
from __future__ import annotations
import os
import threading
from collections.abc import Callable
def _env_positive_int(name: str, default: int) -> int:
@@ -23,38 +21,38 @@ DOUYU_BATCH_TASK_CONCURRENCY = _env_positive_int(
"DOUYU_BATCH_TASK_CONCURRENCY",
5,
)
DOUYU_MAX_ACTIVE_OVERVIEW_TASKS = _env_positive_int(
"DOUYU_MAX_ACTIVE_OVERVIEW_TASKS",
2,
DOUYU_PAYMENT_QR_CONCURRENCY = _env_positive_int(
"DOUYU_PAYMENT_QR_CONCURRENCY",
10,
)
_overview_slots = threading.BoundedSemaphore(DOUYU_MAX_ACTIVE_OVERVIEW_TASKS)
PAYMENT_QR_TASK_TYPES = frozenset(
{
"create_elite_qr",
"create_esports_qr",
"create_gold_qr",
}
)
def clamp_batch_task_concurrency(value: int) -> int:
return max(1, min(int(value), DOUYU_BATCH_TASK_CONCURRENCY))
def _acquire(slots: threading.BoundedSemaphore, stopped: Callable[[], bool]) -> bool:
while not stopped():
if slots.acquire(timeout=0.2):
return True
return False
def acquire_overview_slot(stopped: Callable[[], bool]) -> bool:
"""等待一个一键查询槽;该限制跨所有批次共享。"""
return _acquire(_overview_slots, stopped)
def release_overview_slot() -> None:
_overview_slots.release()
def clamp_douyu_task_concurrency(task_type: str, value: int) -> int:
"""按任务类型限制批次内并发,不把支付出码压回普通查询的上限。"""
limit = (
DOUYU_PAYMENT_QR_CONCURRENCY
if task_type in PAYMENT_QR_TASK_TYPES
else DOUYU_BATCH_TASK_CONCURRENCY
)
return max(1, min(int(value), limit))
__all__ = [
"DOUYU_BATCH_TASK_CONCURRENCY",
"DOUYU_MAX_ACTIVE_OVERVIEW_TASKS",
"acquire_overview_slot",
"DOUYU_PAYMENT_QR_CONCURRENCY",
"PAYMENT_QR_TASK_TYPES",
"clamp_batch_task_concurrency",
"release_overview_slot",
"clamp_douyu_task_concurrency",
]
+43 -15
View File
@@ -13,10 +13,6 @@ from ..database import WorkerSessionLocal
from ..models import DouyuTask
from .cookie_check_service import check_douyu_cookie
from .douyu_account_probe import persist_probe_result, probe_session
from .douyu_concurrency import (
acquire_overview_slot,
release_overview_slot,
)
from .douyu_runner_bind import BindMixin
from .douyu_runner_core import ( # noqa: F401 (douyu_batch_registry 供 routers 重导出)
DouyuBatchRunnerCore,
@@ -48,18 +44,50 @@ class DouyuBatchRunner(
):
"""批量执行斗鱼活动任务(功能域 Mixin 聚合 + 批次调度)。"""
def poll_payment_task_once(
self,
db,
task: DouyuTask,
account,
config: dict,
) -> None:
cookie = str(account.cookie or "")
result = dict(task.result or {})
if not cookie:
self._mark_task(
db,
task,
"failed",
"账号 Cookie 已失效,无法继续查询支付到账",
result,
)
return
if task.task_type == "create_gold_qr":
self._poll_create_gold_qr_once(db, task, account, cookie, config)
elif task.task_type == "create_elite_qr":
self._poll_create_elite_qr_once(db, task, account, cookie, config)
elif task.task_type == "create_esports_qr":
self._poll_create_esports_qr_once(db, task, account, cookie, config)
else:
self._mark_task(
db,
task,
"failed",
f"不支持的支付轮询任务: {task.task_type}",
result,
)
def mark_payment_poll_timeout(self, db, task: DouyuTask) -> None:
result = dict(task.result or {})
message = {
"create_gold_qr": "鱼翅支付到账查询超时",
"create_elite_qr": "精英宝典开通到账查询超时",
"create_esports_qr": "电竞手册开通到账查询超时",
}.get(task.task_type, "支付到账查询超时")
self._mark_task(db, task, "failed", message, result)
def _execute_one(self, task_id: int, config: dict, total: int):
overview_task = self.task_type == "query_account_overview"
overview_slot = False
if overview_task:
overview_slot = acquire_overview_slot(self._stop.is_set)
if not overview_slot:
return
try:
self._execute_one_with_slot(task_id, config, total)
finally:
if overview_slot:
release_overview_slot()
self._execute_one_with_slot(task_id, config, total)
def _execute_one_with_slot(self, task_id: int, config: dict, total: int):
worker_db = SessionLocal()
+42 -7
View File
@@ -5,7 +5,7 @@ from __future__ import annotations
import asyncio
import threading
import time
from datetime import UTC, datetime
from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING, Any
from loguru import logger
@@ -24,7 +24,7 @@ from ..models import (
ProxyConfig as ProxyConfigModel,
)
from .douyu_account_probe import activity_client
from .douyu_concurrency import clamp_batch_task_concurrency
from .douyu_concurrency import clamp_douyu_task_concurrency
from .douyu_goods_service import upsert_goods_snapshot
from .douyu_service import (
DOUYU_CONFIG_FIELDS,
@@ -114,7 +114,7 @@ class DouyuBatchRunnerCore:
self.payload = payload or {}
self.log_queue = log_queue
self.loop = loop
self.concurrency = clamp_batch_task_concurrency(concurrency)
self.concurrency = clamp_douyu_task_concurrency(task_type, concurrency)
self._stop = threading.Event()
self._counter_lock = threading.Lock()
self._started = 0
@@ -277,8 +277,18 @@ class DouyuBatchRunnerCore:
has_platform_task = task.platform_task_id is not None
task.status = status
task.message = message[:512]
if status in {"success", "failed", "error", "stopped"}:
task.queue_lane = ""
task.next_run_at = None
task.run_deadline_at = None
if result is not None:
task.result = slim_douyu_task_result_for_storage(result)
persisted = dict(result)
if (
status in {"success", "failed", "error", "stopped"}
and "payment_polling" in persisted
):
persisted["payment_polling"] = False
task.result = slim_douyu_task_result_for_storage(persisted)
task.finished_at = datetime.now(UTC)
account = (
db.query(Account).filter(Account.id == task.account_id).first()
@@ -332,6 +342,26 @@ class DouyuBatchRunnerCore:
)
self._sync_platform_fulfillment(task_id, enabled=has_platform_task)
def _schedule_payment_poll(
self,
db: Session,
task: DouyuTask,
result: dict,
message: str,
*,
delay_seconds: int = DOUYU_PAYMENT_POLL_INTERVAL,
) -> None:
"""二维码已生成后释放 worker,由数据库调度器继续查询到账。"""
now = datetime.now(UTC)
result["payment_polling"] = True
task.queue_lane = "payment_poll"
task.next_run_at = now + timedelta(seconds=max(1, int(delay_seconds)))
task.last_run_at = now
if task.run_deadline_at is None:
task.run_deadline_at = now + timedelta(seconds=DOUYU_PAYMENT_POLL_SECONDS)
result["payment_deadline_at"] = task.run_deadline_at.isoformat()
self._update_task_progress(db, task, "running", message, result)
def _update_task_progress(
self,
db: Session,
@@ -428,13 +458,18 @@ class DouyuBatchRunnerCore:
payload = payload_raw if isinstance(payload_raw, dict) else {}
return {**payload, **self.payload}
def _client(self, cookie: str) -> DouyuActivityClient:
def _client(
self,
cookie: str,
*,
direct: bool = False,
) -> DouyuActivityClient:
"""任务 HTTP 客户端:写操作任务注入会话级代理(兑换/锁单/支付/充值/送礼),
读操作任务直连;见 DOUYU_PROXY_TASK_TYPES。"""
读操作任务直连;持久支付轮询显式 ``direct=True``,不能每轮取新代理。"""
return activity_client(
cookie,
logger_callback=lambda msg: self._push_log("debug", msg),
proxies=self._proxies_for_task(),
proxies=None if direct else self._proxies_for_task(),
)
def _sleep_interruptible(self, seconds: float) -> bool:
+194 -175
View File
@@ -3,7 +3,6 @@
from __future__ import annotations
import re
import time
from datetime import UTC, datetime
from decimal import Decimal
from typing import TYPE_CHECKING, Any, cast
@@ -20,7 +19,6 @@ from core.douyu import (
from ..models import Account, DouyuTask
from .business_events import record_recharge
from .douyu_activity_query import query_exchange_balance, query_gold_account
from .douyu_runner_core import DOUYU_PAYMENT_POLL_INTERVAL, DOUYU_PAYMENT_POLL_SECONDS
from .douyu_service import update_account_profile_from_cookie
@@ -33,7 +31,9 @@ class GoldMixin:
@staticmethod
def _to_int(value: Any) -> int | None: ...
def _client(self, cookie: str) -> DouyuActivityClient: ...
def _client(
self, cookie: str, *, direct: bool = False
) -> DouyuActivityClient: ...
def _task_payload(self, task: DouyuTask) -> dict: ...
def _push_log(self, level: str, message: str) -> None: ...
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
@@ -55,76 +55,84 @@ class GoldMixin:
"exchange_balance_query": exchange,
}
def _wait_gold_balance_after_payment(
def _poll_create_gold_qr_once(
self,
db: Session,
task: DouyuTask,
account: Account,
client: DouyuActivityClient,
result: dict,
baseline_gold: int | None,
) -> bool:
"""等待鱼翅充值到账;余额变化后写回账号表。"""
deadline = time.monotonic() + DOUYU_PAYMENT_POLL_SECONDS
cookie: str,
config: dict,
) -> None:
"""查询一次鱼翅扫码充值是否到账,然后释放 worker。"""
result = dict(task.result or {})
result["payment_polling"] = True
result["baseline_gold_balance"] = baseline_gold
poll_count = 0
last_gold = baseline_gold
baseline_ready = baseline_gold is not None
while not self._stop.is_set() and time.monotonic() <= deadline:
try:
balance_result = self._refresh_account_gold_balance(client, account)
db.commit()
poll_count += 1
last_gold = balance_result["gold_balance"]
result.update(balance_result)
result["payment_poll_count"] = poll_count
result["payment_polling"] = True
if not baseline_ready and last_gold is not None:
baseline_gold = last_gold
result["baseline_gold_balance"] = baseline_gold
baseline_ready = True
self._update_task_progress(
db,
task,
"running",
f"鱼翅支付码已生成,已记录当前余额 {last_gold},等待到账",
result,
)
if self._stop.wait(DOUYU_PAYMENT_POLL_INTERVAL):
break
continue
# ⚠️ 判据必须是 `>`(余额**增加**)而不是 `!=`。
# 原来用 `!=`,于是余额**下降**也算「到账」:批内并发(concurrency
# 最高 10)时同账号的送礼任务会消耗鱼翅,余额一变就被这个充值任务
# 读成「鱼翅已到账」,往台账里记一笔根本没发生的充值。
changed = last_gold is not None and (
baseline_gold is None or last_gold > baseline_gold
)
if changed:
result["payment_polling"] = False
result["gold_recharged"] = True
return True
self._update_task_progress(
result["payment_poll_count"] = int(result.get("payment_poll_count") or 0) + 1
try:
client = self._client(cookie, direct=True)
balance_result = self._refresh_account_gold_balance(client, account)
db.commit()
result.update(balance_result)
last_gold = balance_result["gold_balance"]
baseline_gold = self._to_int(result.get("baseline_gold_balance"))
if baseline_gold is None:
result["baseline_gold_balance"] = last_gold
self._schedule_payment_poll(
db,
task,
"running",
f"鱼翅支付码已生成,等待到账(当前鱼翅 {last_gold if last_gold is not None else '-'})",
result,
f"鱼翅支付码已生成,已记录当前余额 {last_gold if last_gold is not None else '-'},等待到账",
)
return
# 余额必须增加才算到账,下降可能来自同账号送礼等消费。
if last_gold is not None and last_gold > baseline_gold:
result.pop("payment_poll_error", None)
result["gold_recharged"] = True
result["payment_polling"] = False
amount = int(
result.get("payment_amount")
or self._task_payload(task).get("amount")
or self._task_payload(task).get("gold_amount")
or 1
)
channel = str(
result.get("payment_channel")
or config.get("gold_recharge_channel")
or "wechat_qr"
)
account.updated_at = datetime.now(UTC)
record_recharge(
db,
account,
task_id=task.id,
amount=amount,
channel=channel,
balance_after=account.gold_balance,
confirmed=True,
actor_id=int(task.created_by),
)
self._mark_task(
db,
task,
"success",
f"鱼翅已到账,当前余额: {account.gold_balance if account.gold_balance is not None else '-'}",
result,
)
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
poll_count += 1
result["payment_poll_count"] = poll_count
result["payment_poll_error"] = str(exc)
self._update_task_progress(
db, task, "running", f"等待鱼翅到账: {exc}", result
)
if self._stop.wait(DOUYU_PAYMENT_POLL_INTERVAL):
break
result["payment_polling"] = False
result["gold_recharged"] = False
result["gold_balance"] = last_gold
return False
return
result.pop("payment_poll_error", None)
self._schedule_payment_poll(
db,
task,
result,
f"鱼翅支付码已生成,等待到账(当前鱼翅 {last_gold if last_gold is not None else '-'})",
)
except Exception as exc: # noqa: BLE001 外部接口失败继续等待
result["payment_poll_error"] = str(exc)
self._schedule_payment_poll(
db,
task,
result,
f"等待鱼翅到账: {exc}",
)
@staticmethod
def _supplier_value(payload: dict, *keys: str):
@@ -184,65 +192,119 @@ class GoldMixin:
)
return f"DYGF{batch_token}T{task.id}"
def _wait_supplier_gold_order(
def _poll_create_gold_supplier_once(
self,
db: Session,
task: DouyuTask,
client: FishFinRechargeClient,
result: dict,
) -> int | None:
"""轮询供应商直充订单至结束状态。"""
account: Account,
amount: int,
) -> None:
"""查询一次供应商直充订单状态。"""
db.refresh(task)
if task.status in {"success", "failed", "error", "stopped"}:
return
task_id = int(task.id)
result = dict(task.result or {})
order_no = str(result["out_order_id"])
deadline = time.monotonic() + DOUYU_PAYMENT_POLL_SECONDS
poll_count = 0
result["payment_polling"] = True
while not self._stop.is_set() and time.monotonic() <= deadline:
try:
# 回调可能已在另一个数据库会话中结束订单,刷新后直接使用其结果。
db.refresh(task)
if task.status in {"success", "failed"}:
callback_result = (
task.result if isinstance(task.result, dict) else result
)
result.update(callback_result)
result["payment_polling"] = False
return self._supplier_order_status(callback_result)
payload = client.query_order(order_no)
code = self._to_int(self._supplier_value(payload, "code"))
status = self._supplier_order_status(payload)
poll_count += 1
result.update(
{
"payment_poll_count": poll_count,
"supplier_code": code,
"supplier_order_status": status,
"supplier_order": self._supplier_result(payload),
}
result["payment_poll_count"] = int(result.get("payment_poll_count") or 0) + 1
try:
client = FishFinRechargeClient(FishFinRechargeConfig.from_env())
payload = client.query_order(order_no)
code = self._to_int(self._supplier_value(payload, "code"))
status = self._supplier_order_status(payload)
result.update(
{
"supplier_code": code,
"supplier_order_status": status,
"supplier_order": self._supplier_result(payload),
}
)
# 查询期间供应商回调可能已经把任务收口。重新锁行后再决定是否写入,
# 避免长时间 HTTP 调用结束后用旧状态覆盖回调结果。
db.rollback()
locked = (
db.query(DouyuTask)
.filter(
DouyuTask.id == task_id,
DouyuTask.status == "running",
DouyuTask.queue_lane == "payment_poll",
)
if code != 200:
result["payment_polling"] = False
return status if status in {2, 3, 4} else 4
if status in {2, 3, 4}:
result["payment_polling"] = False
return status
self._update_task_progress(
.with_for_update()
.first()
)
if locked is None:
return
task = locked
locked_account = (
db.query(Account).filter(Account.id == task.account_id).first()
)
if locked_account is not None:
account = locked_account
if code != 200:
self._finish_supplier_gold_order(
db,
task,
"running",
f"供应商直充订单处理中(状态 {status if status is not None else '-'})",
account,
result,
amount=amount,
status=(status if status in {2, 3, 4} else 4),
)
except FishFinRechargeError as exc:
poll_count += 1
result["payment_poll_count"] = poll_count
result["payment_poll_error"] = str(exc)
self._update_task_progress(
db, task, "running", f"查询供应商订单失败: {exc}", result
return
if status in {2, 3, 4}:
self._finish_supplier_gold_order(
db,
task,
account,
result,
amount=amount,
status=status,
)
if self._stop.wait(DOUYU_PAYMENT_POLL_INTERVAL):
break
result["payment_polling"] = False
return None
return
result.pop("payment_poll_error", None)
self._schedule_payment_poll(
db,
task,
result,
f"供应商直充订单处理中(状态 {status if status is not None else '-'})",
)
except FishFinRechargeError as exc:
result["payment_poll_error"] = str(exc)
self._schedule_payment_poll(
db,
task,
result,
f"查询供应商订单失败: {exc}",
)
def _finish_supplier_gold_order(
self,
db: Session,
task: DouyuTask,
account: Account,
result: dict,
*,
amount: int,
status: int | None,
) -> None:
if status == 2:
account.updated_at = datetime.now(UTC)
record_recharge(
db,
account,
task_id=task.id,
amount=amount,
order_id=str(result.get("out_order_id") or ""),
supplier_order_id=str(result.get("order_id") or ""),
channel="supplier_api",
douyu_uid=str(result.get("douyu_uid") or ""),
balance_after=account.gold_balance,
actor_id=int(task.created_by),
confirmed=False,
)
self._mark_task(db, task, "success", "供应商直充成功", result)
return
self._mark_task(db, task, "failed", "供应商直充失败", result)
def _execute_create_gold_qr(
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
@@ -271,45 +333,19 @@ class GoldMixin:
result = client.create_gold_qr(
amount=amount, pay_type=int(config["gold_pay_type"])
)
result.update(
{
"payment_amount": amount,
"payment_channel": channel,
"baseline_gold_balance": baseline_gold,
}
)
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", f"鱼翅 {amount} 元支付码已生成,等待到账", result
)
recharged = self._wait_gold_balance_after_payment(
db, task, account, client, result, baseline_gold
)
if self._stop.is_set():
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if recharged:
account.updated_at = datetime.now(UTC)
# 金额用**本地变量 amount**:二维码链路的 result 里没有金额
# (create_gold_qr 只返回 pay_url/token/raw),旧收集器在这里恒写
# amount=None,导致 accounts.recharge_total 系统性少计。
record_recharge(
db,
account,
task_id=task.id,
amount=amount,
channel=channel,
balance_after=account.gold_balance,
confirmed=True,
actor_id=int(task.created_by),
)
self._mark_task(
db,
task,
"success",
f"鱼翅已到账,当前余额: {account.gold_balance if account.gold_balance is not None else '-'}",
result,
)
return
self._mark_task(
self._schedule_payment_poll(
db,
task,
"failed",
f"未检测到鱼翅到账,当前余额: {account.gold_balance if account.gold_balance is not None else '-'}",
result,
f"鱼翅 {amount} 元支付码已生成,等待到账",
)
def _execute_create_gold_supplier_order(
@@ -431,39 +467,22 @@ class GoldMixin:
)
return
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", "供应商直充订单已创建,等待到账", result
)
if status not in {2, 3, 4}:
status = self._wait_supplier_gold_order(db, task, client, result)
if self._stop.is_set():
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if status == 2:
account.updated_at = datetime.now(UTC)
# 幂等键与**供应商回调**(routers/douyu.py)完全一致 —— 两条路径都可能
# 观测到同一笔成功(_wait_supplier_gold_order 会采纳回调结果再 mark 一次),
# 同一个 source_ref 天然去重。
record_recharge(
if status in {2, 3, 4}:
self._finish_supplier_gold_order(
db,
task,
account,
task_id=task.id,
result,
amount=amount,
order_id=str(result.get("out_order_id") or ""),
supplier_order_id=str(result.get("order_id") or ""),
channel="supplier_api",
douyu_uid=str(result.get("douyu_uid") or ""),
balance_after=account.gold_balance,
actor_id=int(task.created_by),
# 供应商终态成功 ≠ 斗鱼余额已变,如实标注
confirmed=False,
status=status,
)
self._mark_task(db, task, "success", "供应商直充成功", result)
return
if status in {3, 4}:
self._mark_task(db, task, "failed", "供应商直充失败", result)
return
self._mark_task(db, task, "failed", "供应商直充订单查询超时", result)
self._schedule_payment_poll(
db,
task,
result,
"供应商直充订单已创建,等待到账",
)
def _execute_query_gold_balance(
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
+177 -218
View File
@@ -2,7 +2,6 @@
from __future__ import annotations
import time
from datetime import UTC, datetime
from typing import TYPE_CHECKING, Any
@@ -17,7 +16,6 @@ from .douyu_elite_status import (
elite_is_currently_open,
normalize_elite_expire_time,
)
from .douyu_runner_core import DOUYU_PAYMENT_POLL_INTERVAL, DOUYU_PAYMENT_POLL_SECONDS
from .douyu_service import account_uid, update_account_profile_from_cookie
@@ -30,11 +28,14 @@ class ManualMixin:
@staticmethod
def _to_int(value: Any) -> int | None: ...
def _client(self, cookie: str) -> DouyuActivityClient: ...
def _client(
self, cookie: str, *, direct: bool = False
) -> DouyuActivityClient: ...
def _task_payload(self, task: DouyuTask) -> dict: ...
def _push_log(self, level: str, message: str) -> None: ...
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
def _update_task_progress(self, *args: Any, **kwargs: Any) -> None: ...
def _schedule_payment_poll(self, *args: Any, **kwargs: Any) -> None: ...
@classmethod
def _confirm_act_alias(cls, config: dict) -> str: ...
@@ -136,71 +137,89 @@ class ManualMixin:
result,
)
def _wait_points_after_payment(
def _poll_create_elite_qr_once(
self,
db: Session,
task: DouyuTask,
account: Account,
client: DouyuActivityClient,
cookie: str,
ctn: str,
result: dict,
*,
baseline_points: int | None = None,
) -> bool:
"""等待宝典支付到账。
判定口径是「积分比开通前有增长」:旧口径 `积分 >= 300` 会让账上本来就
有 300+ 积分的账号在出码后第一轮轮询就被判成功,二维码刚弹出来就消失
(线上 2~4 秒 success 的案例都是这种)。拿不到基线时才退回旧口径。
"""
deadline = time.monotonic() + DOUYU_PAYMENT_POLL_SECONDS
config: dict,
) -> None:
"""查询一次精英宝典开通到账。"""
del config
result = dict(task.result or {})
result["payment_polling"] = True
result["payment_target_points"] = 300
result["elite_points_baseline"] = baseline_points
poll_count = 0
last_points = None
while not self._stop.is_set() and time.monotonic() <= deadline:
try:
points_result = self._refresh_account_points(
client, account, cookie, ctn=ctn
result["payment_poll_count"] = int(result.get("payment_poll_count") or 0) + 1
baseline_points = self._to_int(result.get("elite_points_baseline"))
try:
client = self._client(cookie, direct=True)
ctn = str(result.get("payment_ctn") or "")
if not ctn:
ctn = query_csrf(client, refresh_subscribe=True)
result["payment_ctn"] = ctn
points_result = self._refresh_account_points(
client, account, cookie, ctn=ctn
)
db.commit()
last_points = points_result["points"]
result.update(points_result)
arrived = last_points is not None and (
last_points > baseline_points
if baseline_points is not None
else last_points >= 300
)
if arrived:
result.pop("payment_poll_error", None)
result["payment_polling"] = False
result["elite_opened"] = True
account.elite_opened = True
account.updated_at = datetime.now(UTC)
spent = _points_spent(baseline_points, account.points)
emit(
db,
account_id=account.id,
event_type=EVENT_ELITE_OPENED,
title=f"精英宝典开通 -{spent}" if spent else "精英宝典开通",
occurred_at=account.updated_at,
amount=-spent if spent else None,
detail={
"points": account.points,
"expire_time": account.elite_expire_time,
"spent": spent,
},
source="manual",
actor_type="user",
actor_id=int(task.created_by),
source_ref=(
f"elite_opened:{account.id}:"
f"{int(account.updated_at.timestamp())}"
),
)
db.commit()
poll_count += 1
last_points = points_result["points"]
result.update(points_result)
result["payment_poll_count"] = poll_count
result["payment_polling"] = True
if last_points is not None:
arrived = (
last_points > baseline_points
if baseline_points is not None
else last_points >= 300
)
if arrived:
result["payment_polling"] = False
result["elite_opened"] = True
return True
self._update_task_progress(
self._mark_task(
db,
task,
"running",
f"精英宝典支付码已生成,等待开通到账(当前积分 {last_points if last_points is not None else '-'})",
"success",
f"精英宝典已开通,积分: {account.points}",
result,
)
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
poll_count += 1
result["payment_poll_count"] = poll_count
result["payment_poll_error"] = str(exc)
self._update_task_progress(
db, task, "running", f"等待开通到账: {exc}", result
)
if self._stop.wait(DOUYU_PAYMENT_POLL_INTERVAL):
break
result["payment_polling"] = False
result["elite_opened"] = False
result["points"] = last_points
return False
return
result.pop("payment_poll_error", None)
self._schedule_payment_poll(
db,
task,
result,
"精英宝典支付码已生成,等待开通到账"
f"(当前积分 {last_points if last_points is not None else '-'})",
)
except Exception as exc: # noqa: BLE001 外部接口失败继续等待
result["payment_poll_error"] = str(exc)
self._schedule_payment_poll(
db,
task,
result,
f"等待精英宝典开通到账: {exc}",
)
def _refresh_esports_handbook(
self,
@@ -238,76 +257,97 @@ class ManualMixin:
"points": manual_score,
}
def _wait_esports_open_after_payment(
def _poll_create_esports_qr_once(
self,
db: Session,
task: DouyuTask,
account: Account,
client: DouyuActivityClient,
*,
manual_id: str,
result: dict,
baseline_manual_type: int | None,
baseline_manual_score: int | None,
) -> bool:
"""等待电竞手册支付到账,以 manualType=1 或积分变化作为成功条件。"""
deadline = time.monotonic() + DOUYU_PAYMENT_POLL_SECONDS
cookie: str,
config: dict,
) -> None:
"""查询一次电竞手册开通到账。"""
result = dict(task.result or {})
result["payment_polling"] = True
result["esports_manual_type_baseline"] = baseline_manual_type
result["esports_manual_score_baseline"] = baseline_manual_score
poll_count = 0
last_manual_type = baseline_manual_type
last_manual_score = baseline_manual_score
while not self._stop.is_set() and time.monotonic() <= deadline:
try:
handbook_result = self._refresh_esports_handbook(
client,
account,
manual_id=manual_id,
result["payment_poll_count"] = int(result.get("payment_poll_count") or 0) + 1
manual_id = str(
result.get("payment_manual_id") or config.get("esports_manual_id") or ""
).strip()
baseline_manual_score = self._to_int(
result.get("esports_manual_score_baseline")
)
if not manual_id:
self._mark_task(
db,
task,
"failed",
"请先配置电竞手册 manualID",
result,
)
return
try:
client = self._client(cookie, direct=True)
handbook_result = self._refresh_esports_handbook(
client,
account,
manual_id=manual_id,
)
db.commit()
last_manual_type = handbook_result["esports_manual_type"]
last_manual_score = handbook_result["esports_manual_score"]
result.update(handbook_result)
opened = (last_manual_type is not None and last_manual_type >= 1) or (
baseline_manual_score is not None
and last_manual_score is not None
and last_manual_score > baseline_manual_score
)
if opened:
result.pop("payment_poll_error", None)
result["payment_polling"] = False
result["esports_opened"] = True
account.updated_at = datetime.now(UTC)
spent = _points_spent(baseline_manual_score, account.esports_points)
emit(
db,
account_id=account.id,
event_type=EVENT_ESPORTS_OPENED,
title=f"电竞手册开通 -{spent}" if spent else "电竞手册开通",
occurred_at=account.updated_at,
amount=-spent if spent else None,
detail={"points": account.esports_points, "spent": spent},
source="manual",
actor_type="user",
actor_id=int(task.created_by),
source_ref=(
f"esports_opened:{account.id}:"
f"{int(account.updated_at.timestamp())}"
),
)
db.commit()
poll_count += 1
last_manual_type = handbook_result["esports_manual_type"]
last_manual_score = handbook_result["esports_manual_score"]
result.update(handbook_result)
result["payment_poll_count"] = poll_count
result["payment_polling"] = True
opened = (last_manual_type is not None and last_manual_type >= 1) or (
baseline_manual_score is not None
and last_manual_score is not None
and last_manual_score > baseline_manual_score
)
if opened:
result["payment_polling"] = False
result["esports_opened"] = True
return True
self._update_task_progress(
self._mark_task(
db,
task,
"running",
"电竞手册支付码已生成,等待开通到账"
f"(类型 {last_manual_type if last_manual_type is not None else '-'},"
f"积分 {last_manual_score if last_manual_score is not None else '-'})",
"success",
"电竞手册已开通,积分: "
f"{account.esports_points if account.esports_points is not None else '-'}",
result,
)
except Exception as exc: # noqa: BLE001 外部接口与任务边界需要保留宽泛异常兜底
poll_count += 1
result["payment_poll_count"] = poll_count
result["payment_poll_error"] = str(exc)
self._update_task_progress(
db, task, "running", f"等待电竞手册到账: {exc}", result
)
if self._stop.wait(DOUYU_PAYMENT_POLL_INTERVAL):
break
result["payment_polling"] = False
result["esports_opened"] = False
result["esports_manual_type"] = last_manual_type
result["esports_manual_score"] = last_manual_score
result["esports_points"] = last_manual_score
result["points"] = last_manual_score
return False
return
result.pop("payment_poll_error", None)
self._schedule_payment_poll(
db,
task,
result,
"电竞手册支付码已生成,等待开通到账"
f"(类型 {last_manual_type if last_manual_type is not None else '-'},"
f"积分 {last_manual_score if last_manual_score is not None else '-'})",
)
except Exception as exc: # noqa: BLE001 外部接口失败继续等待
result["payment_poll_error"] = str(exc)
self._schedule_payment_poll(
db,
task,
result,
f"等待电竞手册开通到账: {exc}",
)
def _execute_create_elite_qr(
self, db: Session, task: DouyuTask, account: Account, cookie: str, config: dict
@@ -378,60 +418,20 @@ class ManualMixin:
amount=int(config["elite_amount"]),
room_id=str(config["room_id"]),
)
result.update(
{
"payment_ctn": ctn,
"elite_points_baseline": baseline_points,
"payment_manual_id": manual_id,
"payment_rid": str(config.get("rid") or ""),
}
)
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", "精英宝典支付码已生成,等待开通到账", result
)
opened = self._wait_points_after_payment(
self._schedule_payment_poll(
db,
task,
account,
client,
cookie,
ctn,
result,
baseline_points=baseline_points,
)
if self._stop.is_set():
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if opened:
account.elite_opened = True
account.updated_at = datetime.now(UTC)
# elite_opened 是**覆盖式**布尔值:开通之后再操作也看不出「什么时候开的」。
# 在这里记一条事件,补上开通时间。
# 开通是**资产减少**(花积分)。只记余额、不记花掉多少的话,
# 账号的积分流水就对不上账 —— 进项有金额、出项没有。
spent = _points_spent(baseline_points, account.points)
emit(
db,
account_id=account.id,
event_type=EVENT_ELITE_OPENED,
title=f"精英宝典开通 -{spent}" if spent else "精英宝典开通",
occurred_at=account.updated_at,
amount=-spent if spent else None,
detail={
"points": account.points,
"expire_time": account.elite_expire_time,
"spent": spent,
},
source="manual",
actor_type="user",
actor_id=int(task.created_by),
source_ref=(
f"elite_opened:{account.id}:{int(account.updated_at.timestamp())}"
),
)
self._mark_task(
db, task, "success", f"精英宝典已开通,积分: {account.points}", result
)
return
self._mark_task(
db,
task,
"failed",
f"未检测到精英宝典开通到账,当前积分: {account.points if account.points is not None else '-'}",
result,
"精英宝典支付码已生成,等待开通到账",
)
def _execute_create_esports_qr(
@@ -487,62 +487,21 @@ class ManualMixin:
amount=int(config["esports_amount"]),
room_id=str(config["room_id"]),
)
result.update(baseline_result)
result.update(
{
**baseline_result,
"payment_ctn": ctn,
"payment_manual_id": manual_id,
"esports_manual_type_baseline": baseline_manual_type,
"esports_manual_score_baseline": baseline_manual_score,
}
)
account.updated_at = datetime.now(UTC)
self._update_task_progress(
db, task, "running", "电竞手册支付码已生成,等待开通到账", result
)
opened = self._wait_esports_open_after_payment(
self._schedule_payment_poll(
db,
task,
account,
client,
manual_id=manual_id,
result=result,
baseline_manual_type=baseline_manual_type,
baseline_manual_score=baseline_manual_score,
)
if self._stop.is_set():
self._mark_task(db, task, "stopped", "任务已停止", result)
return
if opened:
account.updated_at = datetime.now(UTC)
# 与精英宝典同理:状态字段是覆盖式的,这里补上「什么时候开的」。
# 注意只在**真正开通**这一支发事件;上面「基线查到已开通」那一支是检测,
# 每次都发会刷屏。
# 同理:开通电竞手册也要记清花了多少积分
spent = _points_spent(baseline_manual_score, account.esports_points)
emit(
db,
account_id=account.id,
event_type=EVENT_ESPORTS_OPENED,
title=f"电竞手册开通 -{spent}" if spent else "电竞手册开通",
occurred_at=account.updated_at,
amount=-spent if spent else None,
detail={"points": account.esports_points, "spent": spent},
source="manual",
actor_type="user",
actor_id=int(task.created_by),
source_ref=(
f"esports_opened:{account.id}:{int(account.updated_at.timestamp())}"
),
)
self._mark_task(
db,
task,
"success",
f"电竞手册已开通,积分: {account.esports_points if account.esports_points is not None else '-'}",
result,
)
return
self._mark_task(
db,
task,
"failed",
"未检测到电竞手册开通到账"
f",类型: {result.get('esports_manual_type', '-')},"
f"积分: {result.get('esports_manual_score', '-')}",
result,
"电竞手册支付码已生成,等待开通到账",
)
def _execute_query_esports_points(
+18 -10
View File
@@ -469,17 +469,25 @@ def cleanup_orphan_douyu_tasks(
query = query.filter(~DouyuTask.batch_id.in_(list(active_batch_ids)))
now = datetime.now(UTC)
# Scan waits are durable. A restarted scheduler can resume them from the
# database, so startup cleanup must not turn them into stopped tasks.
query = query.filter(
or_(
DouyuTask.task_type != "get_bind_qr",
DouyuTask.queue_lane != "binding_poll",
DouyuTask.next_run_at.is_(None),
DouyuTask.run_deadline_at.is_(None),
DouyuTask.run_deadline_at <= now,
)
# Scan and payment waits are durable. A restarted scheduler can resume them
# from the database, so startup cleanup must not stop unexpired rows.
durable_binding = and_(
DouyuTask.task_type == "get_bind_qr",
DouyuTask.queue_lane == "binding_poll",
DouyuTask.next_run_at.isnot(None),
DouyuTask.run_deadline_at.isnot(None),
DouyuTask.run_deadline_at > now,
)
durable_payment = and_(
DouyuTask.task_type.in_(
{"create_elite_qr", "create_esports_qr", "create_gold_qr"}
),
DouyuTask.queue_lane == "payment_poll",
DouyuTask.next_run_at.isnot(None),
DouyuTask.run_deadline_at.isnot(None),
DouyuTask.run_deadline_at > now,
)
query = query.filter(~or_(durable_binding, durable_payment))
tasks = query.all()
if not tasks:
@@ -0,0 +1,269 @@
"""Database-backed scheduler for generated payment QR codes."""
from __future__ import annotations
import os
import threading
from concurrent.futures import Future
from datetime import UTC, datetime, timedelta
from loguru import logger
from sqlalchemy.orm import joinedload
from ..database import WorkerSessionLocal
from ..models import DouyuTask
from .douyu_runner import DouyuBatchRunner
from .douyu_runner_core import DOUYU_PAYMENT_POLL_INTERVAL
from .task_runtime import TaskLane, submit
DEFAULT_INTERVAL_SECONDS = 1
DEFAULT_BATCH_SIZE = 30
CLAIM_SECONDS = 60
PAYMENT_TASK_TYPES = frozenset(
{
"create_elite_qr",
"create_esports_qr",
"create_gold_qr",
}
)
_stop_event = threading.Event()
_thread: threading.Thread | None = None
_lock = threading.Lock()
_in_flight: set[int] = set()
_in_flight_lock = threading.Lock()
def _env_int(name: str, default: int, minimum: int, maximum: int) -> int:
try:
value = int(os.getenv(name, str(default)))
except ValueError:
return default
return max(minimum, min(maximum, value))
def _claim_due_payment_tasks(limit: int) -> list[int]:
db = WorkerSessionLocal()
now = datetime.now(UTC)
claimed: list[int] = []
try:
rows = (
db.query(DouyuTask)
.filter(
DouyuTask.task_type.in_(PAYMENT_TASK_TYPES),
DouyuTask.status == "running",
DouyuTask.queue_lane == "payment_poll",
DouyuTask.next_run_at.isnot(None),
DouyuTask.next_run_at <= now,
DouyuTask.run_deadline_at.isnot(None),
)
.order_by(DouyuTask.next_run_at.asc(), DouyuTask.id.asc())
.with_for_update(skip_locked=True)
.limit(limit)
.all()
)
for row in rows:
task_id = int(row.id)
with _in_flight_lock:
if task_id in _in_flight:
continue
_in_flight.add(task_id)
row.next_run_at = now + timedelta(seconds=CLAIM_SECONDS)
row.last_run_at = now
row.attempt_count = int(row.attempt_count or 0) + 1
claimed.append(task_id)
db.commit()
return claimed
except Exception:
db.rollback()
with _in_flight_lock:
for task_id in claimed:
_in_flight.discard(task_id)
raise
finally:
db.close()
def _reschedule_after_error(
db,
task: DouyuTask,
account,
exc: Exception,
) -> None:
result = dict(task.result or {})
result["payment_poll_error"] = str(exc)
runner = DouyuBatchRunner(
db,
task.batch_id,
task.task_type,
close_db=False,
)
runner._schedule_payment_poll(
db,
task,
result,
f"支付到账查询失败,稍后重试: {exc}",
delay_seconds=DOUYU_PAYMENT_POLL_INTERVAL,
)
if account is not None:
logger.warning(
"[douyu] 支付轮询失败 task_id={} account_id={}: {}",
task.id,
account.id,
exc,
)
def poll_payment_task_once(task_id: int) -> None:
db = WorkerSessionLocal()
task = None
account = None
try:
task = (
db.query(DouyuTask)
.options(joinedload(DouyuTask.account))
.filter(DouyuTask.id == int(task_id))
.first()
)
if (
task is None
or task.status != "running"
or task.task_type not in PAYMENT_TASK_TYPES
or task.queue_lane != "payment_poll"
):
return
account = task.account
if account is None:
DouyuBatchRunner(
db,
task.batch_id,
task.task_type,
close_db=False,
)._mark_task(
db,
task,
"failed",
"账号不存在,无法继续查询支付到账",
dict(task.result or {}),
)
return
runner = DouyuBatchRunner(
db,
task.batch_id,
task.task_type,
close_db=False,
)
now = datetime.now(UTC)
if task.run_deadline_at is not None and task.run_deadline_at <= now:
runner.mark_payment_poll_timeout(db, task)
return
config = runner._config_info(db)
runner.poll_payment_task_once(db, task, account, config)
except Exception as exc: # noqa: BLE001 - durable row is retried after backoff
db.rollback()
if task is not None:
try:
db.refresh(task)
if task.status == "running" and task.queue_lane == "payment_poll":
_reschedule_after_error(db, task, account, exc)
except Exception: # noqa: BLE001 - scheduler must survive one bad row
logger.exception("[douyu] 支付轮询失败后重排也失败 task_id={}", task_id)
finally:
db.close()
with _in_flight_lock:
_in_flight.discard(int(task_id))
def _release_callback(future: Future) -> None:
try:
future.result()
except Exception as exc: # noqa: BLE001 - worker already logs; keep scheduler alive
logger.warning("支付到账查询任务异常: {}", exc)
def run_payment_poll_dispatch_once(limit: int = DEFAULT_BATCH_SIZE) -> int:
claimed = _claim_due_payment_tasks(max(1, limit))
submitted = 0
for task_id in claimed:
future = submit(
TaskLane.PAYMENT_POLL,
poll_payment_task_once,
task_id,
stopped=_stop_event.is_set,
)
if future is None:
with _in_flight_lock:
_in_flight.discard(task_id)
break
future.add_done_callback(_release_callback)
submitted += 1
return submitted
def _run() -> None:
interval = _env_int(
"PAYMENT_POLL_DISPATCH_SECONDS",
DEFAULT_INTERVAL_SECONDS,
1,
30,
)
while not _stop_event.is_set():
try:
batch_size = _env_int(
"PAYMENT_POLL_DISPATCH_BATCH",
DEFAULT_BATCH_SIZE,
1,
200,
)
run_payment_poll_dispatch_once(batch_size)
except Exception as exc: # noqa: BLE001 - scheduler survives any one bad tick
logger.warning("支付到账调度轮次失败: {}", exc)
_stop_event.wait(interval)
def start_payment_poll_scheduler() -> bool:
global _thread
with _lock:
if _thread is not None and _thread.is_alive():
return False
_stop_event.clear()
_thread = threading.Thread(
target=_run,
name="payment-poll-scheduler",
daemon=True,
)
_thread.start()
logger.info("支付到账定时调度器已启动")
return True
def stop_payment_poll_scheduler() -> None:
global _thread
_stop_event.set()
with _lock:
thread = _thread
_thread = None
if thread is not None and thread.is_alive():
thread.join(timeout=3)
def scheduler_status() -> dict[str, object]:
with _lock:
running = _thread is not None and _thread.is_alive()
with _in_flight_lock:
in_flight = len(_in_flight)
return {
"running": running,
"in_flight": in_flight,
"claim_seconds": CLAIM_SECONDS,
}
__all__ = [
"PAYMENT_TASK_TYPES",
"poll_payment_task_once",
"run_payment_poll_dispatch_once",
"scheduler_status",
"start_payment_poll_scheduler",
"stop_payment_poll_scheduler",
]
+9 -3
View File
@@ -23,9 +23,11 @@ class TaskLane(StrEnum):
LOGIN = "login"
ACCOUNT_CHECK = "account_check"
AUTOMATION = "automation"
OVERVIEW_QUERY = "overview_query"
QUERY_BATCH = "query_batch"
COLLAB_RECOMMEND = "collab_recommend"
PAYMENT_WAIT = "payment_wait"
PAYMENT_QR = "payment_qr"
PAYMENT_POLL = "payment_poll"
OPS_WRITE = "ops_write"
HUYA_BATCH = "huya_batch"
HUYA_REGISTER = "huya_register"
@@ -53,9 +55,11 @@ _DEFAULT_WORKERS: dict[TaskLane, int] = {
TaskLane.LOGIN: 4,
TaskLane.ACCOUNT_CHECK: 4,
TaskLane.AUTOMATION: 2,
TaskLane.OVERVIEW_QUERY: _env_int("DOUYU_MAX_ACTIVE_OVERVIEW_TASKS", 5),
TaskLane.QUERY_BATCH: 3,
TaskLane.COLLAB_RECOMMEND: 2,
TaskLane.PAYMENT_WAIT: 2,
TaskLane.PAYMENT_QR: 10,
TaskLane.PAYMENT_POLL: 3,
TaskLane.OPS_WRITE: 2,
TaskLane.HUYA_BATCH: 2,
TaskLane.HUYA_REGISTER: 2,
@@ -154,6 +158,8 @@ def shutdown(wait: bool = False) -> None:
def douyu_lane_for_task(task_type: str) -> TaskLane:
if task_type == "query_account_overview":
return TaskLane.OVERVIEW_QUERY
if task_type in {
"get_bind_qr",
"confirm_bind",
@@ -171,7 +177,7 @@ def douyu_lane_for_task(task_type: str) -> TaskLane:
"create_esports_qr",
"create_gold_qr",
}:
return TaskLane.PAYMENT_WAIT
return TaskLane.PAYMENT_QR
if task_type in {
"lock_goods",
"pay_locked_order",
@@ -992,7 +992,9 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
// 支付码批次尽量一次全出码,队列只负责展示和等待到账,不让人工等前一个付完才生成下一个。
const batchConcurrency = DOUYU_PAYMENT_TASK_TYPE_SET.has(taskType)
? Math.min(10, Math.max(concurrency, ids.length))
: concurrency;
: taskType === 'query_account_overview'
? Math.min(5, Math.max(concurrency, ids.length))
: Math.min(5, concurrency);
try {
const result = await douyuApi.createTasks({
account_ids: ids,