修复(登录任务): 取码失败自动换渠道,被跳过账号也落日志

- 登录先用人工设置的默认接口(当前 iframe):等码超时/发信失败后,
  同一账号自动切到另一条通道(iframe <-> wgapi)再试一轮,每个账号
  最多切一次;DOUYU_LOGIN_EMAIL_FALLBACK_API=off 可关闭
- 步骤 4/5 日志写明当前取码通道,切渠道时单独告警,页面可直接看到
- 配额/账号上限挡下的账号不再「凭空消失」:落一行 skipped 任务,
  日志写明未执行原因;界面新增「已跳过」状态、筛选与重试
- 停止、服务重启收敛、执行线程异常这几条路径也补单账号日志
- 修复刷新线程与收尾写库的覆盖竞态(旧快照会吃掉最后的失败原因)
- 补测试:渠道回退、跳过行与日志、日志真正落库
This commit is contained in:
yml
2026-11-25 19:04:38 +08:00
parent 5efe6b6949
commit 5dec234e3e
10 changed files with 732 additions and 60 deletions
+155
View File
@@ -0,0 +1,155 @@
"""邮箱验证码渠道回退:默认通道失败后自动换另一条。
运营以前要在「登录设置 → 登录接口」里手工切 wgapi / iframe:某个渠道收不到
斗鱼的验证码邮件时,整批账号都会卡在同一个渠道上。现在的规则是:**先按人工
设置的默认通道登录**,只有它在取码这一步失败后才自动切到另一条再试一次。
"""
from __future__ import annotations
from types import SimpleNamespace
from core.douyu import create_login_api_strategy
from core.douyu.login import DouyuLogin, EmailCodeTimeoutError
def _login(strategy: str | None = None, max_retries: int = 20) -> DouyuLogin:
account = SimpleNamespace(
username="u",
password="p",
email="e@example.com",
email_password="ep",
email_imap_server="imap.example.com",
email_imap_port=993,
email_imap_ssl=True,
)
return DouyuLogin(
account=account,
proxy="http://127.0.0.1:1",
max_login_retries=max_retries,
api_strategy=create_login_api_strategy(strategy) if strategy else None,
)
def _stub(monkeypatch, client: DouyuLogin) -> None:
"""跳过真实代理切换与等待,只保留渠道切换逻辑。"""
monkeypatch.setattr(client, "_prepare_retry", lambda: True)
monkeypatch.setattr(client, "_sleep_interruptible", lambda _s: None)
def test_iframe_default_switches_to_wgapi_after_code_timeouts(monkeypatch):
"""默认通道是 iframe 时:先用 iframe,两轮收不到码后切 wgapi。"""
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
client = _login("iframe")
assert client.api.name == "iframe"
calls: list[str] = []
def steps(deadline: float = 0) -> str:
calls.append(client.api.name)
if len(calls) <= 2:
raise EmailCodeTimeoutError("等待验证码超时")
return "cookie"
monkeypatch.setattr(client, "_run_login_steps", steps)
_stub(monkeypatch, client)
result = client.login()
assert result.success is True
assert client.api.name == "wgapi"
assert calls == ["iframe", "iframe", "wgapi"]
def test_wgapi_default_switches_to_iframe(monkeypatch):
"""默认通道是 wgapi 时:反向切到 iframe。"""
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
client = _login()
assert client.api.name == "wgapi"
calls: list[str] = []
def steps(deadline: float = 0) -> str:
calls.append(client.api.name)
if len(calls) <= 2:
raise EmailCodeTimeoutError("等待验证码超时")
return "cookie"
monkeypatch.setattr(client, "_run_login_steps", steps)
_stub(monkeypatch, client)
result = client.login()
assert result.success is True
assert client.api.name == "iframe"
assert calls == ["wgapi", "wgapi", "iframe"]
def test_fallback_only_switches_once(monkeypatch):
"""两条通道都收不到码时收口,不能在 wgapi/iframe 之间来回打转。"""
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
client = _login("iframe")
calls: list[str] = []
def steps(deadline: float = 0) -> str:
calls.append(client.api.name)
raise EmailCodeTimeoutError("等待验证码超时")
monkeypatch.setattr(client, "_run_login_steps", steps)
_stub(monkeypatch, client)
result = client.login()
assert result.success is False
assert result.code == "email_code_timeout"
assert client.api.name == "wgapi"
# iframe 两轮 + wgapi 两轮,之后直接判定失败
assert calls == ["iframe", "iframe", "wgapi", "wgapi"]
def test_fallback_can_be_disabled_with_env(monkeypatch):
monkeypatch.setenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", "off")
client = _login("iframe")
calls: list[str] = []
def steps(deadline: float = 0) -> str:
calls.append(client.api.name)
raise EmailCodeTimeoutError("等待验证码超时")
monkeypatch.setattr(client, "_run_login_steps", steps)
_stub(monkeypatch, client)
result = client.login()
assert result.success is False
assert client.api.name == "iframe"
assert calls == ["iframe", "iframe"]
def test_send_email_failure_also_switches_channel(monkeypatch):
"""发信接口本身报错也是「这条通道不通」,同样换一条再试。"""
monkeypatch.delenv("DOUYU_LOGIN_EMAIL_FALLBACK_API", raising=False)
client = _login("wgapi")
calls: list[str] = []
def steps(deadline: float = 0) -> str:
calls.append(client.api.name)
if client.api.name == "wgapi":
raise ValueError("发送验证邮件失败: 服务繁忙")
return "cookie"
monkeypatch.setattr(client, "_run_login_steps", steps)
_stub(monkeypatch, client)
result = client.login()
assert result.success is True
assert calls == ["wgapi", "iframe"]
def test_channel_error_classifier_ignores_geetest_failures():
"""极验失败与收码通道无关:换通道只会白烧一次代理。"""
assert not DouyuLogin._is_email_channel_error(
ValueError("第二次登录失败: 验证码错误")
)
assert not DouyuLogin._is_email_channel_error(ValueError("极验验证失败: slide"))
assert DouyuLogin._is_email_channel_error(ValueError("提交验证码失败: 验证码错误"))
assert DouyuLogin._is_email_channel_error(ValueError("发送验证邮件失败: 繁忙"))