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
@@ -0,0 +1,139 @@
"""login_tasks 增加 attempt_* 字段:登录尝试与 CK 记录分离。
事故背景:批次启动时会把复用的成功记录直接改成 pending(旧 CK 仍保留),
批次被停止或失败后没有任何地方把它改回成功,账号于是从工作台消失。
按「替换 CK 是最后一步、只有成功后才会替换旧的 CK」的原则,把尝试状态独立
出来:批次只写 attempt_*,只有登录成功才会把新 CK 写回 status/cookie/message,
因此停止、失败、进程重启都不可能破坏已有 CK。
存量数据归一:
- 持有 CK 但状态不是 success 的记录(relogin_failed/failed/pending/running…)
把状态、消息、批次、时间搬到 attempt_*,status 归一到 success,让 status
重新只表示「这条记录持有可用 CK」;
- 没有 CK 且卡在 pending/running 的记录收敛为 failed,避免永久显示排队中。
"""
from collections.abc import Sequence
import sqlalchemy as sa
from alembic import op
revision: str = "20261001_0035"
down_revision: str | None = "20260901_0034"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
# 升级前可能停留在这些状态、但旧 CK 已经保留在记录里的批次状态。
_INTERRUPTED_STATUSES = ("pending", "running", "relogin_pending", "relogin_running")
_INTERRUPTED_SQL = "(" + ",".join(f"'{item}'" for item in _INTERRUPTED_STATUSES) + ")"
# 历史重登状态统一归一为 failed,避免 attempt_status 里混入页面不认识的值。
_LEGACY_ATTEMPT_STATUSES = _INTERRUPTED_STATUSES + ("relogin_failed",)
_LEGACY_ATTEMPT_SQL = (
"(" + ",".join(f"'{item}'" for item in _LEGACY_ATTEMPT_STATUSES) + ")"
)
def upgrade() -> None:
bind = op.get_bind()
columns = {item["name"] for item in sa.inspect(bind).get_columns("login_tasks")}
if "attempt_status" not in columns:
# NOT NULL + server_default 会让存量行直接填上空串,避免出现 NULL
# 导致 NOT IN 查询把历史记录整批排除。
op.add_column(
"login_tasks",
sa.Column(
"attempt_status",
sa.String(length=32),
nullable=False,
server_default="",
),
)
if "attempt_message" not in columns:
op.add_column(
"login_tasks",
sa.Column(
"attempt_message",
sa.String(length=512),
nullable=False,
server_default="",
),
)
if "attempt_batch_id" not in columns:
op.add_column(
"login_tasks",
sa.Column(
"attempt_batch_id",
sa.String(length=64),
nullable=False,
server_default="",
),
)
if "attempt_at" not in columns:
op.add_column(
"login_tasks",
sa.Column("attempt_at", sa.DateTime(), nullable=True),
)
# 1) 持有 CK 的记录:历史批次状态搬进 attempt_*(status 稍后再归一,
# 避免同一条 UPDATE 里读到被覆盖后的值)。
bind.execute(
sa.text(
f"""
UPDATE login_tasks
SET attempt_status = CASE
WHEN status IN {_LEGACY_ATTEMPT_SQL} THEN 'failed'
ELSE status
END,
attempt_message = CASE
WHEN status IN {_INTERRUPTED_SQL}
THEN '登录已中断(旧 Cookie 已保留)'
ELSE COALESCE(message, '')
END,
attempt_batch_id = COALESCE(batch_id, ''),
attempt_at = COALESCE(finished_at, created_at)
WHERE cookie IS NOT NULL
AND cookie <> ''
AND status <> 'success'
AND (attempt_status IS NULL OR attempt_status = '')
"""
)
)
# 2) 持有 CK 的记录:status 只表示「持有可用 CK」。
bind.execute(
sa.text(
"""
UPDATE login_tasks
SET status = 'success'
WHERE cookie IS NOT NULL
AND cookie <> ''
AND status <> 'success'
"""
)
)
# 3) 没有 CK 且卡在批次中的记录:收敛为失败,不再永久排队中。
bind.execute(
sa.text(
f"""
UPDATE login_tasks
SET status = 'failed',
message = '登录已中断(服务重启),无 Cookie',
finished_at = COALESCE(finished_at, created_at),
attempt_status = 'failed',
attempt_message = '登录已中断(服务重启),无 Cookie',
attempt_at = COALESCE(attempt_at, created_at)
WHERE status IN {_INTERRUPTED_SQL}
AND (cookie IS NULL OR cookie = '')
AND (attempt_status IS NULL OR attempt_status = '')
"""
)
)
def downgrade() -> None:
bind = op.get_bind()
columns = {item["name"] for item in sa.inspect(bind).get_columns("login_tasks")}
for name in ("attempt_at", "attempt_batch_id", "attempt_message", "attempt_status"):
if name in columns:
op.drop_column("login_tasks", name)