线上事故:分配给客服的账号(如「远行者103」)在斗鱼工作台与 CK 列表里 查不到,只有账号管理里还能搜到。原因是可见性判定写成了 LoginTask.status == "success",而按设计保留旧 CK 的三种状态都不再是 success: - 普通登录批次启动时会把复用的成功记录置为 pending/running(旧 CK 保留), 批次被停止或失败后一直停在 pending,账号永久消失; - CK 重登失败置为 relogin_failed(旧 CK 保留),账号从工作台消失; - 普通登录失败置为 failed(复用记录的旧 CK 未清空),同样消失。 线上现状:12545 个账号持有 CK,但只有 11703 个能出现在工作台,842 个 (644 重登失败 + 176 排队中 + …)对客服和管理员都不可见。 改动: - douyu_service 新增 cookie_holding_filter()/latest_cookie_login_task(), 以「记录里还留着 Cookie」作为唯一判据,并抽出 LOGIN_IN_FLIGHT_STATUSES 与 cookie_operable_filter() 供检测/重登入口复用; - 斗鱼工作台账号列表、任务账号校验、账号管理 has_cookie 筛选、分配校验与 概览、CK 管理/操作列表、首页 CK 统计统一改用该判据; - 工作台取 CK 从 latest_success_login_task 换成 latest_cookie_login_task, 保证重登失败/排队中的账号仍能被任务正常执行; - 新增 cleanup_orphan_login_tasks():进程重启后把遗留的 pending/running 收敛为 failed 并保留旧 CK,避免页面永久显示排队中。 新增回归测试覆盖上述状态下的可见性、可执行性与启动清理。
104 lines
3.5 KiB
Python
104 lines
3.5 KiB
Python
"""账号管理服务层:封装账号导入、查询等业务逻辑,隔离 core/ 依赖。"""
|
|
|
|
import csv
|
|
import re
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from ..models import Account
|
|
from .douyu_service import cookie_holding_account_ids_query
|
|
|
|
EMAIL_PATTERN = re.compile(r"^[^\s@|]+@[^\s@|]+\.[^\s@|]+$")
|
|
|
|
|
|
def split_account_line(line: str) -> list[str]:
|
|
"""拆分一行账号文本,支持 |、tab、逗号、空格分隔。"""
|
|
if "|" in line:
|
|
return line.split("|")
|
|
if "\t" in line:
|
|
return line.split("\t")
|
|
if "," in line:
|
|
return next(csv.reader([line]))
|
|
return line.split()
|
|
|
|
|
|
def cookie_account_ids_query(db: Session):
|
|
"""返回当前仍持有 Cookie 的账号ID子查询。
|
|
|
|
以「记录里还留着 CK」为准,而不是以登录批次状态为准:排队中的登录与
|
|
重登失败都会保留旧 CK,账号不应因此从筛选结果里消失。
|
|
"""
|
|
return cookie_holding_account_ids_query(db)
|
|
|
|
|
|
def parse_and_build_accounts(
|
|
db: Session,
|
|
text: str,
|
|
default_tag: str = "",
|
|
) -> tuple[list[Account], int, int]:
|
|
"""
|
|
解析批量导入文本,构建 Account ORM 对象列表,并过滤已存在的重复账号。
|
|
|
|
Args:
|
|
db: 数据库会话,用于查询已有账号
|
|
text: 批量导入文本,每行格式:用户名|密码|邮箱|邮箱密码|标签(可选)
|
|
default_tag: 统一标签兜底。某行未单独写标签时使用该值,
|
|
行内第5列标签优先。
|
|
|
|
Returns:
|
|
(accounts, skipped_count, duplicated_count)
|
|
skipped_count 为格式非法的行数;duplicated_count 为库中已存在或同批重复被跳过的行数。
|
|
"""
|
|
from core.douyu.email_verifier import get_email_config_for_account
|
|
|
|
default_tag = (default_tag or "").strip()
|
|
# 库中已有用户名(大小写不敏感),用于导入去重
|
|
existing_usernames = {
|
|
row[0].lower()
|
|
for row in db.query(Account.username).filter(Account.username != "").all()
|
|
}
|
|
accounts = []
|
|
skipped = 0
|
|
duplicated = 0
|
|
seen_in_batch: set[str] = set()
|
|
for line in text.strip().split("\n"):
|
|
line = line.strip()
|
|
if not line or line.startswith("#"):
|
|
continue
|
|
parts = split_account_line(line)
|
|
if len(parts) < 4:
|
|
skipped += 1
|
|
continue
|
|
|
|
username, password, email, email_password = [p.strip() for p in parts[:4]]
|
|
line_tag = parts[4].strip() if len(parts) > 4 else ""
|
|
# 行内标签优先,未写则用统一兜底标签
|
|
tag = line_tag or default_tag
|
|
if not all([username, password, email, email_password]):
|
|
skipped += 1
|
|
continue
|
|
if not EMAIL_PATTERN.match(email):
|
|
skipped += 1
|
|
continue
|
|
|
|
username_key = username.lower()
|
|
if username_key in existing_usernames or username_key in seen_in_batch:
|
|
duplicated += 1
|
|
continue
|
|
seen_in_batch.add(username_key)
|
|
|
|
email_cfg = get_email_config_for_account(email)
|
|
accounts.append(
|
|
Account(
|
|
username=username,
|
|
password=password,
|
|
email=email,
|
|
email_password=email_password,
|
|
email_imap_server=email_cfg["server"],
|
|
email_imap_port=email_cfg.get("port", 993),
|
|
email_imap_ssl=email_cfg.get("ssl", True),
|
|
tag=tag,
|
|
)
|
|
)
|
|
return accounts, skipped, duplicated
|