merge: 同步 main 同名角色绑定修复
This commit is contained in:
+4
-3
@@ -95,8 +95,8 @@ DOUYU_MAX_ACTIVE_OVERVIEW_TASKS=5
|
||||
TASK_LANE_BATCH_COORDINATOR_WORKERS=2
|
||||
TASK_LANE_BATCH_COORDINATOR_MAX_IN_FLIGHT=2
|
||||
TASK_LANE_INTERACTIVE_BINDING_WORKERS=3
|
||||
TASK_LANE_BINDING_POLL_WORKERS=2
|
||||
TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=2
|
||||
TASK_LANE_BINDING_POLL_WORKERS=6
|
||||
TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=6
|
||||
TASK_LANE_LOGIN_WORKERS=4
|
||||
TASK_LANE_ACCOUNT_CHECK_WORKERS=4
|
||||
TASK_LANE_ACCOUNT_SYNC_WORKERS=3
|
||||
@@ -209,7 +209,8 @@ PLATFORM_FULFILLMENT_MATCH_MODE=hybrid
|
||||
# 订单 Webhook 连续唤醒时保持最小选号间隔,避免忙轮询压满数据库。
|
||||
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=false
|
||||
|
||||
+4
-3
@@ -55,8 +55,8 @@ services:
|
||||
- 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_INTERACTIVE_BINDING_WORKERS=${TASK_LANE_INTERACTIVE_BINDING_WORKERS:-3}
|
||||
- TASK_LANE_BINDING_POLL_WORKERS=${TASK_LANE_BINDING_POLL_WORKERS:-2}
|
||||
- TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT=${TASK_LANE_BINDING_POLL_MAX_IN_FLIGHT:-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:-6}
|
||||
- TASK_LANE_LOGIN_WORKERS=${TASK_LANE_LOGIN_WORKERS:-4}
|
||||
- TASK_LANE_ACCOUNT_CHECK_WORKERS=${TASK_LANE_ACCOUNT_CHECK_WORKERS:-4}
|
||||
- TASK_LANE_ACCOUNT_SYNC_WORKERS=${TASK_LANE_ACCOUNT_SYNC_WORKERS:-3}
|
||||
@@ -121,7 +121,8 @@ services:
|
||||
- ACCOUNT_COMMODITY_STATE_TTL_SECONDS=${ACCOUNT_COMMODITY_STATE_TTL_SECONDS:-900}
|
||||
- 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_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)。
|
||||
# OSS_MODE=oss 才走对象存储,否则落容器内 ./data/file_storage。
|
||||
- OSS_MODE=${OSS_MODE:-local}
|
||||
|
||||
@@ -91,14 +91,18 @@
|
||||
`existing_actionable_pending`;
|
||||
3. 没有扫码前 baseline:可确认,原因 `first_role_without_baseline`;
|
||||
4. 角色名与 baseline 不同:可确认,原因 `role_changed`;
|
||||
5. 角色名相同且 `bindAct=1`:已经绑定,不可重复确认;
|
||||
6. 同 alias 从 `bindAct=1` 转为 `bindAct=0 + needBindAct=1`:可确认,原因
|
||||
`same_alias_after_bound_transition`;
|
||||
7. 其他同名角色:视为旧残留,原因 `same_role_stale`。
|
||||
5. 角色名相同,但同 alias 仍返回 `needBindAct=1`:可确认,即使当前
|
||||
`bindAct=1`,原因 `same_alias_after_bound_transition`;
|
||||
6. 动作 alias 上 `bindAct=1 + needBindAct=1 + 同名角色`:可确认;
|
||||
7. 角色名相同且接口不再要求绑定:视为已经绑定,原因 `already_bound`;
|
||||
8. 其他同名角色:视为旧残留,原因 `same_role_stale`。
|
||||
|
||||
规则 2 是 2026-11-21 线上修复:动作 alias 已经存在同名 pending 时,它仍是可执行
|
||||
确认的 pending,不应因为“角色名没变化”一直灰掉确认按钮。
|
||||
|
||||
规则 5、6 解决“重新绑定同名角色”:业务不要求每次都必须选择一个不同的角色。
|
||||
只要官方动作 alias 仍要求绑定,同名角色也必须能进入确认流程。
|
||||
|
||||
### 完整流程
|
||||
|
||||
```text
|
||||
@@ -107,7 +111,9 @@
|
||||
-> 找动作 alias 上的可确认 pending
|
||||
-> 生成二维码
|
||||
-> 持久化 baseline、候选来源、判定原因
|
||||
-> binding_poll 每次只查一次角色
|
||||
-> binding_poll 每轮先查 legacy(cjm)
|
||||
-> 角色/状态变化、每 3 轮或最后一轮再补查动作 alias(QYOOB)
|
||||
-> 官方直接返回 bindAct=1 时立即结束为 confirmed
|
||||
-> 找到确认条件:任务 success + bind_ready_for_confirm=true
|
||||
-> actBind
|
||||
-> 回查 bindAct=1
|
||||
@@ -115,7 +121,20 @@
|
||||
```
|
||||
|
||||
二维码任务不能在 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 轮巡检和最后一轮补查。这样既能
|
||||
承接十几个客服并发扫码,也不会把几十个二维码放大成持续的双倍官方请求。
|
||||
|
||||
### 确认前保护
|
||||
|
||||
@@ -155,6 +174,20 @@
|
||||
- `pending_role_name`
|
||||
- `bind_summary`
|
||||
|
||||
### 角色接口原始响应留档
|
||||
|
||||
关键角色接口的原始响应写入 `douyu_role_api_logs`,用于还原不同 alias 在
|
||||
扫码、待确认、确认和回查各阶段到底返回了什么。记录内容:
|
||||
|
||||
- 账号、任务、作用域、阶段和 alias;
|
||||
- `role_name`、`bindAct`、`bindRole`、`needBindAct`、`needBindRole`、
|
||||
换绑倒计时和可换绑时间;
|
||||
- 请求摘要与原始响应 JSON;
|
||||
- 入库前的 Cookie、token、CSRF 等敏感键脱敏结果。
|
||||
|
||||
轮询不会把相同响应每 5 秒重复入库,只在 legacy 状态变化、动作 alias 响应变化、
|
||||
二维码预检、确认前、确认接口返回和确认后回查时写入。
|
||||
|
||||
建议的生产只读查询:
|
||||
|
||||
```sql
|
||||
@@ -168,6 +201,18 @@ ORDER BY id DESC
|
||||
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
|
||||
@@ -240,11 +285,32 @@ docker compose logs douyu-login | grep '确认前状态'
|
||||
回归:`tests/test_douyu_binding_state.py`、`tests/test_douyu_account_probe.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`。
|
||||
|
||||
### 2026-11-21:同名角色被误判为已经绑定
|
||||
|
||||
生产接口留档显示,活动 alias 可以同时返回 `bindAct=1`、`needBindAct=1` 和与上次
|
||||
相同的角色名。旧判定先看 `bindAct=1` 并直接返回 `already_bound`,导致用户重新
|
||||
绑定同名角色时任务一直等待。
|
||||
|
||||
修复:只要动作 alias 仍返回 `needBindAct=1`,同名角色也进入可确认状态;候选选择
|
||||
仍先选真正变化的新角色,再选择同名可确认角色,避免活动 alias 的旧快照压掉
|
||||
`cjm` 上的新角色。
|
||||
|
||||
### 历史固定规则
|
||||
|
||||
- 不要把 `bindRole=1` 当成已绑定;
|
||||
- 不要把未确认时的换绑倒计时当成冷却;
|
||||
- 不要用 `cjm` 的旧角色覆盖活动 alias pending;
|
||||
- 不要要求每次绑定都必须换一个不同的角色,同名重绑是正常流程;
|
||||
- 不要在页面层重新实现 `bindAct/needBindAct` 判断;
|
||||
- 不要把原始 `bind_summary` 当业务文案,它只用于排障。
|
||||
|
||||
@@ -254,3 +320,4 @@ docker compose logs douyu-login | grep '确认前状态'
|
||||
| --- | --- |
|
||||
| 2026-11-21 | 建立斗鱼协议总览;补充多 alias 职责、pending 真值表、结构化判定日志和同名 pending 修复记录 |
|
||||
| 2026-11-21 | 补充 `storehome` 单账号全量商品资格、`storedetail`/`batchExchangeLimit` 边界和抓包证据入口 |
|
||||
| 2026-11-21 | 补充同名重绑、轮询退避、直接绑定识别和角色接口原始响应留档 |
|
||||
|
||||
@@ -238,7 +238,34 @@ def test_same_role_after_activity_bind_transition_is_pending():
|
||||
)
|
||||
|
||||
|
||||
def test_pending_candidate_picker_ignores_legacy_same_role_residual():
|
||||
def test_same_bound_role_with_need_bind_act_is_confirmable():
|
||||
role = "少和我较劲"
|
||||
baseline = _role_candidate(
|
||||
ACT_ALIAS,
|
||||
role,
|
||||
bound_act=True,
|
||||
need_bind_act=True,
|
||||
)
|
||||
candidate = _role_candidate(
|
||||
ACT_ALIAS,
|
||||
role,
|
||||
bound_act=True,
|
||||
need_bind_act=True,
|
||||
)
|
||||
|
||||
decision = binding_pending_decision(
|
||||
candidate,
|
||||
baseline_role_name=role,
|
||||
baseline_is_bound_act=True,
|
||||
baseline_info=baseline,
|
||||
actionable_aliases=[ACT_ALIAS],
|
||||
)
|
||||
|
||||
assert decision["confirmable"] is True
|
||||
assert decision["reason"] == "same_alias_after_bound_transition"
|
||||
|
||||
|
||||
def test_pending_candidate_picker_uses_action_alias_same_role():
|
||||
old_role = "少和我较劲"
|
||||
baseline = _role_candidate(ACT_ALIAS, old_role, bound_act=True)
|
||||
legacy = _role_candidate(LEGACY_ALIAS, old_role, bound_act=False)
|
||||
@@ -252,7 +279,8 @@ def test_pending_candidate_picker_ignores_legacy_same_role_residual():
|
||||
baseline_info=baseline,
|
||||
)
|
||||
|
||||
assert selected is None
|
||||
assert selected is not None
|
||||
assert selected["act_alias"] == ACT_ALIAS
|
||||
|
||||
|
||||
def test_task_result_helpers_use_shared_same_role_transition_rule():
|
||||
|
||||
@@ -9,7 +9,7 @@ from sqlalchemy import text
|
||||
from tests.support.db import make_test_session
|
||||
from web.backend.database import engine as app_engine
|
||||
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.schemas import DouyuTaskBatchRequest
|
||||
from web.backend.services import (
|
||||
@@ -20,7 +20,9 @@ from web.backend.services import (
|
||||
douyu_service,
|
||||
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_bind import bind_poll_interval_seconds
|
||||
from web.backend.services.douyu_runner_core import (
|
||||
DouyuBatchRunnerCore,
|
||||
slim_douyu_task_result_for_storage,
|
||||
@@ -187,6 +189,77 @@ def test_cleanup_preserves_recoverable_binding_poll(db):
|
||||
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):
|
||||
_, _, task = _fixtures(db, status="running")
|
||||
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
|
||||
|
||||
|
||||
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(
|
||||
db, monkeypatch
|
||||
):
|
||||
@@ -518,6 +815,75 @@ def test_qr_preflight_accepts_existing_action_alias_pending_same_role(
|
||||
assert task.result["bind_decision"]["reason"] == "existing_actionable_pending"
|
||||
|
||||
|
||||
def test_qr_preflight_accepts_same_bound_role_that_still_needs_binding(
|
||||
db, monkeypatch
|
||||
):
|
||||
"""用户重新绑定同名角色时,bindAct=1 不应阻断可确认状态。"""
|
||||
_, account, task = _fixtures(db, status="running")
|
||||
role = "你哥奢皇"
|
||||
legacy = {
|
||||
"act_alias": "cjm",
|
||||
"role_name": role,
|
||||
"area_name": "微信",
|
||||
"plat_name": "安卓(android)",
|
||||
"is_bound_account": True,
|
||||
"is_bound_role": True,
|
||||
"is_bound_act": False,
|
||||
"need_bind_act": False,
|
||||
"need_bind_role": True,
|
||||
"change_role_wait_time": 0,
|
||||
"can_change_time": 0,
|
||||
"can_change_role": True,
|
||||
}
|
||||
activity = {
|
||||
**legacy,
|
||||
"act_alias": "20260120QYOOB",
|
||||
"is_bound_act": True,
|
||||
"need_bind_act": True,
|
||||
"can_change_time": 1_790_870_400,
|
||||
}
|
||||
client = type(
|
||||
"Client",
|
||||
(),
|
||||
{"get_bind_qr": lambda self, _alias: {"url": "https://example.test/qr"}},
|
||||
)()
|
||||
runner = DouyuBatchRunner(
|
||||
db,
|
||||
task.batch_id,
|
||||
"get_bind_qr",
|
||||
close_db=False,
|
||||
)
|
||||
monkeypatch.setattr(runner, "_client", lambda _cookie: client)
|
||||
monkeypatch.setattr(
|
||||
runner,
|
||||
"_fetch_bind_info_candidates",
|
||||
lambda *_args: [legacy, activity],
|
||||
)
|
||||
|
||||
runner._execute_get_bind_qr(
|
||||
db,
|
||||
task,
|
||||
account,
|
||||
"PHPSESSID=x",
|
||||
{
|
||||
"bind_act_alias": "20260120QYOOB",
|
||||
"confirm_act_alias": "20260120QYOOB",
|
||||
"legacy_act_alias": "cjm",
|
||||
},
|
||||
)
|
||||
|
||||
db.refresh(task)
|
||||
assert task.status == "success"
|
||||
assert task.result["url"] == "https://example.test/qr"
|
||||
assert task.result["pending_role_name"] == role
|
||||
assert task.result["bind_ready_for_confirm"] is True
|
||||
assert task.result["bind_decision"]["alias"] == "20260120QYOOB"
|
||||
assert (
|
||||
task.result["bind_decision"]["reason"]
|
||||
== "same_alias_after_bound_transition"
|
||||
)
|
||||
|
||||
|
||||
def test_binding_poll_accepts_existing_action_alias_pending_same_role(
|
||||
db, monkeypatch
|
||||
):
|
||||
@@ -688,6 +1054,7 @@ def test_douyu_lanes_separate_interactive_query_payment_and_writes():
|
||||
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_QR] == 10
|
||||
assert _DEFAULT_WORKERS[TaskLane.PAYMENT_POLL] == 3
|
||||
assert _DEFAULT_WORKERS[TaskLane.OVERVIEW_QUERY] == 5
|
||||
assert _DEFAULT_WORKERS[TaskLane.BINDING_POLL] == 6
|
||||
|
||||
|
||||
def test_slim_task_result_removes_debug_payloads():
|
||||
|
||||
@@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent
|
||||
PROJECT_ROOT = TESTS_DIR.parent
|
||||
VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions"
|
||||
|
||||
HEAD_REVISION = "20261121_0110"
|
||||
HEAD_REVISION = "20261121_0111"
|
||||
|
||||
# 迁移新增、但不声明在模型里的复合查询索引。
|
||||
EXTRA_INDEXES = (
|
||||
|
||||
@@ -438,9 +438,13 @@
|
||||
"revision": "20261121_0109",
|
||||
"sha256": "0aa85febbc0f2c99f5768bba2364f8797a748c4de740780e33a2017311f591b0"
|
||||
},
|
||||
"20261121_0110_account_platform_foundation.py": {
|
||||
"20261121_0110_douyu_role_api_logs.py": {
|
||||
"revision": "20261121_0110",
|
||||
"sha256": "94c0158a061a7670d1c15cfd57172d98eddd271a4d82be0cb7d0ea65013f8712"
|
||||
"sha256": "4f445f867e3fc41c7713f9296fe7be700823e936ed838039347e5b26abcd48a7"
|
||||
},
|
||||
"20261121_0111_account_platform_foundation.py": {
|
||||
"revision": "20261121_0111",
|
||||
"sha256": "0cf919b766769c84d357271acbf32bd968b2b6756b04cc059b26fd9fb5d41c10"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
+2
-2
@@ -5,8 +5,8 @@ from __future__ import annotations
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261121_0110"
|
||||
down_revision: str | None = "20261121_0109"
|
||||
revision: str = "20261121_0111"
|
||||
down_revision: str | None = "20261121_0110"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
@@ -602,6 +602,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):
|
||||
"""斗鱼兑换商品快照"""
|
||||
|
||||
|
||||
@@ -295,10 +295,9 @@ def binding_pending_decision(
|
||||
) -> dict[str, Any]:
|
||||
"""解释一个绑定候选是否可进入确认阶段。
|
||||
|
||||
角色名不是唯一事实:用户可能换绑到同名角色。判断还要对比扫码前后的
|
||||
活动绑定状态,尤其是活动 alias 从 ``bindAct=1`` 转为
|
||||
``bindAct=0 + needBindAct=1`` 的待确认转换。活动 alias 上已经存在的
|
||||
同名 pending 也不是“旧角色残留”,它仍然可以调用确认接口。
|
||||
角色名不是唯一事实,也不能要求每次都必须换新角色。用户重新绑定同名角色
|
||||
时,判断还要对比扫码前后的活动绑定状态。活动 alias 只要仍要求
|
||||
``needBindAct=1``,即使当前 ``bindAct=1``,同名角色也可以进入确认流程。
|
||||
"""
|
||||
result: dict[str, Any] = {
|
||||
"alias": str((info or {}).get("act_alias") or ""),
|
||||
@@ -354,10 +353,10 @@ def binding_pending_decision(
|
||||
if role_name.casefold() != baseline.casefold():
|
||||
result.update({"confirmable": True, "reason": "role_changed"})
|
||||
return result
|
||||
if snapshot["is_bound_act"]:
|
||||
result["reason"] = "already_bound"
|
||||
return result
|
||||
if not isinstance(baseline_info, dict):
|
||||
if snapshot["is_bound_act"]:
|
||||
result["reason"] = "already_bound"
|
||||
return result
|
||||
result["reason"] = "same_role_without_baseline_info"
|
||||
return result
|
||||
|
||||
@@ -387,6 +386,9 @@ def binding_pending_decision(
|
||||
}
|
||||
)
|
||||
return result
|
||||
if snapshot["is_bound_act"]:
|
||||
result["reason"] = "already_bound"
|
||||
return result
|
||||
result["reason"] = "same_role_stale"
|
||||
return result
|
||||
|
||||
|
||||
@@ -64,6 +64,7 @@ from .douyu_elite_status import (
|
||||
normalize_elite_expire_time,
|
||||
)
|
||||
from .douyu_goods_service import serialize_goods
|
||||
from .douyu_role_audit import record_role_api_candidates
|
||||
|
||||
SESSION_VALID = "valid"
|
||||
SESSION_INVALID = "invalid"
|
||||
@@ -297,23 +298,34 @@ def resolve_elite_role(
|
||||
)
|
||||
pending_info = None
|
||||
pending_decision: dict[str, Any] | None = None
|
||||
for alias in ordered_aliases:
|
||||
for info in candidates_info:
|
||||
if str(info.get("act_alias") or "") != alias:
|
||||
continue
|
||||
decision = binding_pending_decision(
|
||||
info,
|
||||
baseline_role_name=str((bound_info or {}).get("role_name") or ""),
|
||||
baseline_is_bound_act=is_bound_act(bound_info),
|
||||
baseline_info=bound_info,
|
||||
actionable_aliases=action_aliases,
|
||||
)
|
||||
if decision["confirmable"]:
|
||||
pending_info = info
|
||||
pending_decision = decision
|
||||
break
|
||||
if pending_info is not None:
|
||||
break
|
||||
baseline_role = str((bound_info or {}).get("role_name") or "").strip()
|
||||
|
||||
def find_pending(*, changed_role_only: bool) -> tuple[dict | None, dict | None]:
|
||||
for alias in ordered_aliases:
|
||||
for info in candidates_info:
|
||||
if str(info.get("act_alias") or "") != alias:
|
||||
continue
|
||||
role_name = str(info.get("role_name") or "").strip()
|
||||
if (
|
||||
changed_role_only
|
||||
and baseline_role
|
||||
and role_name.casefold() == baseline_role.casefold()
|
||||
):
|
||||
continue
|
||||
decision = binding_pending_decision(
|
||||
info,
|
||||
baseline_role_name=baseline_role,
|
||||
baseline_is_bound_act=is_bound_act(bound_info),
|
||||
baseline_info=bound_info,
|
||||
actionable_aliases=action_aliases,
|
||||
)
|
||||
if decision["confirmable"]:
|
||||
return info, decision
|
||||
return None, None
|
||||
|
||||
pending_info, pending_decision = find_pending(changed_role_only=True)
|
||||
if pending_info is None:
|
||||
pending_info, pending_decision = find_pending(changed_role_only=False)
|
||||
if bound_info is not None:
|
||||
return _role_result(
|
||||
status=ROLE_BOUND,
|
||||
@@ -1030,6 +1042,13 @@ def persist_probe_result(
|
||||
pass
|
||||
|
||||
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):
|
||||
role = roles.get(scope)
|
||||
if isinstance(role, dict):
|
||||
@@ -1043,6 +1062,23 @@ def persist_probe_result(
|
||||
observed_at=now,
|
||||
raw_payload=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 {}
|
||||
if gold.get("status") == CAPABILITY_OK and _to_int(gold.get("balance")) is not None:
|
||||
|
||||
@@ -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",
|
||||
]
|
||||
@@ -3,6 +3,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import TYPE_CHECKING, Any
|
||||
@@ -37,14 +38,46 @@ from .douyu_account_probe import (
|
||||
resolve_elite_role,
|
||||
)
|
||||
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_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_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_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:
|
||||
"""游戏角色绑定域:绑定状态机、扫码/确认绑定、换绑冷却。"""
|
||||
|
||||
@@ -157,6 +190,113 @@ class BindMixin:
|
||||
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:
|
||||
return binding_snapshot(bind_info)
|
||||
|
||||
@@ -497,7 +637,22 @@ class BindMixin:
|
||||
“是否 pending”只调用 ``_is_pending_role``;二维码轮询、查询角色和换绑
|
||||
时间查询都从这里选候选,避免各自用角色名或 bindRole 再写一套判断。
|
||||
"""
|
||||
for info in self._ordered_bind_candidates(candidates, prefer_aliases):
|
||||
ordered = self._ordered_bind_candidates(candidates, prefer_aliases)
|
||||
baseline = baseline_role_name.strip().casefold()
|
||||
if baseline:
|
||||
for info in ordered:
|
||||
role_name = str(info.get("role_name") or "").strip()
|
||||
if not role_name or role_name.casefold() == baseline:
|
||||
continue
|
||||
if self._is_pending_role(
|
||||
info,
|
||||
baseline_role_name=baseline_role_name,
|
||||
baseline_is_bound_act=baseline_is_bound_act,
|
||||
baseline_info=baseline_info,
|
||||
actionable_aliases=actionable_aliases,
|
||||
):
|
||||
return info
|
||||
for info in ordered:
|
||||
if self._is_pending_role(
|
||||
info,
|
||||
baseline_role_name=baseline_role_name,
|
||||
@@ -732,7 +887,6 @@ class BindMixin:
|
||||
task.queue_lane = "binding_poll"
|
||||
task.next_run_at = now + timedelta(seconds=max(1, interval_seconds))
|
||||
task.last_run_at = now
|
||||
task.attempt_count = int(task.attempt_count or 0) + 1
|
||||
|
||||
def poll_bind_role_once(
|
||||
self,
|
||||
@@ -754,7 +908,19 @@ class BindMixin:
|
||||
if not query_aliases:
|
||||
query_aliases = self._query_bind_act_aliases(config)
|
||||
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(
|
||||
{
|
||||
"bind_polling": False,
|
||||
@@ -771,17 +937,78 @@ class BindMixin:
|
||||
"绑定二维码已过期,未识别到新扫码角色",
|
||||
result,
|
||||
)
|
||||
return
|
||||
|
||||
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:
|
||||
attempts = max(1, int(task.attempt_count or 1))
|
||||
backoff = min(30, max(DOUYU_BIND_ROLE_POLL_INTERVAL, attempts * 2))
|
||||
if deadline is not None and datetime.now(UTC) >= deadline:
|
||||
finish_timeout()
|
||||
return
|
||||
result["bind_poll_error"] = "所有 actAlias 查询绑定信息失败"
|
||||
result["bind_polling"] = True
|
||||
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(
|
||||
db,
|
||||
task,
|
||||
@@ -833,7 +1060,7 @@ class BindMixin:
|
||||
)
|
||||
self._push_binding_decision_log(
|
||||
task=task,
|
||||
stage=f"poll:{int(result.get('poll_count') or 0) + 1}",
|
||||
stage=f"poll:{poll_number}",
|
||||
candidates=candidates,
|
||||
decisions=candidate_decisions,
|
||||
selected=bind_info,
|
||||
@@ -841,6 +1068,63 @@ class BindMixin:
|
||||
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:
|
||||
task.queue_lane = ""
|
||||
task.next_run_at = None
|
||||
@@ -853,8 +1137,8 @@ class BindMixin:
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "role_ready",
|
||||
"bind_polling": False,
|
||||
"pending_role_name": snapshot["role_name"],
|
||||
"poll_count": int(result.get("poll_count") or 0) + 1,
|
||||
"pending_role_name": snapshot["role_name"],
|
||||
"poll_count": poll_number,
|
||||
"bind_summary": summary,
|
||||
"action_act_aliases": action_aliases,
|
||||
"bind_decision": selected_decision,
|
||||
@@ -894,17 +1178,20 @@ class BindMixin:
|
||||
"bind_confirmed": False,
|
||||
"bind_phase": "waiting_scan",
|
||||
"bind_polling": True,
|
||||
"poll_count": int(result.get("poll_count") or 0) + 1,
|
||||
"poll_count": poll_number,
|
||||
"bind_summary": summary,
|
||||
"action_act_aliases": action_aliases,
|
||||
"bind_decision": selected_decision,
|
||||
"bind_candidate_decisions": candidate_decisions,
|
||||
}
|
||||
)
|
||||
if deadline is not None and datetime.now(UTC) >= deadline:
|
||||
finish_timeout()
|
||||
return
|
||||
self._schedule_next_bind_poll(
|
||||
task,
|
||||
now=now,
|
||||
interval_seconds=DOUYU_BIND_ROLE_POLL_INTERVAL,
|
||||
interval_seconds=interval_seconds,
|
||||
)
|
||||
self._update_task_progress(
|
||||
db,
|
||||
@@ -934,6 +1221,15 @@ class BindMixin:
|
||||
db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias"
|
||||
)
|
||||
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 的换绑最新态
|
||||
before = self._pick_baseline_bind_info(before_candidates, config) or {}
|
||||
# 换绑冷却必须看活动当前绑定(QYOOB),不要用 cjm
|
||||
@@ -1078,6 +1374,17 @@ class BindMixin:
|
||||
"bind_decision": pending_decision,
|
||||
"bind_candidate_decisions": candidate_decisions,
|
||||
"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"]:
|
||||
pending_snapshot = self._bind_snapshot(pending_before)
|
||||
@@ -1153,6 +1460,15 @@ class BindMixin:
|
||||
db, task, "failed", "查询绑定信息失败,请检查 Cookie 或 actAlias"
|
||||
)
|
||||
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
|
||||
before_bound = self._pick_current_bound_info(
|
||||
before_candidates,
|
||||
@@ -1300,6 +1616,16 @@ class BindMixin:
|
||||
)
|
||||
return
|
||||
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(
|
||||
"info",
|
||||
f"确认绑定接口返回 act={use_confirm_alias} "
|
||||
@@ -1332,6 +1658,16 @@ class BindMixin:
|
||||
after = _pick_bound_after(after_candidates)
|
||||
if after is not None:
|
||||
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 before_bound is not None:
|
||||
|
||||
@@ -4,7 +4,7 @@ from __future__ import annotations
|
||||
|
||||
import os
|
||||
import uuid
|
||||
from datetime import UTC, datetime
|
||||
from datetime import UTC, datetime, timedelta
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy import and_, func, or_, select
|
||||
@@ -56,6 +56,8 @@ SUPPORTED_DOUYU_TASK_TYPES = {
|
||||
"exchange_xpd_goods": "兑换小店商品",
|
||||
}
|
||||
|
||||
BINDING_POLL_CLEANUP_GRACE_SECONDS = 120
|
||||
|
||||
DOUYU_HANDBOOK_SCOPES = {"elite", "esports", "peace"}
|
||||
DOUYU_HANDBOOK_TASK_TYPES = {
|
||||
"elite": {
|
||||
@@ -476,7 +478,8 @@ def cleanup_orphan_douyu_tasks(
|
||||
DouyuTask.queue_lane == "binding_poll",
|
||||
DouyuTask.next_run_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_(
|
||||
DouyuTask.task_type.in_(
|
||||
|
||||
@@ -88,7 +88,9 @@ ACTIVE_STATUSES = {
|
||||
"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
|
||||
DEFAULT_LIVE_CANDIDATE_LIMIT = 20
|
||||
|
||||
|
||||
@@ -54,7 +54,7 @@ class LaneConfig:
|
||||
_DEFAULT_WORKERS: dict[TaskLane, int] = {
|
||||
TaskLane.INTERACTIVE_BINDING: 3,
|
||||
TaskLane.BATCH_COORDINATOR: 2,
|
||||
TaskLane.BINDING_POLL: 2,
|
||||
TaskLane.BINDING_POLL: 6,
|
||||
TaskLane.LOGIN: 4,
|
||||
TaskLane.ACCOUNT_CHECK: 4,
|
||||
TaskLane.ACCOUNT_SYNC: 3,
|
||||
|
||||
@@ -1519,7 +1519,12 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
: (latestQueryGameTaskByAccount.get(qrTask.account_id) || null))
|
||||
: 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
|
||||
? (latestConfirmXpdBindTaskByAccount.get(qrTask.account_id) || null)
|
||||
: null;
|
||||
@@ -1616,11 +1621,7 @@ export default function DouyuTasksPage({ handbook }: { handbook: HandbookKind })
|
||||
};
|
||||
|
||||
const queryQrRole = () => {
|
||||
if (!qrTask) return;
|
||||
if (qrAutoPolling) {
|
||||
message.info('正在自动识别角色,请稍候');
|
||||
return;
|
||||
}
|
||||
if (!qrTask || qrQueryRunning) return;
|
||||
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 />}
|
||||
onClick={queryQrRole}
|
||||
loading={qrQueryRunning}
|
||||
disabled={qrAutoPolling}
|
||||
disabled={qrQueryRunning}
|
||||
>
|
||||
{qrAutoPolling ? '自动识别中' : '查询角色'}
|
||||
{qrQueryRunning ? '查询中' : '立即查询角色'}
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
|
||||
Reference in New Issue
Block a user