fix(douyu): 替换 CK 是最后一步,只有登录成功才写回记录

上一版把可见性改成「记录里还有 CK」,账号不会再消失,但根因还在:批次启动
时就把复用的成功记录改成 pending(旧 CK 仍在),停止或失败后没人恢复状态,
记录一直显示成一次没完成的登录。

按「替换 CK 是最后一步」把两者拆开:

- login_tasks 新增 attempt_status / attempt_message / attempt_batch_id /
  attempt_at,批次只写 attempt_*,status/cookie/message 只在登录成功时更新;
- _begin_attempt 只登记尝试,_mark_attempt_failed 只记录失败(已有 CK 的记录
  原样不动),_write_login_success 是唯一替换旧 CK 的入口;
- 停止批次、代理不可用、登录异常、进程重启清理全部改为收敛 attempt_*,
  因此停止一个批次不可能再让账号失去 CK;
- 复用的记录改用「持有 CK 的记录」,不再创建第二条记录,失败记录在无 CK 时
  才复用;
- 登录任务列表/统计按 attempt_* 展示「这次登录怎么样了」,批次筛选同时匹配
  attempt_batch_id;CK 操作页的重登状态也从 attempt_* 映射;
- 迁移 20261001_0035 归一存量数据:持有 CK 的记录 status 统一为 success、
  历史状态搬进 attempt_*,无 CK 且卡在 pending/running 的收敛为 failed。

新增测试:批次失败/停止不碰旧 CK、成功后替换 CK、列表展示尝试状态、
迁移存量数据归一。
This commit is contained in:
yml2213
2026-10-01 18:29:49 +08:00
parent 64c399617a
commit 6a25097412
10 changed files with 808 additions and 153 deletions
+31 -9
View File
@@ -207,8 +207,8 @@ class TestCookieOperation:
)
def test_relogin_failed_record_stays_visible_and_can_be_checked(self):
self.owned_task.status = "relogin_failed"
self.owned_task.message = "重新登录失败: 密码错误(旧 Cookie 已保留)"
self.owned_task.attempt_status = "failed"
self.owned_task.attempt_message = "重新登录失败: 密码错误(旧 Cookie 已保留)"
self.session.commit()
result = list_cookie_operations(
@@ -223,9 +223,13 @@ class TestCookieOperation:
assert result["total"] == 1
assert result["items"][0]["relogin_status"] == "relogin_failed"
assert "旧 Cookie 已保留" in result["items"][0]["relogin_message"]
# 失败的重登尝试不影响记录持有的 CK
assert self.owned_task.cookie == "owned-secret"
assert self.owned_task.status == "success"
def test_service_restart_cleans_orphan_relogin_tasks(self):
self.owned_task.status = "relogin_running"
self.owned_task.attempt_status = "running"
self.owned_task.attempt_message = "正在重新登录,旧 Cookie 保留中"
self.session.commit()
cleaned = cleanup_orphan_relogin_tasks(self.session)
@@ -233,13 +237,17 @@ class TestCookieOperation:
assert cleaned == 1
self.session.refresh(self.owned_task)
self.session.refresh(self.other_task)
assert self.owned_task.status == "relogin_failed"
assert "服务重启" in self.owned_task.message
# 只收敛尝试状态,已有 CK 与成功状态原封不动
assert self.owned_task.attempt_status == "failed"
assert "服务重启" in self.owned_task.attempt_message
assert self.owned_task.status == "success"
assert self.owned_task.cookie == "owned-secret"
assert self.other_task.attempt_status == ""
assert self.other_task.status == "success"
def test_service_restart_cleans_orphan_login_tasks(self):
"""普通登录批次遗留的 pending/running 必须收敛,且保留旧 CK。"""
self.owned_task.status = "pending"
"""普通登录批次遗留的在途尝试必须收敛,且保留旧 CK。"""
self.owned_task.attempt_status = "pending"
self.owned_task.cookie = "acf_uid=owned"
self.session.commit()
@@ -248,11 +256,25 @@ class TestCookieOperation:
assert cleaned == 1
self.session.refresh(self.owned_task)
self.session.refresh(self.other_task)
assert self.owned_task.status == "failed"
assert self.owned_task.attempt_status == "failed"
assert self.owned_task.cookie == "acf_uid=owned"
assert "服务重启" in self.owned_task.message
assert self.owned_task.status == "success"
assert "服务重启" in self.owned_task.attempt_message
assert self.other_task.status == "success"
def test_service_restart_cleans_legacy_in_flight_status(self):
"""升级前直接把状态写在 status 上的在途记录也要能收敛。"""
self.owned_task.status = "relogin_running"
self.owned_task.attempt_status = ""
self.session.commit()
cleaned = cleanup_orphan_login_tasks(self.session)
assert cleaned == 1
self.session.refresh(self.owned_task)
assert self.owned_task.attempt_status == "failed"
assert self.owned_task.cookie == "owned-secret"
def test_runner_copies_proxy_config_before_background_execution(self):
proxy = ProxyConfig(
enabled=True,