fix(douyu): 稳定绑定轮询并留档角色接口响应

This commit is contained in:
yml
2026-11-21 22:35:03 +08:00
parent 7f7943a1e8
commit 2c11a784d2
15 changed files with 1110 additions and 36 deletions
+4 -3
View File
@@ -95,8 +95,8 @@ DOUYU_MAX_ACTIVE_OVERVIEW_TASKS=5
TASK_LANE_BATCH_COORDINATOR_WORKERS=2 TASK_LANE_BATCH_COORDINATOR_WORKERS=2
TASK_LANE_BATCH_COORDINATOR_MAX_IN_FLIGHT=2 TASK_LANE_BATCH_COORDINATOR_MAX_IN_FLIGHT=2
TASK_LANE_INTERACTIVE_BINDING_WORKERS=3 TASK_LANE_INTERACTIVE_BINDING_WORKERS=3
TASK_LANE_BINDING_POLL_WORKERS=2 TASK_LANE_BINDING_POLL_WORKERS=6
TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=2 TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=6
TASK_LANE_LOGIN_WORKERS=4 TASK_LANE_LOGIN_WORKERS=4
TASK_LANE_ACCOUNT_CHECK_WORKERS=4 TASK_LANE_ACCOUNT_CHECK_WORKERS=4
TASK_LANE_OVERVIEW_QUERY_WORKERS=5 TASK_LANE_OVERVIEW_QUERY_WORKERS=5
@@ -208,7 +208,8 @@ PLATFORM_FULFILLMENT_MATCH_MODE=hybrid
# 订单 Webhook 连续唤醒时保持最小选号间隔,避免忙轮询压满数据库。 # 订单 Webhook 连续唤醒时保持最小选号间隔,避免忙轮询压满数据库。
PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=5 PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=5
# 客户绑定二维码有效期 # 客户绑定二维码有效期
PLATFORM_BINDING_QR_TTL_SECONDS=600 DOUYU_BIND_ROLE_POLL_SECONDS=1200
PLATFORM_BINDING_QR_TTL_SECONDS=1260
# Uvicorn reload(开发模式设为 true,生产环境保持 false) # Uvicorn reload(开发模式设为 true,生产环境保持 false)
UVICORN_RELOAD=false UVICORN_RELOAD=false
+4 -3
View File
@@ -55,8 +55,8 @@ services:
- TASK_LANE_BATCH_COORDINATOR_WORKERS=${TASK_LANE_BATCH_COORDINATOR_WORKERS:-2} - TASK_LANE_BATCH_COORDINATOR_WORKERS=${TASK_LANE_BATCH_COORDINATOR_WORKERS:-2}
- TASK_LANE_BATCH_COORDINATOR_MAX_IN_FLIGHT=${TASK_LANE_BATCH_COORDINATOR_MAX_IN_FLIGHT:-2} - TASK_LANE_BATCH_COORDINATOR_MAX_IN_FLIGHT=${TASK_LANE_BATCH_COORDINATOR_MAX_IN_FLIGHT:-2}
- TASK_LANE_INTERACTIVE_BINDING_WORKERS=${TASK_LANE_INTERACTIVE_BINDING_WORKERS:-3} - TASK_LANE_INTERACTIVE_BINDING_WORKERS=${TASK_LANE_INTERACTIVE_BINDING_WORKERS:-3}
- TASK_LANE_BINDING_POLL_WORKERS=${TASK_LANE_BINDING_POLL_WORKERS:-2} - TASK_LANE_BINDING_POLL_WORKERS=${TASK_LANE_BINDING_POLL_WORKERS:-6}
- TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=${TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT:-2} - TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=${TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT:-6}
- TASK_LANE_LOGIN_WORKERS=${TASK_LANE_LOGIN_WORKERS:-4} - TASK_LANE_LOGIN_WORKERS=${TASK_LANE_LOGIN_WORKERS:-4}
- TASK_LANE_ACCOUNT_CHECK_WORKERS=${TASK_LANE_ACCOUNT_CHECK_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_OVERVIEW_QUERY_WORKERS=${TASK_LANE_OVERVIEW_QUERY_WORKERS:-5}
@@ -120,7 +120,8 @@ services:
- ACCOUNT_COMMODITY_STATE_TTL_SECONDS=${ACCOUNT_COMMODITY_STATE_TTL_SECONDS:-900} - ACCOUNT_COMMODITY_STATE_TTL_SECONDS=${ACCOUNT_COMMODITY_STATE_TTL_SECONDS:-900}
- PLATFORM_FULFILLMENT_MATCH_MODE=${PLATFORM_FULFILLMENT_MATCH_MODE:-hybrid} - PLATFORM_FULFILLMENT_MATCH_MODE=${PLATFORM_FULFILLMENT_MATCH_MODE:-hybrid}
- PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=${PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS:-5} - PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS=${PLATFORM_FULFILLMENT_AUTO_ALLOCATE_MIN_INTERVAL_SECONDS:-5}
- PLATFORM_BINDING_QR_TTL_SECONDS=${PLATFORM_BINDING_QR_TTL_SECONDS:-600} - DOUYU_BIND_ROLE_POLL_SECONDS=${DOUYU_BIND_ROLE_POLL_SECONDS:-1200}
- PLATFORM_BINDING_QR_TTL_SECONDS=${PLATFORM_BINDING_QR_TTL_SECONDS:-1260}
# 用户上传图片的桶(在线协作 / P2 资料收集共用;生产用内网 endpoint)。 # 用户上传图片的桶(在线协作 / P2 资料收集共用;生产用内网 endpoint)。
# OSS_MODE=oss 才走对象存储,否则落容器内 ./data/file_storage。 # OSS_MODE=oss 才走对象存储,否则落容器内 ./data/file_storage。
- OSS_MODE=${OSS_MODE:-local} - OSS_MODE=${OSS_MODE:-local}
+54 -3
View File
@@ -104,7 +104,9 @@
-> 找动作 alias 上的可确认 pending -> 找动作 alias 上的可确认 pending
-> 生成二维码 -> 生成二维码
-> 持久化 baseline、候选来源、判定原因 -> 持久化 baseline、候选来源、判定原因
-> binding_poll 每次只查一次角色 -> binding_poll 每轮先查 legacy(cjm)
-> 角色/状态变化、每 3 轮或最后一轮再补查动作 alias(QYOOB)
-> 官方直接返回 bindAct=1 时立即结束为 confirmed
-> 找到确认条件:任务 success + bind_ready_for_confirm=true -> 找到确认条件:任务 success + bind_ready_for_confirm=true
-> actBind -> actBind
-> 回查 bindAct=1 -> 回查 bindAct=1
@@ -112,7 +114,20 @@
``` ```
二维码任务不能在 worker 内长时间阻塞;出码后写 `binding_poll`、`next_run_at` 和 二维码任务不能在 worker 内长时间阻塞;出码后写 `binding_poll`、`next_run_at` 和
`run_deadline_at`,由数据库调度器继续轮询。 `run_deadline_at`,由数据库调度器继续轮询。轮询窗口为 20 分钟,平台展示的二维码
TTL 为 21 分钟,留 1 分钟收尾余量。
轮询间隔按任务生命周期退避:
| 已等待时间 | 间隔 |
| --- | --- |
| 0-2 分钟 | 5 秒 |
| 2-10 分钟 | 10 秒 |
| 10-20 分钟 | 20 秒 |
`binding_poll` 有 6 个独立执行槽,但不会每轮同时请求所有 alias。生产常态是每轮
只查 `cjm`,动作 alias 仅在 legacy 状态变化、每 3 轮巡检和最后一轮补查。这样既能
承接十几个客服并发扫码,也不会把几十个二维码放大成持续的双倍官方请求。
### 确认前保护 ### 确认前保护
@@ -152,6 +167,20 @@
- `pending_role_name` - `pending_role_name`
- `bind_summary` - `bind_summary`
### 角色接口原始响应留档
关键角色接口的原始响应写入 `douyu_role_api_logs`,用于还原不同 alias 在
扫码、待确认、确认和回查各阶段到底返回了什么。记录内容:
- 账号、任务、作用域、阶段和 alias;
- `role_name`、`bindAct`、`bindRole`、`needBindAct`、`needBindRole`、
换绑倒计时和可换绑时间;
- 请求摘要与原始响应 JSON;
- 入库前的 Cookie、token、CSRF 等敏感键脱敏结果。
轮询不会把相同响应每 5 秒重复入库,只在 legacy 状态变化、动作 alias 响应变化、
二维码预检、确认前、确认接口返回和确认后回查时写入。
建议的生产只读查询: 建议的生产只读查询:
```sql ```sql
@@ -165,6 +194,18 @@ ORDER BY id DESC
LIMIT 20; LIMIT 20;
``` ```
角色接口原始响应查询:
```sql
SELECT id, task_id, stage, act_alias, role_name,
is_bound_act, need_bind_act, change_role_wait_time,
response_json, created_at
FROM douyu_role_api_logs
WHERE account_id = ?
ORDER BY id DESC
LIMIT 100;
```
日志检索: 日志检索:
```bash ```bash
@@ -231,6 +272,16 @@ docker compose logs douyu-login | grep '确认前状态'
回归:`tests/test_douyu_binding_state.py`、`tests/test_douyu_account_probe.py`、 回归:`tests/test_douyu_binding_state.py`、`tests/test_douyu_account_probe.py`、
`tests/test_douyu_task_concurrency.py`。 `tests/test_douyu_task_concurrency.py`。
### 2026-11-21:扫码成功但轮询超时、确认按钮不稳定
生产失败任务中大量出现 `role_timeout`,但 `result` 实际已经带回角色,且部分
`bindAct=1`。原因是 10 分钟轮询窗口偏短、每轮无条件查两个 alias,以及只把
`bindAct=0 + bindRole=1` 当成功。
修复:轮询窗口延长到 20 分钟;分阶段退避;legacy 每轮查询、动作 alias 按变化/
周期/末轮补查;直接 `bindAct=1` 立即收敛;前端自动轮询期间仍允许“立即查询角色”;
关键接口响应进入 `douyu_role_api_logs`。
### 历史固定规则 ### 历史固定规则
- 不要把 `bindRole=1` 当成已绑定; - 不要把 `bindRole=1` 当成已绑定;
@@ -243,4 +294,4 @@ docker compose logs douyu-login | grep '确认前状态'
| 日期 | 变更 | | 日期 | 变更 |
| --- | --- | | --- | --- |
| 2026-11-21 | 建立斗鱼协议总览;补充多 alias 职责、pending 真值表、结构化判定日志和同名 pending 修复记录 | | 2026-11-21 | 建立斗鱼协议总览;补充多 alias 职责、pending 真值表、结构化判定日志、轮询退避、直接绑定识别和角色接口原始响应留档 |
+299 -1
View File
@@ -9,7 +9,7 @@ from sqlalchemy import text
from tests.support.db import make_test_session from tests.support.db import make_test_session
from web.backend.database import engine as app_engine from web.backend.database import engine as app_engine
from web.backend.main import health from web.backend.main import health
from web.backend.models import Account, DouyuTask, User from web.backend.models import Account, DouyuRoleApiLog, DouyuTask, User
from web.backend.routers import douyu as douyu_router from web.backend.routers import douyu as douyu_router
from web.backend.schemas import DouyuTaskBatchRequest from web.backend.schemas import DouyuTaskBatchRequest
from web.backend.services import ( from web.backend.services import (
@@ -20,7 +20,9 @@ from web.backend.services import (
douyu_service, douyu_service,
payment_poll_scheduler, payment_poll_scheduler,
) )
from web.backend.services.douyu_role_audit import record_role_api_candidates
from web.backend.services.douyu_runner import DouyuBatchRunner from web.backend.services.douyu_runner import DouyuBatchRunner
from web.backend.services.douyu_runner_bind import bind_poll_interval_seconds
from web.backend.services.douyu_runner_core import ( from web.backend.services.douyu_runner_core import (
DouyuBatchRunnerCore, DouyuBatchRunnerCore,
slim_douyu_task_result_for_storage, slim_douyu_task_result_for_storage,
@@ -187,6 +189,77 @@ def test_cleanup_preserves_recoverable_binding_poll(db):
assert task.queue_lane == "binding_poll" assert task.queue_lane == "binding_poll"
@pytest.mark.parametrize(
("elapsed", "expected"),
[
(0, 5),
(119, 5),
(120, 10),
(599, 10),
(600, 20),
(1200, 20),
],
)
def test_binding_poll_interval_backs_off_by_elapsed_time(elapsed, expected):
assert bind_poll_interval_seconds(elapsed) == expected
def test_role_api_audit_redacts_secrets_and_keeps_raw_role_payload(db):
_, account, task = _fixtures(db, status="running")
written = record_role_api_candidates(
db,
account_id=account.id,
task_id=task.id,
candidates=[
{
"act_alias": "bind-act",
"api_version": "v1",
"role_name": "new-role",
"is_bound_act": True,
"raw": {
"cookie": "should-not-be-stored",
"data": {
"bindAct": 1,
"gameRole": {"roleName": "new-role"},
},
},
}
],
scope="elite",
stage="test",
source="pytest",
)
db.commit()
row = db.query(DouyuRoleApiLog).one()
assert written == 1
assert row.account_id == account.id
assert row.task_id == task.id
assert row.role_name == "new-role"
assert row.is_bound_act is True
assert row.response_json["cookie"] == "<redacted>"
assert row.response_json["data"]["bindAct"] == 1
def test_cleanup_gives_binding_poll_a_final_poll_grace(db):
_, _, 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(seconds=30)
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"
def test_cleanup_preserves_recoverable_payment_poll(db): def test_cleanup_preserves_recoverable_payment_poll(db):
_, _, task = _fixtures(db, status="running") _, _, task = _fixtures(db, status="running")
task.task_type = "create_gold_qr" task.task_type = "create_gold_qr"
@@ -454,6 +527,230 @@ def test_binding_poll_once_finishes_when_role_is_found(db, monkeypatch):
assert task.result["bind_ready_for_confirm"] is True assert task.result["bind_ready_for_confirm"] is True
def test_binding_poll_checks_action_alias_only_on_change_or_cadence(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)
legacy = {
"act_alias": "cjm",
"role_name": "old-role",
"is_bound_act": True,
"is_bound_role": True,
"raw": {"data": {"bindAct": 1}},
}
activity = {
"act_alias": "20260120QYOOB",
"role_name": "old-role",
"is_bound_act": True,
"is_bound_role": True,
"raw": {"data": {"bindAct": 1}},
}
task.result = {
"url": "https://example.test/qr",
"query_act_aliases": ["cjm", "20260120QYOOB"],
"baseline_role_name": "old-role",
"baseline_is_bound_act": True,
"before_bind_info": activity,
"bind_poll_legacy_snapshot": {
"role_name": "old-role",
"area_name": "",
"plat_name": "",
"is_bound_act": True,
"is_bound_role": True,
"need_bind_act": False,
"need_bind_role": False,
},
"bind_polling": True,
"qr_generated_at": datetime.now(UTC).isoformat(),
"poll_count": 0,
}
db.commit()
runner = DouyuBatchRunner(
db,
task.batch_id,
"get_bind_qr",
close_db=False,
)
calls: list[list[str]] = []
def fake_fetch(_client, aliases):
calls.append(list(aliases))
return [legacy if alias == "cjm" else activity for alias in aliases]
monkeypatch.setattr(runner, "_client", lambda _cookie: object())
monkeypatch.setattr(runner, "_fetch_bind_info_candidates", fake_fetch)
runner.poll_bind_role_once(
db,
task,
account,
{
"bind_act_alias": "20260120QYOOB",
"confirm_act_alias": "20260120QYOOB",
"legacy_act_alias": "cjm",
},
)
runner.poll_bind_role_once(
db,
task,
account,
{
"bind_act_alias": "20260120QYOOB",
"confirm_act_alias": "20260120QYOOB",
"legacy_act_alias": "cjm",
},
)
runner.poll_bind_role_once(
db,
task,
account,
{
"bind_act_alias": "20260120QYOOB",
"confirm_act_alias": "20260120QYOOB",
"legacy_act_alias": "cjm",
},
)
assert calls == [
["cjm"],
["cjm"],
["cjm"],
["20260120QYOOB"],
]
assert db.query(DouyuRoleApiLog).count() == 1
assert db.query(DouyuRoleApiLog).one().stage == "binding_poll.action_alias"
def test_binding_poll_finishes_when_scan_directly_binds_new_role(db, monkeypatch):
"""扫码后官方直接返回 bindAct=1 时,也必须结束等待而不是继续轮询。"""
_, 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",
"baseline_is_bound_act": True,
"before_bind_info": {
"act_alias": "bind-act",
"role_name": "old-role",
"is_bound_act": True,
},
"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",
"is_bound_act": True,
"is_bound_role": True,
"need_bind_act": True,
}
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": True,
"is_bound_role": True,
"is_bound_account": True,
"need_bind_act": True,
"need_bind_role": True,
}
monkeypatch.setattr(runner, "_client", lambda _cookie: type("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)
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.result["bind_confirmed"] is True
assert task.result["bind_phase"] == "confirmed"
assert task.result["role_name"] == "new-role"
assert task.result["bind_decision"]["reason"] == "directly_confirmed"
def test_deadline_poll_still_checks_role_once_before_timeout(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(seconds=1)
task.result = {
"url": "https://example.test/qr",
"query_act_aliases": ["bind-act"],
"baseline_role_name": "",
"baseline_is_bound_act": False,
"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": True,
"need_bind_act": True,
"need_bind_role": True,
}
monkeypatch.setattr(runner, "_client", lambda _cookie: type("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.result["bind_ready_for_confirm"] is True
def test_qr_preflight_accepts_existing_action_alias_pending_same_role( def test_qr_preflight_accepts_existing_action_alias_pending_same_role(
db, monkeypatch db, monkeypatch
): ):
@@ -688,6 +985,7 @@ def test_douyu_lanes_separate_interactive_query_payment_and_writes():
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 10 assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 10
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 3 assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 3
assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == 5 assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == 5
assert _DEFAULT_WORKERS[TaskLane.BINDING_POLL] == 6
def test_slim_task_result_removes_debug_payloads(): def test_slim_task_result_removes_debug_payloads():
+1 -1
View File
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
PROJECT_ROOT = TESTS_DIR.parent PROJECT_ROOT = TESTS_DIR.parent
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions" VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
HEAD_REVISION = "20261121_0109" HEAD_REVISION = "20261121_0110"
# 迁移新增、但不声明在模型里的复合查询索引。 # 迁移新增、但不声明在模型里的复合查询索引。
EXTRA_INDEXES = ( EXTRA_INDEXES = (
@@ -437,6 +437,10 @@
"20261121_0109_account_pool_tier_index.py": { "20261121_0109_account_pool_tier_index.py": {
"revision": "20261121_0109", "revision": "20261121_0109",
"sha256": "0aa85febbc0f2c99f5768bba2364f8797a748c4de740780e33a2017311f591b0" "sha256": "0aa85febbc0f2c99f5768bba2364f8797a748c4de740780e33a2017311f591b0"
},
"20261121_0110_douyu_role_api_logs.py": {
"revision": "20261121_0110",
"sha256": "4f445f867e3fc41c7713f9296fe7be700823e936ed838039347e5b26abcd48a7"
} }
} }
} }
@@ -0,0 +1,139 @@
"""add durable douyu role API response logs
Role binding has multiple aliases and state flags. Persist key raw responses so
production failures can be replayed without guessing from the final task result.
"""
from __future__ import annotations
import sqlalchemy as sa
from alembic import op
revision: str = "20261121_0110"
down_revision: str | None = "20261121_0109"
branch_labels: str | None = None
depends_on: str | None = None
def upgrade() -> None:
op.create_table(
"douyu_role_api_logs",
sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
sa.Column("account_id", sa.Integer(), nullable=False),
sa.Column("task_id", sa.Integer(), nullable=True),
sa.Column(
"scope",
sa.String(length=16),
nullable=False,
server_default="elite",
),
sa.Column(
"stage",
sa.String(length=48),
nullable=False,
server_default="",
),
sa.Column(
"source",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column(
"act_alias",
sa.String(length=64),
nullable=False,
server_default="",
),
sa.Column(
"api_version",
sa.String(length=16),
nullable=False,
server_default="",
),
sa.Column(
"role_name",
sa.String(length=128),
nullable=False,
server_default="",
),
sa.Column(
"area_name",
sa.String(length=128),
nullable=False,
server_default="",
),
sa.Column(
"plat_name",
sa.String(length=128),
nullable=False,
server_default="",
),
sa.Column(
"is_bound_act",
sa.Boolean(),
nullable=False,
server_default="0",
),
sa.Column(
"is_bound_role",
sa.Boolean(),
nullable=False,
server_default="0",
),
sa.Column(
"need_bind_act",
sa.Boolean(),
nullable=False,
server_default="0",
),
sa.Column(
"need_bind_role",
sa.Boolean(),
nullable=False,
server_default="0",
),
sa.Column("change_role_wait_time", sa.BigInteger(), nullable=True),
sa.Column("can_change_time", sa.BigInteger(), nullable=True),
sa.Column("request_json", sa.JSON(), nullable=True),
sa.Column("response_json", sa.JSON(), nullable=True),
sa.Column(
"created_at",
sa.DateTime(),
nullable=False,
server_default=sa.text("CURRENT_TIMESTAMP"),
),
sa.ForeignKeyConstraint(
["account_id"],
["accounts.id"],
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["task_id"],
["douyu_tasks.id"],
ondelete="SET NULL",
),
sa.PrimaryKeyConstraint("id"),
)
op.create_index(
"ix_douyu_role_api_logs_account_created",
"douyu_role_api_logs",
["account_id", "created_at", "id"],
unique=False,
)
op.create_index(
"ix_douyu_role_api_logs_task_created",
"douyu_role_api_logs",
["task_id", "created_at", "id"],
unique=False,
)
op.create_index(
"ix_douyu_role_api_logs_alias_created",
"douyu_role_api_logs",
["act_alias", "created_at", "id"],
unique=False,
)
def downgrade() -> None:
op.drop_table("douyu_role_api_logs")
+88
View File
@@ -584,6 +584,94 @@ class DouyuConfig(Base):
) )
class DouyuRoleApiLog(Base):
"""斗鱼角色接口原始响应与归一状态,仅追加。
角色绑定/换绑涉及多个 alias(当前活动、cjm/legacy、确认接口),只看最终
``douyu_tasks.result`` 无法还原每一步接口到底返回了什么。这里保存关键节点
的原始响应,并用角色字段做快速检索;轮询阶段只在状态变化时写入,避免相同
响应每 5 秒重复落库。
"""
__tablename__ = "douyu_role_api_logs"
id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=True)
account_id: Mapped[int] = mapped_column(
ForeignKey("accounts.id", ondelete="CASCADE"),
nullable=False,
)
task_id: Mapped[int | None] = mapped_column(
ForeignKey("douyu_tasks.id", ondelete="SET NULL"),
nullable=True,
)
scope: Mapped[str] = mapped_column(
String(16), nullable=False, default="elite", server_default="elite"
)
stage: Mapped[str] = mapped_column(
String(48), nullable=False, default="", server_default=""
)
source: Mapped[str] = mapped_column(
String(64), nullable=False, default="", server_default=""
)
act_alias: Mapped[str] = mapped_column(
String(64), nullable=False, default="", server_default=""
)
api_version: Mapped[str] = mapped_column(
String(16), nullable=False, default="", server_default=""
)
role_name: Mapped[str] = mapped_column(
String(128), nullable=False, default="", server_default=""
)
area_name: Mapped[str] = mapped_column(
String(128), nullable=False, default="", server_default=""
)
plat_name: Mapped[str] = mapped_column(
String(128), nullable=False, default="", server_default=""
)
is_bound_act: Mapped[bool] = mapped_column(
Boolean, nullable=False, default=False, server_default="0"
)
is_bound_role: Mapped[bool] = mapped_column(
Boolean, nullable=False, default=False, server_default="0"
)
need_bind_act: Mapped[bool] = mapped_column(
Boolean, nullable=False, default=False, server_default="0"
)
need_bind_role: Mapped[bool] = mapped_column(
Boolean, nullable=False, default=False, server_default="0"
)
change_role_wait_time: Mapped[int | None] = mapped_column(
BigInteger, nullable=True
)
can_change_time: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
request_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
response_json: Mapped[dict | None] = mapped_column(JSON, nullable=True)
created_at: Mapped[datetime] = mapped_column(
UTCDateTime, default=_utcnow, nullable=False
)
__table_args__ = (
Index(
"ix_douyu_role_api_logs_account_created",
"account_id",
"created_at",
"id",
),
Index(
"ix_douyu_role_api_logs_task_created",
"task_id",
"created_at",
"id",
),
Index(
"ix_douyu_role_api_logs_alias_created",
"act_alias",
"created_at",
"id",
),
)
class DouyuGoodsSnapshot(Base): class DouyuGoodsSnapshot(Base):
"""斗鱼兑换商品快照""" """斗鱼兑换商品快照"""
@@ -56,6 +56,7 @@ from .douyu_elite_status import (
normalize_elite_expire_time, normalize_elite_expire_time,
) )
from .douyu_goods_service import serialize_goods from .douyu_goods_service import serialize_goods
from .douyu_role_audit import record_role_api_candidates
SESSION_VALID = "valid" SESSION_VALID = "valid"
SESSION_INVALID = "invalid" SESSION_INVALID = "invalid"
@@ -970,10 +971,34 @@ def persist_probe_result(
pass pass
roles = probe.get("roles") or {} roles = probe.get("roles") or {}
probe_id = str(probe.get("probe_id") or "")
task_id: int | None = None
if probe_id.startswith("task:"):
try:
task_id = int(probe_id.removeprefix("task:"))
except ValueError:
task_id = None
for scope in (SCOPE_ELITE, SCOPE_ESPORTS, SCOPE_PEACE): for scope in (SCOPE_ELITE, SCOPE_ESPORTS, SCOPE_PEACE):
role = roles.get(scope) role = roles.get(scope)
if isinstance(role, dict): if isinstance(role, dict):
_apply_role_result(account, scope, role) _apply_role_result(account, scope, role)
raw_candidates = [
item
for item in (role.get("bind_infos") or [])
if isinstance(item, dict)
]
bind_info = role.get("bind_info")
if not raw_candidates and isinstance(bind_info, dict):
raw_candidates = [bind_info]
record_role_api_candidates(
db,
account_id=account.id,
task_id=task_id,
candidates=raw_candidates,
scope=scope,
stage=f"probe.{scope}",
source=source,
)
gold = probe.get("gold") or {} gold = probe.get("gold") or {}
if gold.get("status") == CAPABILITY_OK and _to_int(gold.get("balance")) is not None: if gold.get("status") == CAPABILITY_OK and _to_int(gold.get("balance")) is not None:
+140
View File
@@ -0,0 +1,140 @@
"""Persist key Douyu role API responses for production diagnosis."""
from __future__ import annotations
from collections.abc import Mapping, Sequence
from datetime import UTC, datetime
from typing import Any
from sqlalchemy.orm import Session
from ..models import DouyuRoleApiLog
from .account_binding import binding_snapshot
_SENSITIVE_KEY_MARKERS = (
"cookie",
"authorization",
"csrf",
"acf_ccn",
"token",
"password",
)
def _redact(value: Any, *, depth: int = 0) -> Any:
if depth > 12:
return "<max-depth>"
if isinstance(value, Mapping):
cleaned: dict[str, Any] = {}
for raw_key, raw_value in value.items():
key = str(raw_key)
if any(marker in key.casefold() for marker in _SENSITIVE_KEY_MARKERS):
cleaned[key] = "<redacted>"
else:
cleaned[key] = _redact(raw_value, depth=depth + 1)
return cleaned
if isinstance(value, (list, tuple, set)):
return [_redact(item, depth=depth + 1) for item in value]
if isinstance(value, datetime):
moment = value if value.tzinfo else value.replace(tzinfo=UTC)
return moment.isoformat()
if value is None or isinstance(value, (str, int, float, bool)):
return value
return str(value)
def record_role_api_candidates(
db: Session,
*,
account_id: int | None,
candidates: Sequence[Mapping[str, Any]],
scope: str,
stage: str,
source: str,
task_id: int | None = None,
) -> int:
"""Append successful role query responses; callers decide when they matter."""
if account_id is None:
return 0
rows: list[DouyuRoleApiLog] = []
for candidate in candidates:
info = dict(candidate)
snapshot = binding_snapshot(info)
act_alias = str(info.get("act_alias") or "")
rows.append(
DouyuRoleApiLog(
account_id=int(account_id),
task_id=int(task_id) if task_id is not None else None,
scope=str(scope or "elite")[:16],
stage=str(stage or "")[:48],
source=str(source or "")[:64],
act_alias=act_alias[:64],
api_version=str(info.get("api_version") or "")[:16],
role_name=str(snapshot["role_name"] or "")[:128],
area_name=str(snapshot["area_name"] or "")[:128],
plat_name=str(snapshot["plat_name"] or "")[:128],
is_bound_act=bool(snapshot["is_bound_act"]),
is_bound_role=bool(snapshot["is_bound_role"]),
need_bind_act=bool(snapshot["need_bind_act"]),
need_bind_role=bool(snapshot["need_bind_role"]),
change_role_wait_time=_to_int(snapshot["change_role_wait_time"]),
can_change_time=_to_int(snapshot["can_change_time"]),
request_json=_redact(
{
"scope": scope,
"stage": stage,
"act_alias": act_alias,
"api_version": info.get("api_version"),
}
),
response_json=_redact(info.get("raw") or info),
)
)
if not rows:
return 0
db.add_all(rows)
return len(rows)
def record_role_api_response(
db: Session,
*,
account_id: int | None,
response: Mapping[str, Any] | None,
scope: str,
stage: str,
source: str,
act_alias: str = "",
task_id: int | None = None,
) -> int:
"""Append a write/confirm response that has no normalized role candidate."""
if account_id is None:
return 0
db.add(
DouyuRoleApiLog(
account_id=int(account_id),
task_id=int(task_id) if task_id is not None else None,
scope=str(scope or "elite")[:16],
stage=str(stage or "")[:48],
source=str(source or "")[:64],
act_alias=str(act_alias or "")[:64],
request_json=_redact({"scope": scope, "act_alias": act_alias}),
response_json=_redact(dict(response or {})),
)
)
return 1
def _to_int(value: Any) -> int | None:
if value is None or value == "":
return None
try:
return int(float(value))
except (TypeError, ValueError):
return None
__all__ = [
"record_role_api_candidates",
"record_role_api_response",
]
+334 -13
View File
@@ -3,6 +3,7 @@
from __future__ import annotations from __future__ import annotations
import json import json
import os
import time import time
from datetime import UTC, datetime, timedelta from datetime import UTC, datetime, timedelta
from typing import TYPE_CHECKING, Any from typing import TYPE_CHECKING, Any
@@ -37,14 +38,46 @@ from .douyu_account_probe import (
resolve_elite_role, resolve_elite_role,
) )
from .douyu_activity_query import query_esports_bind_info from .douyu_activity_query import query_esports_bind_info
from .douyu_role_audit import (
record_role_api_candidates,
record_role_api_response,
)
DOUYU_LEGACY_BIND_ACT_ALIAS = "20250213NQCYX" DOUYU_LEGACY_BIND_ACT_ALIAS = "20250213NQCYX"
DOUYU_BIND_ROLE_POLL_SECONDS = 600
def _positive_env_int(name: str, default: int) -> int:
try:
value = int(os.getenv(name, str(default)))
except ValueError:
return default
return value if value > 0 else default
# 繁忙时段成功案例也可能轮询 60+ 次才出现角色,10 分钟过短。
DOUYU_BIND_ROLE_POLL_SECONDS = _positive_env_int(
"DOUYU_BIND_ROLE_POLL_SECONDS",
20 * 60,
)
DOUYU_BIND_ROLE_POLL_INTERVAL = 5 DOUYU_BIND_ROLE_POLL_INTERVAL = 5
DOUYU_BIND_ROLE_POLL_FAST_SECONDS = 2 * 60
DOUYU_BIND_ROLE_POLL_MEDIUM_SECONDS = 10 * 60
DOUYU_BIND_ROLE_POLL_SLOW_INTERVAL = 20
DOUYU_BIND_ACTION_ALIAS_EVERY_POLLS = 3
DOUYU_CONFIRM_EFFECT_POLL_TIMES = 3 DOUYU_CONFIRM_EFFECT_POLL_TIMES = 3
DOUYU_CONFIRM_EFFECT_POLL_INTERVAL = 3 DOUYU_CONFIRM_EFFECT_POLL_INTERVAL = 3
def bind_poll_interval_seconds(elapsed_seconds: float) -> int:
"""Return the scan-wait polling cadence for a task's elapsed lifetime."""
elapsed = max(0.0, float(elapsed_seconds or 0))
if elapsed < DOUYU_BIND_ROLE_POLL_FAST_SECONDS:
return DOUYU_BIND_ROLE_POLL_INTERVAL
if elapsed < DOUYU_BIND_ROLE_POLL_MEDIUM_SECONDS:
return 10
return DOUYU_BIND_ROLE_POLL_SLOW_INTERVAL
class BindMixin: class BindMixin:
"""游戏角色绑定域:绑定状态机、扫码/确认绑定、换绑冷却。""" """游戏角色绑定域:绑定状态机、扫码/确认绑定、换绑冷却。"""
@@ -157,6 +190,113 @@ class BindMixin:
actionable_aliases=actionable_aliases, actionable_aliases=actionable_aliases,
) )
@classmethod
def _is_newly_confirmed_binding(
cls,
snapshot: dict,
*,
baseline_role_name: str,
baseline_is_bound_act: bool,
) -> bool:
"""扫码完成后官方可能直接返回 bindAct=1,而不是 pending。"""
role_name = str(snapshot.get("role_name") or "").strip()
if not snapshot.get("is_bound_act") or not role_name:
return False
baseline_role = baseline_role_name.strip()
if not baseline_is_bound_act or not baseline_role:
return True
return role_name.casefold() != baseline_role.casefold()
@classmethod
def _find_newly_confirmed_binding(
cls,
candidates: list[dict],
*,
baseline_role_name: str,
baseline_is_bound_act: bool,
) -> dict | None:
"""Find a scan result that the official API already confirmed."""
for candidate in candidates:
if cls._is_newly_confirmed_binding(
binding_snapshot(candidate),
baseline_role_name=baseline_role_name,
baseline_is_bound_act=baseline_is_bound_act,
):
return candidate
return None
@staticmethod
def _bind_poll_snapshot(info: dict | None) -> dict[str, Any] | None:
"""Small, JSON-safe snapshot used only to detect polling changes."""
if not isinstance(info, dict):
return None
snapshot = binding_snapshot(info)
return {
"role_name": snapshot["role_name"],
"area_name": snapshot["area_name"],
"plat_name": snapshot["plat_name"],
"is_bound_act": bool(snapshot["is_bound_act"]),
"is_bound_role": bool(snapshot["is_bound_role"]),
"need_bind_act": bool(snapshot["need_bind_act"]),
"need_bind_role": bool(snapshot["need_bind_role"]),
}
@classmethod
def _bind_poll_snapshot_changed(
cls,
previous: dict | None,
current: dict | None,
) -> bool:
current_snapshot = cls._bind_poll_snapshot(current)
if current_snapshot is None:
return False
previous_snapshot = cls._bind_poll_snapshot(previous)
if previous_snapshot is not None:
return previous_snapshot != current_snapshot
return bool(
current_snapshot["role_name"]
or current_snapshot["is_bound_act"]
or current_snapshot["is_bound_role"]
or current_snapshot["need_bind_act"]
or current_snapshot["need_bind_role"]
)
@staticmethod
def _split_bind_poll_aliases(
query_aliases: list[str],
config: dict,
) -> tuple[list[str], list[str]]:
"""Prefer the cheap legacy probe; activity aliases are verified on change."""
legacy_alias = str(config.get("legacy_act_alias") or "").strip()
if legacy_alias and legacy_alias in query_aliases:
return (
[legacy_alias],
[alias for alias in query_aliases if alias != legacy_alias],
)
return query_aliases, []
@staticmethod
def _bind_poll_elapsed_seconds(
result: dict,
*,
now: datetime,
deadline: datetime | None,
) -> float:
raw_generated_at = str(result.get("qr_generated_at") or "").strip()
generated_at: datetime | None = None
if raw_generated_at:
try:
generated_at = datetime.fromisoformat(raw_generated_at)
except ValueError:
generated_at = None
if generated_at is not None and generated_at.tzinfo is None:
generated_at = generated_at.replace(tzinfo=UTC)
if generated_at is None and deadline is not None:
generated_at = deadline - timedelta(seconds=DOUYU_BIND_ROLE_POLL_SECONDS)
if generated_at is None:
return 0.0
return max(0.0, (now - generated_at).total_seconds())
def _bind_snapshot(self, bind_info: dict | None) -> dict: def _bind_snapshot(self, bind_info: dict | None) -> dict:
return binding_snapshot(bind_info) return binding_snapshot(bind_info)
@@ -732,7 +872,6 @@ class BindMixin:
task.queue_lane = "binding_poll" task.queue_lane = "binding_poll"
task.next_run_at = now + timedelta(seconds=max(1, interval_seconds)) task.next_run_at = now + timedelta(seconds=max(1, interval_seconds))
task.last_run_at = now task.last_run_at = now
task.attempt_count = int(task.attempt_count or 0) + 1
def poll_bind_role_once( def poll_bind_role_once(
self, self,
@@ -754,7 +893,19 @@ class BindMixin:
if not query_aliases: if not query_aliases:
query_aliases = self._query_bind_act_aliases(config) query_aliases = self._query_bind_act_aliases(config)
deadline = task.run_deadline_at deadline = task.run_deadline_at
if deadline is not None and now >= deadline: poll_number = int(result.get("poll_count") or 0) + 1
interval_seconds = bind_poll_interval_seconds(
self._bind_poll_elapsed_seconds(result, now=now, deadline=deadline)
)
last_poll = bool(
deadline is not None
and (
now >= deadline
or now + timedelta(seconds=interval_seconds) >= deadline
)
)
def finish_timeout() -> None:
result.update( result.update(
{ {
"bind_polling": False, "bind_polling": False,
@@ -771,17 +922,78 @@ class BindMixin:
"绑定二维码已过期,未识别到新扫码角色", "绑定二维码已过期,未识别到新扫码角色",
result, result,
) )
return
client = self._client(str(account.cookie or "")) client = self._client(str(account.cookie or ""))
candidates = self._fetch_bind_info_candidates(client, query_aliases) primary_aliases, action_aliases_to_verify = self._split_bind_poll_aliases(
query_aliases,
config,
)
candidates = self._fetch_bind_info_candidates(client, primary_aliases)
primary_info = candidates[0] if candidates else None
legacy_changed = self._bind_poll_snapshot_changed(
result.get("bind_poll_legacy_snapshot"),
primary_info,
)
verify_action_alias = bool(
action_aliases_to_verify
and (
legacy_changed
or last_poll
or poll_number % DOUYU_BIND_ACTION_ALIAS_EVERY_POLLS == 0
)
)
action_candidates: list[dict] = []
if verify_action_alias:
action_candidates = self._fetch_bind_info_candidates(
client,
action_aliases_to_verify,
)
candidates.extend(action_candidates)
if primary_info is not None and legacy_changed:
record_role_api_candidates(
db,
account_id=account.id,
task_id=task.id,
candidates=[primary_info],
scope=SCOPE_ELITE,
stage="binding_poll.legacy_changed",
source="douyu_task.get_bind_qr",
)
action_snapshot = [
self._bind_poll_snapshot(item) for item in action_candidates
]
if action_candidates and action_snapshot != result.get(
"bind_poll_action_audit"
):
record_role_api_candidates(
db,
account_id=account.id,
task_id=task.id,
candidates=action_candidates,
scope=SCOPE_ELITE,
stage="binding_poll.action_alias",
source="douyu_task.get_bind_qr",
)
result["bind_poll_action_audit"] = action_snapshot
result["bind_poll_round"] = poll_number
result["bind_poll_query_aliases"] = [
*primary_aliases,
*(action_aliases_to_verify if verify_action_alias else []),
]
if primary_info is not None:
result["bind_poll_legacy_snapshot"] = self._bind_poll_snapshot(primary_info)
if not candidates: if not candidates:
attempts = max(1, int(task.attempt_count or 1)) if deadline is not None and datetime.now(UTC) >= deadline:
backoff = min(30, max(DOUYU_BIND_ROLE_POLL_INTERVAL, attempts * 2)) finish_timeout()
return
result["bind_poll_error"] = "所有 actAlias 查询绑定信息失败" result["bind_poll_error"] = "所有 actAlias 查询绑定信息失败"
result["bind_polling"] = True result["bind_polling"] = True
result["bind_phase"] = "waiting_scan" result["bind_phase"] = "waiting_scan"
self._schedule_next_bind_poll(task, now=now, interval_seconds=backoff) self._schedule_next_bind_poll(
task,
now=now,
interval_seconds=interval_seconds,
)
self._update_task_progress( self._update_task_progress(
db, db,
task, task,
@@ -833,7 +1045,7 @@ class BindMixin:
) )
self._push_binding_decision_log( self._push_binding_decision_log(
task=task, task=task,
stage=f"poll:{int(result.get('poll_count') or 0) + 1}", stage=f"poll:{poll_number}",
candidates=candidates, candidates=candidates,
decisions=candidate_decisions, decisions=candidate_decisions,
selected=bind_info, selected=bind_info,
@@ -841,6 +1053,63 @@ class BindMixin:
actionable_aliases=action_aliases, actionable_aliases=action_aliases,
) )
directly_confirmed = self._find_newly_confirmed_binding(
candidates,
baseline_role_name=baseline_role_name,
baseline_is_bound_act=baseline_is_bound_act,
)
if directly_confirmed is not None:
bind_info = directly_confirmed
snapshot = self._bind_snapshot(bind_info)
query_alias = str(bind_info.get("act_alias") or "")
selected_decision = binding_pending_decision(
bind_info,
baseline_role_name=baseline_role_name,
baseline_is_bound_act=baseline_is_bound_act,
baseline_info=baseline_info,
actionable_aliases=action_aliases,
)
selected_decision = {
**selected_decision,
"confirmable": False,
"reason": "directly_confirmed",
}
summary = (
f"act={query_alias or '-'} "
f"{self._format_bind_summary(bind_info, pending=False)}"
)
task.queue_lane = ""
task.next_run_at = None
result.update(
{
**snapshot,
"act_alias": query_alias,
"query_act_alias": query_alias,
"role_name": snapshot["role_name"],
"current_role_name": snapshot["role_name"],
"current_area_name": snapshot["area_name"],
"current_plat_name": snapshot["plat_name"],
"bind_ready_for_confirm": False,
"bind_confirmed": True,
"bind_phase": "confirmed",
"bind_polling": False,
"poll_count": poll_number,
"bind_summary": summary,
"action_act_aliases": action_aliases,
"bind_decision": selected_decision,
"bind_candidate_decisions": candidate_decisions,
}
)
self._apply_bind_info_to_account(account, bind_info, BIND_BOUND)
self._mark_task(
db,
task,
"success",
f"绑定成功: {snapshot['role_name']}",
result,
)
return
if is_pending_role: if is_pending_role:
task.queue_lane = "" task.queue_lane = ""
task.next_run_at = None task.next_run_at = None
@@ -853,8 +1122,8 @@ class BindMixin:
"bind_confirmed": False, "bind_confirmed": False,
"bind_phase": "role_ready", "bind_phase": "role_ready",
"bind_polling": False, "bind_polling": False,
"pending_role_name": snapshot["role_name"], "pending_role_name": snapshot["role_name"],
"poll_count": int(result.get("poll_count") or 0) + 1, "poll_count": poll_number,
"bind_summary": summary, "bind_summary": summary,
"action_act_aliases": action_aliases, "action_act_aliases": action_aliases,
"bind_decision": selected_decision, "bind_decision": selected_decision,
@@ -894,17 +1163,20 @@ class BindMixin:
"bind_confirmed": False, "bind_confirmed": False,
"bind_phase": "waiting_scan", "bind_phase": "waiting_scan",
"bind_polling": True, "bind_polling": True,
"poll_count": int(result.get("poll_count") or 0) + 1, "poll_count": poll_number,
"bind_summary": summary, "bind_summary": summary,
"action_act_aliases": action_aliases, "action_act_aliases": action_aliases,
"bind_decision": selected_decision, "bind_decision": selected_decision,
"bind_candidate_decisions": candidate_decisions, "bind_candidate_decisions": candidate_decisions,
} }
) )
if deadline is not None and datetime.now(UTC) >= deadline:
finish_timeout()
return
self._schedule_next_bind_poll( self._schedule_next_bind_poll(
task, task,
now=now, now=now,
interval_seconds=DOUYU_BIND_ROLE_POLL_INTERVAL, interval_seconds=interval_seconds,
) )
self._update_task_progress( self._update_task_progress(
db, db,
@@ -934,6 +1206,15 @@ class BindMixin:
db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias" db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias"
) )
return return
record_role_api_candidates(
db,
account_id=account.id,
task_id=task.id,
candidates=before_candidates,
scope=SCOPE_ELITE,
stage="bind_qr.preflight",
source="douyu_task.get_bind_qr",
)
# baseline 用活动 alias 的“当前已绑定”;pending 检测优先 cjm 的换绑最新态 # baseline 用活动 alias 的“当前已绑定”;pending 检测优先 cjm 的换绑最新态
before = self._pick_baseline_bind_info(before_candidates, config) or {} before = self._pick_baseline_bind_info(before_candidates, config) or {}
# 换绑冷却必须看活动当前绑定(QYOOB),不要用 cjm # 换绑冷却必须看活动当前绑定(QYOOB),不要用 cjm
@@ -1078,6 +1359,17 @@ class BindMixin:
"bind_decision": pending_decision, "bind_decision": pending_decision,
"bind_candidate_decisions": candidate_decisions, "bind_candidate_decisions": candidate_decisions,
"poll_count": 0, "poll_count": 0,
"bind_poll_legacy_snapshot": self._bind_poll_snapshot(
next(
(
item
for item in before_candidates
if str(item.get("act_alias") or "")
== str(config.get("legacy_act_alias") or "")
),
None,
)
),
} }
if pending_decision["confirmable"]: if pending_decision["confirmable"]:
pending_snapshot = self._bind_snapshot(pending_before) pending_snapshot = self._bind_snapshot(pending_before)
@@ -1153,6 +1445,15 @@ class BindMixin:
db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias" db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias"
) )
return return
record_role_api_candidates(
db,
account_id=account.id,
task_id=task.id,
candidates=before_candidates,
scope=SCOPE_ELITE,
stage="confirm_bind.precheck",
source="douyu_task.confirm_bind",
)
# 确认前“已生效绑定”角色(bound_act=1),确认失败/回查失败时写库用,避免待确认角色污染 game_name # 确认前“已生效绑定”角色(bound_act=1),确认失败/回查失败时写库用,避免待确认角色污染 game_name
before_bound = self._pick_current_bound_info( before_bound = self._pick_current_bound_info(
before_candidates, before_candidates,
@@ -1300,6 +1601,16 @@ class BindMixin:
) )
return return
confirm_raw = confirm_result.get("raw") or {} confirm_raw = confirm_result.get("raw") or {}
record_role_api_response(
db,
account_id=account.id,
task_id=task.id,
response=confirm_raw if isinstance(confirm_raw, dict) else {},
scope=SCOPE_ELITE,
stage="confirm_bind.response",
source="douyu_task.confirm_bind",
act_alias=use_confirm_alias,
)
self._push_log( self._push_log(
"info", "info",
f"确认绑定接口返回 act={use_confirm_alias} " f"确认绑定接口返回 act={use_confirm_alias} "
@@ -1332,6 +1643,16 @@ class BindMixin:
after = _pick_bound_after(after_candidates) after = _pick_bound_after(after_candidates)
if after is not None: if after is not None:
break break
if after_candidates:
record_role_api_candidates(
db,
account_id=account.id,
task_id=task.id,
candidates=after_candidates,
scope=SCOPE_ELITE,
stage="confirm_bind.after",
source="douyu_task.confirm_bind",
)
if after is None: if after is None:
# 已生效绑定始终未出现:确认未生效(或同步延迟超时),保留原绑定 # 已生效绑定始终未出现:确认未生效(或同步延迟超时),保留原绑定
if before_bound is not None: if before_bound is not None:
+5 -2
View File
@@ -4,7 +4,7 @@ from __future__ import annotations
import os import os
import uuid import uuid
from datetime import UTC, datetime from datetime import UTC, datetime, timedelta
from loguru import logger from loguru import logger
from sqlalchemy import and_, func, or_, select from sqlalchemy import and_, func, or_, select
@@ -56,6 +56,8 @@ SUPPORTED_DOUYU_TASK_TYPES = {
"exchange_xpd_goods": "兑换小店商品", "exchange_xpd_goods": "兑换小店商品",
} }
BINDING_POLL_CLEANUP_GRACE_SECONDS = 120
DOUYU_HANDBOOK_SCOPES = {"elite", "esports", "peace"} DOUYU_HANDBOOK_SCOPES = {"elite", "esports", "peace"}
DOUYU_HANDBOOK_TASK_TYPES = { DOUYU_HANDBOOK_TASK_TYPES = {
"elite": { "elite": {
@@ -476,7 +478,8 @@ def cleanup_orphan_douyu_tasks(
DouyuTask.queue_lane == "binding_poll", DouyuTask.queue_lane == "binding_poll",
DouyuTask.next_run_at.isnot(None), DouyuTask.next_run_at.isnot(None),
DouyuTask.run_deadline_at.isnot(None), DouyuTask.run_deadline_at.isnot(None),
DouyuTask.run_deadline_at > now, DouyuTask.run_deadline_at
> now - timedelta(seconds=BINDING_POLL_CLEANUP_GRACE_SECONDS),
) )
durable_payment = and_( durable_payment = and_(
DouyuTask.task_type.in_( DouyuTask.task_type.in_(
@@ -82,7 +82,9 @@ ACTIVE_STATUSES = {
"manual_required", "manual_required",
} }
BINDING_QR_TTL_SECONDS = int(os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "600")) BINDING_QR_TTL_SECONDS = int(
os.getenv("PLATFORM_BINDING_QR_TTL_SECONDS", "1260")
)
AUTO_SYNC_INTERVAL_SECONDS = 900.0 AUTO_SYNC_INTERVAL_SECONDS = 900.0
DEFAULT_LIVE_CANDIDATE_LIMIT = 20 DEFAULT_LIVE_CANDIDATE_LIMIT = 20
+1 -1
View File
@@ -53,7 +53,7 @@ class LaneConfig:
_DEFAULT_WORKERS: dict[TaskLane, int] = { _DEFAULT_WORKERS: dict[TaskLane, int] = {
TaskLane.INTERACTIVE_BINDING: 3, TaskLane.INTERACTIVE_BINDING: 3,
TaskLane.BATCH_COORDINATOR: 2, TaskLane.BATCH_COORDINATOR: 2,
TaskLane.BINDING_POLL: 2, TaskLane.BINDING_POLL: 6,
TaskLane.LOGIN: 4, TaskLane.LOGIN: 4,
TaskLane.ACCOUNT_CHECK: 4, TaskLane.ACCOUNT_CHECK: 4,
TaskLane.AUTOMATION: 2, TaskLane.AUTOMATION: 2,
@@ -1519,7 +1519,12 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
: (latestQueryGameTaskByAccount.get(qrTask.account_id) || null)) : (latestQueryGameTaskByAccount.get(qrTask.account_id) || null))
: null; : null;
const qrQueryResult = qrTask && qrQueryTask && qrQueryTask.id > qrTask.id ? qrQueryTask.result : null; const qrQueryResult = qrTask && qrQueryTask && qrQueryTask.id > qrTask.id ? qrQueryTask.result : null;
const qrQueryRunning = Boolean(qrQueryTask && qrTask && qrQueryTask.id > qrTask.id && qrQueryTask.status === 'running'); const qrQueryRunning = Boolean(
qrQueryTask
&& qrTask
&& qrQueryTask.id > qrTask.id
&& ['planned', 'pending', 'running'].includes(qrQueryTask.status),
);
const qrXpdConfirmTask = isXpdBindTask && qrTask const qrXpdConfirmTask = isXpdBindTask && qrTask
? (latestConfirmXpdBindTaskByAccount.get(qrTask.account_id) || null) ? (latestConfirmXpdBindTaskByAccount.get(qrTask.account_id) || null)
: null; : null;
@@ -1616,11 +1621,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
}; };
const queryQrRole = () => { const queryQrRole = () => {
if (!qrTask) return; if (!qrTask || qrQueryRunning) return;
if (qrAutoPolling) {
message.info('正在自动识别角色,请稍候');
return;
}
void startTask(isXpdBindTask ? 'query_xpd_bind_info' : 'query_game_name', [qrTask.account_id]); void startTask(isXpdBindTask ? 'query_xpd_bind_info' : 'query_game_name', [qrTask.account_id]);
}; };
@@ -3172,9 +3173,9 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
icon={<SearchOutlined />} icon={<SearchOutlined />}
onClick={queryQrRole} onClick={queryQrRole}
loading={qrQueryRunning} loading={qrQueryRunning}
disabled={qrAutoPolling} disabled={qrQueryRunning}
> >
{qrAutoPolling ? '自动识别中' : '查询角色'} {qrQueryRunning ? '查询中' : '立即查询角色'}
</Button> </Button>
<Button <Button
type="primary" type="primary"