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:
@@ -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