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:
@@ -19,7 +19,7 @@ from ..permissions import get_user_permissions, user_has_permission
|
||||
from ..schemas import CookieReloginRequest
|
||||
from ..services.cookie_check_service import check_douyu_cookie
|
||||
from ..services.douyu_service import (
|
||||
LOGIN_IN_FLIGHT_STATUSES,
|
||||
LOGIN_ATTEMPT_IN_FLIGHT_STATUSES,
|
||||
cookie_holding_filter,
|
||||
cookie_operable_filter,
|
||||
)
|
||||
@@ -76,6 +76,34 @@ def _order_cookie_tasks(query, selected_names: list[str]):
|
||||
return query.order_by(LoginTask.finished_at.desc(), LoginTask.id.desc())
|
||||
|
||||
|
||||
# CK 操作页沿用的重登状态词表;attempt_* 是唯一来源,历史值一并兼容。
|
||||
_RELOGIN_STATUS_BY_ATTEMPT = {
|
||||
"pending": "relogin_pending",
|
||||
"running": "relogin_running",
|
||||
"failed": "relogin_failed",
|
||||
"error": "relogin_failed",
|
||||
"relogin_pending": "relogin_pending",
|
||||
"relogin_running": "relogin_running",
|
||||
"relogin_failed": "relogin_failed",
|
||||
}
|
||||
|
||||
|
||||
def _relogin_display(task: LoginTask) -> dict:
|
||||
"""把最近一次登录尝试映射成 CK 操作页展示用的重登状态。
|
||||
|
||||
账号持有的 CK 与「这次尝试的结果」分开存储,页面只关心后者;尝试结束
|
||||
后旧 CK 仍在记录里,因此停止或失败都不会让这一行消失。
|
||||
"""
|
||||
attempt_status = task.attempt_status or ""
|
||||
return {
|
||||
"relogin_status": _RELOGIN_STATUS_BY_ATTEMPT.get(attempt_status, ""),
|
||||
"relogin_message": task.attempt_message or "",
|
||||
"relogin_batch_id": task.attempt_batch_id
|
||||
if attempt_status in LOGIN_ATTEMPT_IN_FLIGHT_STATUSES
|
||||
else "",
|
||||
}
|
||||
|
||||
|
||||
def _check_one_cookie(task: LoginTask) -> dict:
|
||||
"""将共享检测结果补充为 Cookie 记录的接口返回格式。"""
|
||||
result = check_douyu_cookie(task.cookie or "")
|
||||
@@ -291,11 +319,7 @@ def list_cookie_operations(
|
||||
"ck_check_status": task.ck_check_status or "",
|
||||
"ck_checked_at": _fmt_dt(task.ck_checked_at),
|
||||
"created_at": _fmt_dt(task.finished_at),
|
||||
"relogin_status": task.status if task.status != "success" else "",
|
||||
"relogin_message": task.message or "",
|
||||
"relogin_batch_id": task.batch_id
|
||||
if task.status in {"relogin_pending", "relogin_running"}
|
||||
else "",
|
||||
**_relogin_display(task),
|
||||
}
|
||||
for task in tasks
|
||||
],
|
||||
@@ -575,17 +599,16 @@ def _start_relogin_tasks(
|
||||
skipped.append(task.id)
|
||||
continue
|
||||
if not acc.password or not acc.email:
|
||||
task.status = "relogin_failed"
|
||||
task.message = "重新登录失败: 账号缺少密码或邮箱,旧 Cookie 已保留"
|
||||
task.finished_at = datetime.now(UTC)
|
||||
# 只记录这次尝试失败,账号已有的 CK 原封不动。
|
||||
task.attempt_status = "failed"
|
||||
task.attempt_message = "重新登录失败: 账号缺少密码或邮箱,旧 Cookie 已保留"
|
||||
task.attempt_at = datetime.now(UTC)
|
||||
skipped.append(task.id)
|
||||
continue
|
||||
if task.status in LOGIN_IN_FLIGHT_STATUSES:
|
||||
if task.attempt_status in LOGIN_ATTEMPT_IN_FLIGHT_STATUSES:
|
||||
skipped.append(task.id)
|
||||
continue
|
||||
task.status = "relogin_pending"
|
||||
task.message = "等待重新登录,旧 Cookie 已保留"
|
||||
task.finished_at = None
|
||||
# 排队状态由 LoginBatchRunner 在批次启动时写入 attempt_*(含批次号)。
|
||||
task_ids.append(task.id)
|
||||
|
||||
if not task_ids:
|
||||
|
||||
@@ -4,7 +4,7 @@ import asyncio
|
||||
import threading
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, WebSocket, WebSocketDisconnect
|
||||
from sqlalchemy import func
|
||||
from sqlalchemy import func, or_
|
||||
from sqlalchemy.orm import Session, defer, load_only
|
||||
|
||||
from ..database import SessionLocal, get_db
|
||||
@@ -18,12 +18,24 @@ from ..services.login_service import LoginBatchRunner, batch_registry
|
||||
router = APIRouter(prefix="/api/login", tags=["登录任务"])
|
||||
|
||||
|
||||
def _effective_attempt_status():
|
||||
"""页面展示用的状态:优先最近一次登录尝试,没有尝试时回退到 CK 状态。
|
||||
|
||||
``attempt_*`` 才是「这次登录怎么样了」,``status`` 只表示这条记录是否
|
||||
持有可用 CK —— 两者分开,停止/失败/重启都不会让账号从列表里消失。
|
||||
"""
|
||||
return func.coalesce(
|
||||
func.nullif(LoginTask.attempt_status, ""),
|
||||
LoginTask.status,
|
||||
)
|
||||
|
||||
|
||||
def _login_task_summary(query):
|
||||
"""按状态汇总登录任务,避免概览页拉完整任务列表。"""
|
||||
rows = (
|
||||
query.order_by(None)
|
||||
.with_entities(LoginTask.status, func.count(LoginTask.id))
|
||||
.group_by(LoginTask.status)
|
||||
.with_entities(_effective_attempt_status(), func.count(LoginTask.id))
|
||||
.group_by(_effective_attempt_status())
|
||||
.all()
|
||||
)
|
||||
status_counts = {status or "": count for status, count in rows}
|
||||
@@ -120,7 +132,10 @@ def list_tasks(
|
||||
query = query.filter(Account.assigned_to == current.id)
|
||||
|
||||
if batch_id:
|
||||
query = query.filter(LoginTask.batch_id == batch_id)
|
||||
# 记录持有的 CK 属于产出它的批次,最近一次尝试属于新批次,两者都要算。
|
||||
query = query.filter(
|
||||
or_(LoginTask.batch_id == batch_id, LoginTask.attempt_batch_id == batch_id)
|
||||
)
|
||||
|
||||
rows = query.order_by(LoginTask.id.desc()).limit(200).all()
|
||||
# 批量收集 account_id,一次性查出 username
|
||||
@@ -137,18 +152,21 @@ def list_tasks(
|
||||
|
||||
result = []
|
||||
for t in rows:
|
||||
attempt_status = t.attempt_status or ""
|
||||
result.append(
|
||||
LoginTaskOut(
|
||||
id=t.id,
|
||||
batch_id=t.batch_id,
|
||||
batch_id=t.attempt_batch_id or t.batch_id,
|
||||
account_id=t.account_id,
|
||||
account_username=accounts_map.get(t.account_id, ""),
|
||||
status=t.status,
|
||||
status=attempt_status or t.status,
|
||||
cookie="",
|
||||
message=t.message or "",
|
||||
message=(t.attempt_message if attempt_status else "")
|
||||
or t.message
|
||||
or "",
|
||||
created_by=t.created_by,
|
||||
created_at=t.created_at,
|
||||
finished_at=t.finished_at,
|
||||
finished_at=t.attempt_at or t.finished_at,
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user