S1 加好了列,本提交把**读与写一起切**过去。这是设计里「同一份数据的读与写必须
在同一个提交里切」的要求:先切读或先切写都会出现「写账号、读记录」的中间态,
跑起来不报错,只是默默用错数据。
## 核心改动
**判定「谁有 CK」不再借道登录记录**
- `cookie_holding_filter()` / `cookie_operable_filter()` / `cookie_holding_account_ids_query()`
/ `visible_douyu_task_accounts()` 全部改为作用于 `Account`;
- `cookie_operable_filter()` 的「在途排除」仍查 `login_tasks`(那是**某次尝试**的属性),
用 `NOT EXISTS` 子查询 + 迁移 0042 新增的 `(account_id, attempt_status)` 索引;
- 删除死代码 `latest_cookie_login_task()`;`latest_cookie()` 改为直接读账号。
**CK 写入**
- `_write_login_success()` 写 `Account.cookie / cookie_updated_at / cookie_source`;
- `_mark_attempt_failed()` 改看**账号**有没有 CK(决定是否沿用旧的失败语义);
- 检测模式的 `task.cookie = ""` **不再清空账号的 CK**(见下「行为变更」)。
**自动化作业按账号调度**
- `ck_health`:`_select_tasks` → `_select_accounts`,`db.query(Account)`;新增
`count_remaining()`(刻意不带 LIMIT,否则积压数恒等于批次上限);
- `ck_relogin`:同样按账号选行;结算改为「账号 + 本轮尝试记录」,
退避进度写账号;`settle_fresh_results(batch_id)` 按 `attempt_batch_id` 配对,
`run_ck_relogin` 把 **account_ids** 交给 runner;
- 顺带让**没有任何登录历史的账号**也能被巡检/重登(以前它们永远进不了队列)。
**登录批次**
- `LoginBatchRunner` 去掉 `relogin_task_ids`,重登与登录统一走 `account_ids`;
- 批次行复用逻辑重写:只保证「同一账号不并发两行」,不再关心「哪一行持有 CK」。
失败行复用同时看 `attempt_status`(账号有 CK 时 `_mark_attempt_failed` 刻意不动
`status`,只看 status 会永远命不中 → 每次失败刷一行)。
**删除护栏语义变更(用户可感知)**
- 去掉「持有 CK 的记录不可删」——CK 在账号上,删历史不再影响登录态,历史终于可清理;
- **保留**「在途尝试不可删」(批次线程正拿着那条记录)。
## 行为变更(需评审)
1. **账号检测不再清空 CK**。旧代码在 check 模式写 `task.cookie = ""`,而那时记录的
cookie 就是账号唯一凭证 —— 一次「批量检测」会把账号登录态删掉。解耦后
「检测账号凭据」与「CK 是否有效」本就是两件事(后者归 `ck_health` 判),
继续清空等于凭空制造不可逆的数据丢失(重登当前成功率极低)。
2. **Cookie 页的记录单位从「登录记录」变成「账号」**,所有接口的 id 入参/出参
变成 `account_id`;`DELETE /cookies/{id}` 清的是该账号的 CK,**必写审计**。
3. 重复检测从「同一账号多行持 CK」改为「**同一个 CK 出现在多个账号上**」——
后者才是真风险(两个账号共用一个登录态)。用 sha256 指纹分组:
CK 是 AES-GCM + 随机 nonce 加密,**同一明文每次密文都不同**,比密文永远比不出重复。
## 修掉一个真实并发缺陷
`_write_login_success` 原先被传入 `self.db`(runner 主会话),而它是在**工作线程**里
用 `worker_db` 执行的。`emit()` 会往 `account_events` 插一行,其 `account_id` 外键对
`accounts` 那一行加**共享锁**;这把锁加在另一个会话上,紧接着本会话的
`UPDATE accounts SET cookie=...` 就被自己人挡住 —— 实测 **50 秒后
`Lock wait timeout exceeded`,登录明明成功却写不回 CK**(改造前 UPDATE 打在
`login_tasks` 上所以没撞上)。改为传 `worker_db`:事件本就该和状态变更同事务提交,
顺带修掉跨线程共用一个 Session 的隐患。
## 验证
- 全量 pytest **368 passed / 6 skipped**;迁移链 3 passed;
- 前端 `tsc -b && vite build` 通过;
- `ruff check` / `ruff format --check` 全绿(顺带修掉 main 上遗留的 F821);
- 新增/改写的关键回归:**删掉全部登录历史后账号的 CK 与健康状态仍在**、
**无登录历史的账号也能进巡检队列**、结算不碰 CK、删除护栏改为在途语义。
仍未做(设计里排期的后续阶段):`account_lifecycle` 状态收口(S3)、事件发射点补齐(S4)、
业务事实直发事件(S5)、账号页重构(S6)。注意 `status` 目前只有模型默认值与迁移回填,
所以 `disabled/archived` 还没有任何写入方——S3 落地时要把自动化选行的状态过滤一起补上。
287 lines
10 KiB
Python
287 lines
10 KiB
Python
"""自动化作业接口。
|
||
|
||
设计上「先能看、再能开」:查看需要 ``automation:view``(运营也有),改配置与
|
||
手动触发需要 ``automation:manage``(仅超管)。作业默认关闭,打开后由进程内
|
||
调度器按间隔执行。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from datetime import UTC, datetime
|
||
from typing import Any
|
||
|
||
from fastapi import APIRouter, Depends, HTTPException, Query
|
||
from pydantic import BaseModel, Field
|
||
from sqlalchemy import and_, or_, select
|
||
from sqlalchemy.orm import Session
|
||
|
||
from ..database import get_db
|
||
from ..deps import require_permission
|
||
from ..models import Account, AutomationJob, AutomationRun, LoginTask, User
|
||
from ..services.audit_service import record_audit
|
||
from ..services.automation import (
|
||
ensure_jobs,
|
||
registered_jobs,
|
||
run_job_now,
|
||
scheduler_status,
|
||
)
|
||
|
||
router = APIRouter(prefix="/api/automation", tags=["自动化"])
|
||
|
||
MAX_BATCH_SIZE = 2000
|
||
MIN_INTERVAL_SECONDS = 300
|
||
|
||
|
||
class JobUpdate(BaseModel):
|
||
"""作业配置更新;只传要改的字段。"""
|
||
|
||
enabled: bool | None = None
|
||
interval_seconds: int | None = Field(default=None, ge=MIN_INTERVAL_SECONDS)
|
||
batch_size: int | None = Field(default=None, ge=1, le=MAX_BATCH_SIZE)
|
||
concurrency: int | None = Field(default=None, ge=1, le=16)
|
||
|
||
|
||
def _job_out(job: AutomationJob) -> dict[str, Any]:
|
||
return {
|
||
"key": job.key,
|
||
"title": job.title,
|
||
"enabled": bool(job.enabled),
|
||
"interval_seconds": int(job.interval_seconds or 0),
|
||
"batch_size": int(job.batch_size or 0),
|
||
"concurrency": int(job.concurrency or 0),
|
||
"last_run_at": job.last_run_at.isoformat() if job.last_run_at else None,
|
||
"next_run_at": job.next_run_at.isoformat() if job.next_run_at else None,
|
||
"last_status": job.last_status or "",
|
||
"last_message": job.last_message or "",
|
||
"registered": job.key in registered_jobs(),
|
||
}
|
||
|
||
|
||
def _run_out(run: AutomationRun) -> dict[str, Any]:
|
||
return {
|
||
"id": run.id,
|
||
"job_key": run.job_key,
|
||
"started_at": run.started_at.isoformat() if run.started_at else None,
|
||
"finished_at": run.finished_at.isoformat() if run.finished_at else None,
|
||
"status": run.status or "",
|
||
"scanned": int(run.scanned or 0),
|
||
"valid": int(run.valid or 0),
|
||
"invalid": int(run.invalid or 0),
|
||
"skipped": int(run.skipped or 0),
|
||
"message": run.message or "",
|
||
"detail": run.detail or None,
|
||
}
|
||
|
||
|
||
@router.get("/jobs")
|
||
def list_jobs(
|
||
db: Session = Depends(get_db),
|
||
current: User = Depends(require_permission("automation:view")),
|
||
):
|
||
"""列出作业配置、最近一次结果与调度器状态。"""
|
||
ensure_jobs(db)
|
||
jobs = db.query(AutomationJob).order_by(AutomationJob.key.asc()).all()
|
||
latest: dict[str, AutomationRun] = {}
|
||
for run in (
|
||
db.query(AutomationRun).order_by(AutomationRun.id.desc()).limit(200).all()
|
||
):
|
||
latest.setdefault(run.job_key, run)
|
||
# 巡检与重登是两个作业,但通过数据库联动:巡检把行标成 invalid,重登作业的
|
||
# 候选条件正是这个字段。这里把「当前有多少条等着重登」一并返回,页面才能把
|
||
# 这层上下游关系显示出来,而不是让人以为两个开关互不相干。
|
||
from ..services.automation.jobs.ck_relogin import count_pending_relogin
|
||
|
||
pending_relogin = count_pending_relogin(db)
|
||
|
||
return {
|
||
"scheduler": scheduler_status(),
|
||
"pending_relogin": pending_relogin,
|
||
"jobs": [
|
||
{
|
||
**_job_out(job),
|
||
"latest_run": _run_out(latest[job.key]) if job.key in latest else None,
|
||
}
|
||
for job in jobs
|
||
],
|
||
}
|
||
|
||
|
||
@router.patch("/jobs/{key}")
|
||
def update_job(
|
||
key: str,
|
||
payload: JobUpdate,
|
||
db: Session = Depends(get_db),
|
||
current: User = Depends(require_permission("automation:manage")),
|
||
):
|
||
"""改开关 / 间隔 / 批量 / 并发。打开开关时排定下一轮时间。"""
|
||
ensure_jobs(db)
|
||
job = db.query(AutomationJob).filter(AutomationJob.key == key).first()
|
||
if job is None:
|
||
raise HTTPException(status_code=404, detail="作业不存在")
|
||
|
||
changed: dict[str, Any] = {}
|
||
if payload.enabled is not None and bool(payload.enabled) != bool(job.enabled):
|
||
job.enabled = bool(payload.enabled)
|
||
changed["enabled"] = job.enabled
|
||
# 刚打开时排一轮:不早于一个 tick,避免打开瞬间就压满 CPU。
|
||
job.next_run_at = datetime.now(UTC) + _tick_delta() if job.enabled else None
|
||
if payload.interval_seconds is not None:
|
||
changed["interval_seconds"] = payload.interval_seconds
|
||
job.interval_seconds = payload.interval_seconds
|
||
if payload.batch_size is not None:
|
||
changed["batch_size"] = payload.batch_size
|
||
job.batch_size = payload.batch_size
|
||
if payload.concurrency is not None:
|
||
changed["concurrency"] = payload.concurrency
|
||
job.concurrency = payload.concurrency
|
||
|
||
if changed:
|
||
job.updated_at = datetime.now(UTC)
|
||
record_audit(
|
||
db,
|
||
current,
|
||
action="automation:update",
|
||
target=key,
|
||
detail=changed,
|
||
)
|
||
db.commit()
|
||
return _job_out(job)
|
||
|
||
|
||
@router.post("/jobs/{key}/run")
|
||
def run_job(
|
||
key: str,
|
||
db: Session = Depends(get_db),
|
||
current: User = Depends(require_permission("automation:manage")),
|
||
):
|
||
"""立刻跑一轮(不看 enabled)。用于正式打开开关前先试跑。"""
|
||
ensure_jobs(db)
|
||
try:
|
||
outcome = run_job_now(db, key)
|
||
except KeyError:
|
||
raise HTTPException(status_code=404, detail="作业不存在") from None
|
||
record_audit(
|
||
db,
|
||
current,
|
||
action="automation:run",
|
||
target=key,
|
||
detail={
|
||
"status": outcome.get("status"),
|
||
"scanned": outcome.get("scanned"),
|
||
"valid": outcome.get("valid"),
|
||
"invalid": outcome.get("invalid"),
|
||
"message": outcome.get("message"),
|
||
},
|
||
)
|
||
db.commit()
|
||
return outcome
|
||
|
||
|
||
# 失败分型 → 页面展示用的中文说明。「转人工」的两种要写清为什么,
|
||
# 否则客服不知道该做什么。
|
||
MANUAL_KIND_LABELS = {
|
||
"captcha": "极验滑块:需要人工过一次验证",
|
||
"credential": "凭证问题:需要核对账号密码 / 邮箱",
|
||
"": "重试次数用尽:需要人工确认账号是否还可用",
|
||
}
|
||
|
||
|
||
def _manual_kind_label(kind: str) -> str:
|
||
return MANUAL_KIND_LABELS.get(kind, f"待确认({kind})")
|
||
|
||
|
||
@router.get("/manual-queue")
|
||
def manual_queue(
|
||
limit: int = Query(default=20, ge=1, le=100),
|
||
db: Session = Depends(get_db),
|
||
current: User = Depends(require_permission("automation:view")),
|
||
):
|
||
"""待人工处理的重登失败账号,按失败原因分组。
|
||
|
||
自动重登只处理「还能自动救」的失败;极验滑块、凭证问题、以及重试次数用尽的
|
||
必须交给人 —— 这个接口就是那份清单(规划 §7.4.6)。
|
||
"""
|
||
from ..services.automation.jobs.ck_relogin import MANUAL_KINDS
|
||
|
||
# 解耦后这一队列的单位是**账号**(2026-10-04):重登进度与 CK 都在 accounts 上。
|
||
# 「最近尝试时间」仍来自登录记录(那是尝试的属性),用相关子查询取最后一条。
|
||
latest_attempt_at = (
|
||
select(LoginTask.attempt_at)
|
||
.where(LoginTask.account_id == Account.id)
|
||
.order_by(LoginTask.id.desc())
|
||
.limit(1)
|
||
.scalar_subquery()
|
||
)
|
||
|
||
# 转人工 = 分型属于需人工,或者次数用尽(next_at 已清空且失败过)。
|
||
manual_filter = or_(
|
||
Account.relogin_failure_kind.in_(tuple(sorted(MANUAL_KINDS))),
|
||
and_(
|
||
Account.relogin_last_error != "",
|
||
Account.relogin_next_at.is_(None),
|
||
Account.relogin_attempts > 0,
|
||
),
|
||
)
|
||
rows = (
|
||
db.query(Account, latest_attempt_at.label("last_attempt_at"))
|
||
.filter(Account.ck_check_status == "invalid")
|
||
.filter(Account.relogin_failure_kind != "")
|
||
.filter(manual_filter)
|
||
.order_by(latest_attempt_at.desc(), Account.id.desc())
|
||
.all()
|
||
)
|
||
grouped: dict[str, dict] = {}
|
||
for account, last_attempt_at in rows:
|
||
kind = account.relogin_failure_kind or ""
|
||
# 可重试的类型却已经没有下次排期 = 次数用尽,单独成组。
|
||
# 否则它会混进「验证码失败」组里,客服看不出「这个已经不会再自动试了」。
|
||
if kind not in MANUAL_KINDS:
|
||
kind = ""
|
||
group = grouped.setdefault(
|
||
kind,
|
||
{
|
||
"kind": kind,
|
||
"label": _manual_kind_label(kind),
|
||
"count": 0,
|
||
"samples": [],
|
||
},
|
||
)
|
||
group["count"] += 1
|
||
if len(group["samples"]) < limit:
|
||
group["samples"].append(
|
||
{
|
||
"account_id": account.id,
|
||
"account_username": account.username,
|
||
"relogin_attempts": int(account.relogin_attempts or 0),
|
||
"last_error": account.relogin_last_error or "",
|
||
"attempt_at": last_attempt_at.isoformat()
|
||
if last_attempt_at
|
||
else None,
|
||
}
|
||
)
|
||
groups = sorted(grouped.values(), key=lambda item: -item["count"])
|
||
return {"total": len(rows), "groups": groups}
|
||
|
||
|
||
@router.get("/runs")
|
||
def list_runs(
|
||
job_key: str | None = Query(default=None),
|
||
limit: int = Query(default=50, ge=1, le=200),
|
||
db: Session = Depends(get_db),
|
||
current: User = Depends(require_permission("automation:view")),
|
||
):
|
||
"""运行历史,最近的在前。"""
|
||
query = db.query(AutomationRun)
|
||
if job_key:
|
||
query = query.filter(AutomationRun.job_key == job_key)
|
||
runs = query.order_by(AutomationRun.id.desc()).limit(limit).all()
|
||
return {"items": [_run_out(run) for run in runs]}
|
||
|
||
|
||
def _tick_delta():
|
||
from datetime import timedelta
|
||
|
||
from ..services.automation.scheduler import TICK_SECONDS
|
||
|
||
return timedelta(seconds=TICK_SECONDS)
|