feat(automation): CK 自动重登(P1b)+ 失败分型与待人工队列

P1a 的巡检只产出「谁失效了」的清单,不兑现修复:1,356 个失效账号仍要人工逐个
选集→点重登→过滑块。线上实测 20 个失效账号重登 20/20 成功(2 分 13 秒、零极验),
证实失效是会话级过期而非凭证问题,可救量约 1,350。

后端:
- 迁移 20261002_0038:login_tasks 加 relogin_failure_kind(枚举),供作业决定
  「继续退避重试」还是「转人工」;不用 attempt_message 做字符串匹配(文案一改
  就失效,页面也要按类型筛选);
- jobs/ck_relogin.py:
  - 候选 = 巡检判定 invalid + 允许自动管理 + 无在途尝试 + 退避到期 + 未超每日上限
    + **未转人工**(极验/凭证问题不再自动重登);
  - 复用 LoginBatchRunner(mode="relogin"),成功才替换 CK(沿用既有安全语义)。
    自动化身份取最早的超管做 created_by,权限给 login:view_all 以免受归属限制;
    总时长上限压到 240 秒,避免长时间占住调度线程;
  - settle_results() 独立成纯函数便于单测:成功清空退避状态;失败按分型分流 ——
    captcha/credential 转人工(不排期)、email_quota 退避到次日零点+随机抖动、
    email_code/interrupted/network 走 1h→3h→12h→24h 阶梯;次数用尽也停下等人工;
  - 未知文案默认按可重试处理(有每日上限兜底),别把可能只是抖动的账号推给人工;
- 注册第二个作业「CK 自动重登」(batch 10 / 并发 3,**默认关闭**);
- 新增 GET /api/automation/manual-queue:按原因分组列出待人工处理的账号。可重试
  类型但已无下次排期的(次数用尽)单独成组 —— 否则会混进「验证码失败」里,客服
  看不出这个已经不会再自动试了。

前端:
- /automation 页新增「待人工处理」卡片,按分型分组展示(极验要过滑块、凭证要核对、
  次数用尽要人工确认),各带账号与最近失败原因。

测试:新增 26 条(分型顺序陷阱「上限」优先于「验证码」、退避阶梯、配额退避次日、
候选筛选、MySQL 方言编译、结算、待人工队列分组)。
提交前全量 297 通过、ruff 全绿、前端 tsc 通过。

注意:提交时另一路「测试改用 MySQL」的改造正在同一工作区进行,本次刻意只暂存
本功能相关文件,未触碰 tests/conftest.py、tests/support/ 及对方已转换的测试文件。
This commit is contained in:
yml2213
2026-10-02 20:51:19 +08:00
parent 5645f19102
commit 8fc8e623b9
11 changed files with 1000 additions and 65 deletions
+128 -40
View File
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from 'react';
import { useCallback, useEffect, useState } from "react";
import {
Alert,
Button,
@@ -12,20 +12,26 @@ import {
Table,
Tag,
Typography,
} from 'antd';
import type { ColumnsType } from 'antd/es/table';
import { ReloadOutlined, ThunderboltOutlined } from '@ant-design/icons';
import { message } from '../utils/antdMessage';
import { getErrorMessage } from '../utils/error';
import { automationApi, type AutomationJobItem, type AutomationRunItem } from '../api/modules';
import { usePermissions } from '../hooks/usePermissions';
import { formatTime } from '../utils/time';
} from "antd";
import type { ColumnsType } from "antd/es/table";
import { ReloadOutlined, ThunderboltOutlined } from "@ant-design/icons";
import { message } from "../utils/antdMessage";
import { getErrorMessage } from "../utils/error";
import {
automationApi,
type AutomationJobItem,
type AutomationManualGroup,
type AutomationManualSample,
type AutomationRunItem,
} from "../api/modules";
import { usePermissions } from "../hooks/usePermissions";
import { formatTime } from "../utils/time";
const STATUS_TAG: Record<string, { color: string; text: string }> = {
success: { color: 'green', text: '成功' },
failed: { color: 'red', text: '失败' },
running: { color: 'blue', text: '执行中' },
skipped: { color: 'default', text: '已跳过' },
success: { color: "green", text: "成功" },
failed: { color: "red", text: "失败" },
running: { color: "blue", text: "执行中" },
skipped: { color: "default", text: "已跳过" },
};
function secondsToHours(seconds: number): number {
@@ -37,12 +43,14 @@ export default function AutomationPage() {
const [schedulerEnabled, setSchedulerEnabled] = useState(true);
const [schedulerRunning, setSchedulerRunning] = useState(false);
const [runs, setRuns] = useState<AutomationRunItem[]>([]);
const [manualGroups, setManualGroups] = useState<AutomationManualGroup[]>([]);
const [manualTotal, setManualTotal] = useState(0);
const [loading, setLoading] = useState(false);
const [savingKey, setSavingKey] = useState<string | null>(null);
const [runningKey, setRunningKey] = useState<string | null>(null);
const [forms] = Form.useForm();
const { can } = usePermissions();
const canManage = can('automation:manage');
const canManage = can("automation:manage");
const load = useCallback(async () => {
setLoading(true);
@@ -53,6 +61,9 @@ export default function AutomationPage() {
setSchedulerRunning(data.scheduler.running);
const history = await automationApi.runs({ limit: 50 });
setRuns(history.items);
const manual = await automationApi.manualQueue();
setManualGroups(manual.groups);
setManualTotal(manual.total);
// 统一回填表单:initialValue 只在首次渲染生效,刷新后不会跟着变。
forms.setFieldsValue(
Object.fromEntries(
@@ -84,7 +95,10 @@ export default function AutomationPage() {
try {
await automationApi.updateJob(job.key, {
enabled: values.enabled,
interval_seconds: Math.max(Math.round((values.interval_hours ?? 6) * 3600), 300),
interval_seconds: Math.max(
Math.round((values.interval_hours ?? 6) * 3600),
300,
),
batch_size: values.batch_size,
concurrency: values.concurrency,
});
@@ -112,41 +126,76 @@ export default function AutomationPage() {
}
};
const manualColumns: ColumnsType<AutomationManualSample> = [
{ title: "记录", dataIndex: "task_id", width: 80 },
{
title: "账号",
dataIndex: "account_username",
width: 160,
ellipsis: true,
},
{ title: "已重试", dataIndex: "relogin_attempts", width: 80 },
{
title: "失败原因",
dataIndex: "last_error",
ellipsis: true,
render: (value: string) => value || "-",
},
{
title: "最近尝试",
dataIndex: "attempt_at",
width: 160,
render: (value: string | null) => formatTime(value),
},
];
const runColumns: ColumnsType<AutomationRunItem> = [
{
title: '开始时间',
dataIndex: 'started_at',
title: "开始时间",
dataIndex: "started_at",
width: 180,
render: (value: string | null) => formatTime(value),
},
{
title: '作业',
dataIndex: 'job_key',
title: "作业",
dataIndex: "job_key",
width: 120,
},
{
title: '结果',
dataIndex: 'status',
title: "结果",
dataIndex: "status",
width: 90,
render: (value: string) => {
const tag = STATUS_TAG[value] ?? { color: 'default', text: value || '-' };
const tag = STATUS_TAG[value] ?? {
color: "default",
text: value || "-",
};
return <Tag color={tag.color}>{tag.text}</Tag>;
},
},
{ title: '扫描', dataIndex: 'scanned', width: 80 },
{ title: '有效', dataIndex: 'valid', width: 80 },
{ title: "扫描", dataIndex: "scanned", width: 80 },
{ title: "有效", dataIndex: "valid", width: 80 },
{
title: '失效',
dataIndex: 'invalid',
title: "失效",
dataIndex: "invalid",
width: 80,
render: (value: number) => (value > 0 ? <Typography.Text type="warning">{value}</Typography.Text> : 0),
render: (value: number) =>
value > 0 ? (
<Typography.Text type="warning">{value}</Typography.Text>
) : (
0
),
},
{ title: '耗时', width: 90, render: (_, row) => formatDuration(row.started_at, row.finished_at) },
{ title: '说明', dataIndex: 'message', ellipsis: true },
{
title: "耗时",
width: 90,
render: (_, row) => formatDuration(row.started_at, row.finished_at),
},
{ title: "说明", dataIndex: "message", ellipsis: true },
];
return (
<Space direction="vertical" size="middle" style={{ width: '100%' }}>
<Space direction="vertical" size="middle" style={{ width: "100%" }}>
<Card
title="自动巡检"
extra={
@@ -182,7 +231,12 @@ export default function AutomationPage() {
/>
{jobs.map((job) => (
<Card key={job.key} type="inner" title={job.title} style={{ marginBottom: 16 }}>
<Card
key={job.key}
type="inner"
title={job.title}
style={{ marginBottom: 16 }}
>
<Descriptions size="small" column={2} style={{ marginBottom: 16 }}>
<Descriptions.Item label="最近运行">
{formatTime(job.last_run_at)}
@@ -192,34 +246,36 @@ export default function AutomationPage() {
</Descriptions.Item>
<Descriptions.Item label="最近结果">
{job.last_status ? (
<Tag color={STATUS_TAG[job.last_status]?.color ?? 'default'}>
<Tag color={STATUS_TAG[job.last_status]?.color ?? "default"}>
{STATUS_TAG[job.last_status]?.text ?? job.last_status}
</Tag>
) : (
'-'
"-"
)}
</Descriptions.Item>
<Descriptions.Item label="最近说明">{job.last_message || '-'}</Descriptions.Item>
<Descriptions.Item label="最近说明">
{job.last_message || "-"}
</Descriptions.Item>
</Descriptions>
<Form form={forms} layout="inline" onFinish={() => saveJob(job)}>
<Form.Item
name={[job.key, 'enabled']}
name={[job.key, "enabled"]}
label="启用"
valuePropName="checked"
>
<Switch disabled={!canManage} />
</Form.Item>
<Form.Item
name={[job.key, 'interval_hours']}
name={[job.key, "interval_hours"]}
label="单账号重检间隔(小时)"
>
<InputNumber min={0.1} step={1} disabled={!canManage} />
</Form.Item>
<Form.Item name={[job.key, 'batch_size']} label="每轮条数">
<Form.Item name={[job.key, "batch_size"]} label="每轮条数">
<InputNumber min={1} max={2000} disabled={!canManage} />
</Form.Item>
<Form.Item name={[job.key, 'concurrency']} label="并发">
<Form.Item name={[job.key, "concurrency"]} label="并发">
<InputNumber min={1} max={16} disabled={!canManage} />
</Form.Item>
<Form.Item>
@@ -258,6 +314,38 @@ export default function AutomationPage() {
))}
</Card>
<Card
title="待人工处理"
extra={
<Typography.Text type="secondary">
自动重登救不了的类型(共 {manualTotal} 个)
</Typography.Text>
}
>
{manualGroups.length === 0 ? (
<Typography.Text type="secondary">
暂无 —— 能自动重试的都会自己重试,需要人工的才出现在这里。
</Typography.Text>
) : (
manualGroups.map((group) => (
<Card
key={group.kind || "exhausted"}
type="inner"
title={`${group.label}(${group.count} 个)`}
style={{ marginBottom: 12 }}
>
<Table
rowKey="task_id"
size="small"
pagination={false}
dataSource={group.samples}
columns={manualColumns}
/>
</Card>
))
)}
</Card>
<Card title="运行历史">
<Table
rowKey="id"
@@ -273,9 +361,9 @@ export default function AutomationPage() {
}
function formatDuration(start: string | null, end: string | null): string {
if (!start || !end) return '-';
if (!start || !end) return "-";
const ms = new Date(end).getTime() - new Date(start).getTime();
if (!Number.isFinite(ms) || ms < 0) return '-';
if (!Number.isFinite(ms) || ms < 0) return "-";
if (ms < 1000) return `${ms}ms`;
return `${(ms / 1000).toFixed(1)}s`;
}