Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(tasks): 自动修复的各批派发在任务中心合成一张进行中卡片

longmeidao20dde3665e21
diff --git a/frontend/src/react/activity/activity-page.tsx b/frontend/src/react/activity/activity-page.tsx
index 1c14bcc48..d120f7b38 100644
--- a/frontend/src/react/activity/activity-page.tsx
+++ b/frontend/src/react/activity/activity-page.tsx
@@ -35,11 +35,14 @@ import { BackgroundIssues } from './background-issues';
import { DownloadsPanel } from './downloads-panel';
import { ErrorDetails, ErrorExcerpt } from './error-excerpt';
import {
- elapsedText, fetchEarlier, fetchTasks, foldRoutine, followupDetail, groupFollowups, isActive,
+ elapsedText, fetchEarlier, fetchTasks, foldByKind, foldRoutine, followupDetail, followupTally, groupFollowups, isActive,
hasResultProblems, isRoutine, latestOfKind, mergeFinished, momentText, pollInterval, problemDestination, progressText,
- statusLabel, summaryItems, taskIssues, TASKS_KEY, TRIGGER_LABELS, type TaskRunPayload,
+ statusLabel, summaryItems, taskIssues, TASKS_KEY, TRIGGER_LABELS, type FollowupCounts, type TaskRunPayload,
} from './tasks';
+/** 一张卡里最多列多少条后继。几批合成一张卡时会有上千条,全画出来一屏都是同一句。 */
+const FOLLOWUP_ROWS = 50;
+
/* 完成绿、失败红、进行中蓝、意外中断黄,主动取消和排队为中性。 */
const BADGE_COLORS: Record<string, 'lime' | 'rose' | 'yellow' | 'blue' | 'neutral'> = {
succeeded: 'lime', failed: 'rose', cancelled: 'neutral', interrupted: 'yellow', running: 'blue', pending: 'neutral',
@@ -74,12 +77,15 @@ function FollowupList({ rows }: { rows: TaskRunPayload[] }) {
{detail
? <span className="min-w-0 break-words text-caption-1-regular text-text-secondary">{detail}</span>
: null}
- <div className="w-full min-w-0">
- <Disclosure summary="技术详情" contentClassName="pb-2.5">
- <CodeBlock code={JSON.stringify(row.result_summary, null, 2)} language="json"
- label={`${row.task_label}技术详情`} />
- </Disclosure>
- </div>
+ {/* 排队中的那些还没有结果:空的「技术详情」展开只有一对花括号。 */}
+ {Object.keys(row.result_summary || {}).length
+ ? <div className="w-full min-w-0">
+ <Disclosure summary="技术详情" contentClassName="pb-2.5">
+ <CodeBlock code={JSON.stringify(row.result_summary, null, 2)} language="json"
+ label={`${row.task_label}技术详情`} />
+ </Disclosure>
+ </div>
+ : null}
</li>
);
})}
@@ -90,10 +96,17 @@ function FollowupList({ rows }: { rows: TaskRunPayload[] }) {
/** 一轮任务一张卡。失败时整张卡的框线换成 danger 色,不给结束原因那行字上色——
* 一屏十几行里逐行读红字,比看一眼哪张卡的框是红的慢得多。 */
function RunCard(
- { run, meta, children, footer, followups }:
+ { run, meta, children, footer, followups, followupTotal = 0, badge }:
{ run: TaskRunPayload; meta: string; children?: ReactNode; footer?: string;
- followups?: TaskRunPayload[] },
+ followups?: TaskRunPayload[]; followupTotal?: number; badge?: ReactNode },
) {
+ /* 一轮派出上千条后继时只下发最近几十条,合并的几批又可能各带几十条:标题写全数,
+ 列表先列在跑与排队的、再列最近结束的,最多 `FOLLOWUP_ROWS` 条,并说清只列了其中几条。 */
+ const rank = (row: TaskRunPayload) => (row.status === 'running' ? 2 : row.status === 'pending' ? 1 : 0);
+ const listed = followups ? [...followups].sort((a, b) =>
+ rank(b) - rank(a) || b.id - a.id).slice(0, FOLLOWUP_ROWS) : [];
+ const shown = listed.length;
+ const total = Math.max(followups?.length || 0, followupTotal);
return (
// `id` 让「需要处理」那一行的「查看失败记录」能直接滚到这张卡。
<li id={`run-${run.id}`} data-status={run.status} data-task-key={run.task_key} data-run-id={run.id}
@@ -102,19 +115,23 @@ function RunCard(
bordered: 'line',
className: run.status === 'failed' ? 'flex flex-col border-border-error-default' : 'flex flex-col',
})}>
- <div className="flex flex-col gap-1.5 px-5 pt-5 pb-4">
+ {/* 卡片标题比分段标题(「正在进行」)低一档,标题、时间与摘要之间同一个行距。 */}
+ <div className="flex flex-col gap-2 px-5 py-4">
<div className="flex flex-wrap items-center gap-2">
- <strong className="min-w-0 break-words text-title-1-medium text-text-primary">{run.task_label}</strong>
- <StatusBadge run={run} />
+ <strong className="min-w-0 break-words text-title-3-medium text-text-primary">{run.task_label}</strong>
+ {badge ?? <StatusBadge run={run} />}
</div>
<p className="text-caption-1-regular text-text-secondary">{meta}</p>
{children}
</div>
- {followups?.length ? <div className="border-t border-separator-border px-5 py-2.5">
- <Disclosure summary={`任务明细 · ${followups.length} 项`}
+ {shown ? <div className="border-t border-separator-border px-5 py-2.5">
+ <Disclosure summary={`任务明细 · ${total.toLocaleString()} 项`}
contentClassName="pb-2.5"
- defaultOpen={followups.some((row) => isActive(row))}>
- <FollowupList rows={followups} />
+ defaultOpen={listed.some((row) => isActive(row))}>
+ {shown < total
+ ? <p className="text-caption-1-regular text-text-secondary">列出其中 {shown.toLocaleString()} 项,正在处理的排在前面</p>
+ : null}
+ <FollowupList rows={listed} />
</Disclosure>
</div> : null}
{Object.keys(run.result_summary || {}).length ? <div className="border-t border-separator-border px-5 py-2.5">
@@ -156,8 +173,9 @@ function RunningRun({ run, followups }: { run: TaskRunPayload; followups?: TaskR
* 任务卡只记这一轮的历史:此刻还没解决的问题和「立即重试」在页顶「需要处理」那一块。
* `latest` 说它是不是同一种任务最新的那一轮:有些任务只有那一轮还有处理入口。 */
function SettledRun(
- { run, followups, runs, latest }:
- { run: TaskRunPayload; followups?: TaskRunPayload[]; runs?: TaskRunPayload[]; latest: boolean },
+ { run, followups, runs, latest, followupTotal }:
+ { run: TaskRunPayload; followups?: TaskRunPayload[]; runs?: TaskRunPayload[]; latest: boolean;
+ followupTotal?: number },
) {
const summary = summaryItems(run.result_summary).filter((line) => line !== run.error);
const issues = taskIssues(run);
@@ -170,10 +188,13 @@ function SettledRun(
: [TRIGGER_LABELS[run.trigger] || run.trigger, momentText(run.finished_at),
elapsed && `用时 ${elapsed}`]).filter(Boolean).join(' · ');
return (
- <RunCard run={run} meta={meta} footer={run.error} followups={followups}>
- {summary.length ? <ul className="list-disc space-y-1 pl-5 text-caption-1-regular text-text-secondary wrap-anywhere">
- {summary.map((line, index) => <li key={index}>{line}</li>)}
- </ul> : null}
+ <RunCard run={run} meta={meta} footer={run.error} followups={followups} followupTotal={followupTotal}>
+ {/* 只有一句摘要时不加项目符号:单独一个圆点读起来像漏了下文。 */}
+ {summary.length === 1
+ ? <p className="text-caption-1-regular text-text-secondary wrap-anywhere">{summary[0]}</p>
+ : summary.length ? <ul className="flex list-disc flex-col gap-1 pl-5 text-caption-1-regular text-text-secondary wrap-anywhere">
+ {summary.map((line, index) => <li key={index}>{line}</li>)}
+ </ul> : null}
{issues.length ? <Disclosure summary={`问题详情 · ${issues.length} 项`} contentClassName="pt-2">
<ErrorDetails label="任务问题详情">
<ul className="flex min-w-0 flex-col gap-3" data-task-issues="">
@@ -194,6 +215,37 @@ function SettledRun(
);
}
+/** 自己已经跑完、派出的后继还没做完的几轮,同一种任务合成一张卡。
+ *
+ * 父任务那一轮只是「安排」,几秒就结束;用户关心的是安排下去的那批做到哪了。所以徽章写进行中,
+ * 进度条按全部后继的计数走,而不是按页面上列出的那几行。 */
+function WorkingRun(
+ { runs, followups, counts }:
+ { runs: TaskRunPayload[]; followups: TaskRunPayload[]; counts: FollowupCounts | undefined },
+) {
+ const run = runs[0]!;
+ const oldest = runs[runs.length - 1]!;
+ const tally = followupTally(counts, runs);
+ const total = Math.max(tally.total, followups.length);
+ const settled = tally.total ? tally.settled : followups.filter((row) => !isActive(row)).length;
+ const failed = tally.total ? tally.failed
+ : followups.filter((row) => row.status === 'failed' || row.status === 'interrupted').length;
+ const meta = [TRIGGER_LABELS[run.trigger] || run.trigger,
+ runs.length > 1 ? `${runs.length} 批` : '',
+ `${momentText(oldest.started_at || oldest.finished_at)} 开始`].filter(Boolean).join(' · ');
+ return (
+ <RunCard run={run} meta={meta} followups={followups} followupTotal={total}
+ badge={<Chip color="blue">进行中</Chip>}>
+ <div className="flex flex-col gap-1.5">
+ <Progress label={`${run.task_label}进度`} value={settled} max={total} />
+ <p className="text-caption-1-regular text-text-secondary">
+ 已处理 {progressText(settled, total)} 项{failed ? ` · 其中 ${failed.toLocaleString()} 项失败` : ''}
+ </p>
+ </div>
+ </RunCard>
+ );
+}
+
function Section({ title, children }: { title: string; children: ReactNode }) {
const id = useId();
return (
@@ -221,6 +273,7 @@ export function ActivityPage() {
// 往前翻接上的行。没翻过时一直是空的,页面和只有第一页时完全一样。
const [earlier, setEarlier] = useState<TaskRunPayload[]>([]);
const [earlierMore, setEarlierMore] = useState(false);
+ const [earlierCounts, setEarlierCounts] = useState<FollowupCounts>({});
const [seenTop, setSeenTop] = useState(top);
if (top !== seenTop) {
setSeenTop(top);
@@ -233,6 +286,7 @@ export function ActivityPage() {
onSuccess: (page, { shown }) => {
setEarlier((current) => mergeFinished(current, shown, page.finished || []));
setEarlierMore(Boolean(page.finished_has_more));
+ setEarlierCounts((current) => ({ ...current, ...page.followup_counts }));
},
});
const problem = tasks.error ? errorMessage(tasks.error) : '';
@@ -240,7 +294,8 @@ export function ActivityPage() {
if (!data) return <Page><Note tone="error">{problem || '读取任务中心失败'}</Note></Page>;
const allFinished = mergeFinished(data.finished || [], earlier);
- const allRuns = [...(data.running || []), ...(data.skipped || []), ...allFinished];
+ const parents = data.working_parents || [];
+ const allRuns = [...(data.running || []), ...(data.skipped || []), ...allFinished, ...parents];
// 后继挂到派出它的那张卡下面。父任务不在这一屏上(已经被 prune 掉)时照常单独摆出来
// ——挂不上去就不显示,等于让一条在跑的任务凭空消失。
const byParent = groupFollowups(allRuns);
@@ -254,16 +309,25 @@ export function ActivityPage() {
const loadEarlier = () => {
if (!more.isPending && oldest) more.mutate({ oldest, shown: allFinished });
};
+ const counts: FollowupCounts = { ...earlierCounts, ...data.followup_counts };
+ const childrenOf = (runs: TaskRunPayload[]) => runs.flatMap((run) => byParent.get(run.id) || []);
// 自己跑完、派出的后继还在跑的那一轮算「正在进行」:整件事还没完,放进「最近完成」
- // 就是一张卡上一个已完成、一个进行中。
- const working = settled.filter((run) => byParent.get(run.id)?.some(isActive));
+ // 就是一张卡上一个已完成、一个进行中。页面上只列得到一部分后继,所以先看计数。
+ const working = [...settled, ...topLevel(parents)].filter((run) => followupTally(counts, [run]).active > 0
+ || byParent.get(run.id)?.some(isActive)).sort((a, b) => b.id - a.id);
const running = topLevel(data.running || []);
const skipped = topLevel(data.skipped || []);
// 同一轮不在「最近完成」里再出现一次:一屏两行说的是同一件事,读起来像跑了两轮。
const finished = settled.filter((run) => !working.includes(run)
&& !skipped.some((row) => row.id === run.id));
// 每小时一轮的定时检查什么也没发生时一条条摆出来,一页就被同一句「已检查 83」占满。
- const folds = foldRoutine(finished, (run) => isRoutine(run) && !byParent.get(run.id)?.length);
+ // 派了后继的那一轮,后继全部顺利做完才算例行,有一条失败就单独摆出来。
+ const quietChildren = (run: TaskRunPayload) => {
+ const children = byParent.get(run.id) || [];
+ return children.every((row) => row.status === 'succeeded' && !hasResultProblems(row))
+ && followupTally(counts, [run]).failed === 0;
+ };
+ const folds = foldRoutine(finished, (run) => isRoutine(run) && quietChildren(run));
const newest = latestOfKind(allRuns);
const quiet = !running.length && !working.length && !skipped.length && !finished.length;
return (
@@ -284,8 +348,8 @@ export function ActivityPage() {
? <RunList live>
{running.map((run) => (
<RunningRun key={run.id} run={run} followups={byParent.get(run.id)} />))}
- {working.map((run) => (
- <SettledRun key={run.id} run={run} latest={newest.has(run.id)} followups={byParent.get(run.id)} />))}
+ {foldByKind(working).map(({ run, runs }) => (
+ <WorkingRun key={run.id} runs={runs} followups={childrenOf(runs)} counts={counts} />))}
</RunList>
: <Note tone="neutral">没有任务在跑。</Note>}
</Section>
@@ -302,7 +366,7 @@ export function ActivityPage() {
{finished.length
? <RunList>{folds.map(({ run, runs }) => (
<SettledRun key={run.id} run={run} runs={runs} latest={newest.has(run.id)}
- followups={byParent.get(run.id)} />))}</RunList>
+ followups={childrenOf(runs)} followupTotal={followupTally(counts, runs).total} />))}</RunList>
: null}
{more.error ? <Note tone="error">{errorMessage(more.error)}</Note> : null}
{hasEarlier
diff --git a/frontend/src/react/activity/tasks.ts b/frontend/src/react/activity/tasks.ts
index 3af448170..ed9be7cb3 100644
--- a/frontend/src/react/activity/tasks.ts
+++ b/frontend/src/react/activity/tasks.ts
@@ -43,12 +43,44 @@ export interface ActivityData {
finished: TaskRunPayload[];
/** 「最近完成」这一页后面还有更早的。 */
finished_has_more?: boolean;
+ /** 自己已结束、后继还在排或在跑的父任务里,不在 `running` 与 `finished` 这一页上的那些。 */
+ working_parents?: TaskRunPayload[];
+ /** 父任务 id → 它直接派出的后继按状态各几条。下发的后继只是一部分,进度与「还在不在跑」看这份。 */
+ followup_counts?: FollowupCounts;
}
+export type FollowupCounts = Record<string, Record<string, number>>;
+
/** 往前翻的一页:只有「最近完成」,在跑与被挡下的由轮询那一份负责。 */
export interface FinishedPage {
finished: TaskRunPayload[];
finished_has_more: boolean;
+ followup_counts?: FollowupCounts;
+}
+
+/** 几轮派出的后继合在一起数:共几条、结束了几条、还有几条在排或在跑。 */
+export function followupTally(counts: FollowupCounts | undefined, runs: TaskRunPayload[]) {
+ let total = 0, active = 0, failed = 0;
+ for (const run of runs) {
+ for (const [status, count] of Object.entries(counts?.[String(run.id)] || {})) {
+ total += count;
+ if (status === 'pending' || status === 'running') active += count;
+ if (status === 'failed' || status === 'interrupted') failed += count;
+ }
+ }
+ return { total, active, failed, settled: total - active };
+}
+
+/** 同一种任务连着派出的几轮合成一件事:自动修复每分钟派一批,一屏十几张「已完成」卡说的是同一件事。
+ * 每组按最新那一轮排在前面,`runs[0]` 是最新那一轮。 */
+export function foldByKind(rows: TaskRunPayload[]): RunFold[] {
+ const folds = new Map<string, RunFold>();
+ for (const run of rows) {
+ const fold = folds.get(run.task_key);
+ if (fold) fold.runs.push(run);
+ else folds.set(run.task_key, { run, runs: [run] });
+ }
+ return [...folds.values()];
}
/** 以 `oldest` 为游标取它之前的一页。游标是它的结束时刻加 id,与服务端的排序键一致。 */
diff --git a/frontend/test/react/activity.test.tsx b/frontend/test/react/activity.test.tsx
index 3d03eda00..80a060c9a 100644
--- a/frontend/test/react/activity.test.tsx
+++ b/frontend/test/react/activity.test.tsx
@@ -238,10 +238,40 @@ it('跑完那一轮显示摘要,失败那一轮显示原因', async () => {
expect(host.querySelector('[data-status=succeeded]')?.textContent).toContain('已检查 7');
expect(host.querySelector('[data-status=failed]')?.textContent).toContain('上游挡回来了');
expect(host.querySelector('[data-status=succeeded] span')?.textContent).toBe('已完成');
- // 跑成功那张没有说明区:没有原因可写时不留一条空条子。
- expect(host.querySelectorAll('[data-status=succeeded] p')).toHaveLength(1);
- expect([...host.querySelectorAll('[data-status=succeeded] ul > li')].map((node) => node.textContent))
- .toEqual(['已检查 7']);
+ // 跑成功那张没有说明区:没有原因可写时不留一条空条子。只有一句摘要时不加项目符号。
+ expect([...host.querySelectorAll('[data-status=succeeded] p')].map((node) => node.textContent))
+ .toEqual([expect.stringContaining('手动'), '已检查 7']);
+ expect(host.querySelector('[data-status=succeeded] ul')).toBeNull();
+});
+
+it('派出几十条后继的几批合成一张进行中的卡,进度按计数,正在跑的排最前,排队的不给空的技术详情', async () => {
+ const parent = (id: number, finished_at: string) => run({
+ id, task_key: 'upgrade-repairs', task_label: '自动修复', trigger: 'scheduled', status: 'succeeded',
+ progress_total: null, started_at: finished_at, finished_at, result_summary: { outcome: '已安排 64 项处理' } });
+ const child = (id: number, parentId: number, status: string) => followup(parentId, {
+ id, task_key: 'upgrade-library', task_label: '自动修复资料', status, finished_at: null,
+ progress_label: status === 'running' ? 'ABC-001 · 采集缺失资料' : 'ABC-00' + id, result_summary: {},
+ followup_key: `upgrade-library:x:${id}` });
+ const { host } = await open(payload({
+ running: [child(12, 2, 'pending'), child(11, 1, 'running')],
+ finished: [parent(2, '2026-10-28T09:03:00Z')],
+ // 更早的那一批不在「最近完成」这一页上,接口单独给。
+ working_parents: [parent(1, '2026-10-28T09:02:00Z')],
+ followup_counts: { 1: { succeeded: 60, failed: 2, running: 1, pending: 1 }, 2: { pending: 64 } },
+ }));
+ expect(sections(host)).toEqual(['正在进行']);
+ const cards = host.querySelectorAll('[data-task-key=upgrade-repairs]');
+ expect(cards).toHaveLength(1);
+ const card = cards[0]!;
+ expect(card.querySelector('span')?.textContent).toBe('进行中');
+ expect(card.textContent).toContain('2 批');
+ expect(card.textContent).toContain('已处理 62 / 128 项 · 其中 2 项失败');
+ expect(card.querySelector('summary')?.textContent).toBe('任务明细 · 128 项');
+ expect(card.textContent).toContain('列出其中 2 项,正在处理的排在前面');
+ const rows = [...card.querySelectorAll('[data-followup-key]')];
+ expect(rows.map((row) => row.getAttribute('data-status'))).toEqual(['running', 'pending']);
+ expect(rows[0]!.textContent).toContain('ABC-001 · 采集缺失资料');
+ expect(rows.some((row) => row.textContent?.includes('技术详情'))).toBe(false);
});
it('链接检查显示失效和访问失败数量,技术详情使用可复制代码块', async () => {
diff --git a/src/peach/task_runs.py b/src/peach/task_runs.py
index b8d0696a2..19f69ca10 100644
--- a/src/peach/task_runs.py
+++ b/src/peach/task_runs.py
@@ -729,11 +729,16 @@ class TaskRunStore:
一页数的是顶层那几轮,它们已结束的后继跟着一起回来:一轮拉取派出二十条后继时,
按行数翻页会让后继占满这一页、把派出它们的那一轮挤到下一页,页面上就只剩一串
没有父卡片可挂的散行。父行已被 `prune` 掉的后继自己算顶层。
+
+ 后继还在排或在跑的那几轮不算「完成」,由 `active_parents` 单独交给「正在进行」:
+ 自动修复每分钟派一批,它们留在这里会占满第一页,「最近完成」就只剩一个「加载更早」。
"""
size = max(1, min(int(limit), 500))
marks = ",".join(f"'{name}'" for name in TERMINAL_STATUSES)
+ active = ",".join(f"'{name}'" for name in ACTIVE_STATUSES)
where = (f"t.status IN ({marks}) AND (t.parent_run_id IS NULL OR NOT EXISTS "
- "(SELECT 1 FROM task_run p WHERE p.id=t.parent_run_id))")
+ "(SELECT 1 FROM task_run p WHERE p.id=t.parent_run_id)) AND NOT EXISTS "
+ f"(SELECT 1 FROM task_run c WHERE c.parent_run_id=t.id AND c.status IN ({active}))")
values: list[object] = []
if before is not None:
where += " AND (t.finished_at<? OR (t.finished_at=? AND t.id<?))"
@@ -760,6 +765,38 @@ class TaskRunStore:
[*ids, *ids]).fetchall()
return [_row(row) for row in rows]
+ def active_parents(self) -> list[TaskRun]:
+ """自己已经结束、派出的后继还有在排或在跑的那几轮,新的在前。"""
+ if not self.enabled:
+ return []
+ marks = ",".join(f"'{name}'" for name in ACTIVE_STATUSES)
+ with self.database.read_connection() as connection:
+ rows = connection.execute(
+ f"SELECT {_COLUMNS} FROM task_run WHERE id IN (SELECT DISTINCT parent_run_id FROM task_run "
+ f"WHERE status IN ({marks}) AND parent_run_id IS NOT NULL) AND status NOT IN ({marks}) "
+ "ORDER BY id DESC").fetchall()
+ return [_row(row) for row in rows]
+
+ def followup_counts(self, parent_ids) -> dict[int, dict[str, int]]:
+ """这几轮直接派出的后继按状态各有几条。
+
+ 活动页只取得到最近几十条后继,一轮派出上千条时,父任务还算不算在进行、
+ 做到第几条,要靠这份计数回答,不能数页面上那几行。
+ """
+ ids = sorted({int(run_id) for run_id in parent_ids})
+ if not self.enabled or not ids:
+ return {}
+ counts: dict[int, dict[str, int]] = {}
+ with self.database.read_connection() as connection:
+ for offset in range(0, len(ids), 900):
+ batch = ids[offset:offset + 900]
+ for parent, status, count in connection.execute(
+ f"SELECT parent_run_id, status, count(*) FROM task_run "
+ f"WHERE parent_run_id IN ({','.join('?' * len(batch))}) GROUP BY parent_run_id, status",
+ batch):
+ counts.setdefault(int(parent), {})[str(status)] = int(count)
+ return counts
+
# -- 调用方的便利入口 --------------------------------------------------
@contextmanager
diff --git a/src/peach/upgrade_repairs.py b/src/peach/upgrade_repairs.py
index d220fa9ea..de93ec47c 100644
--- a/src/peach/upgrade_repairs.py
+++ b/src/peach/upgrade_repairs.py
@@ -35,6 +35,11 @@ def _read(path: Path) -> dict:
return value
+def _title(row) -> str:
+ """任务中心里这一条写哪部片:有番号写番号,没有就写文件名。"""
+ return str(row['code'] or row['name'] or f'影片 {row["id"]}')
+
+
def _counts(connection) -> dict:
return {table: connection.execute(f'SELECT count(*) FROM {table}').fetchone()[0]
for table in ('asset', 'entity', 'asset_tag')}
@@ -273,7 +278,7 @@ class UpgradeRepairs:
args.append(self.contract.avatar_root)
args.append(attempts)
planned.extend(planner.stock(*args, limit=min(16, remaining), skip=seen))
- rows = connection.execute("SELECT id,path,location,snapshot_path FROM asset WHERE medium='video' "
+ rows = connection.execute("SELECT id,code,name,path,location,snapshot_path FROM asset WHERE medium='video' "
"AND disposal IS NULL ORDER BY id").fetchall()
from .followups import Followup
selected = set(receipt.get('media_ids', []))
@@ -292,7 +297,7 @@ class UpgradeRepairs:
if cost_for(row['location'], row['path'], config) != 'free':
deferred.append(row['id'])
continue
- planned.append(Followup(key, MEDIA_TASK, '更新影片资料'))
+ planned.append(Followup(key, MEDIA_TASK, _title(row)))
if len(planned) >= MAX_FOLLOWUPS:
break
receipt['deferred'] = deferred
@@ -398,6 +403,9 @@ def _media_run(contract, key: str, handle) -> dict:
if previous:
previous.update(issue_count=0, paused_count=0, issue_preview=[], retryable_asset_ids=[])
service = getattr(contract, 'upgrade_repairs', None)
+ with contract.database.read_connection() as connection:
+ found = connection.execute('SELECT id,code,name FROM asset WHERE id=?', (asset_id,)).fetchone()
+ title = _title(found) if found else f'影片 {asset_id}'
def active():
return service is None or not service.stopping.is_set()
try:
@@ -406,14 +414,15 @@ def _media_run(contract, key: str, handle) -> dict:
recheck_existing=True, job_id=job_id,
state_file=state_file, resume_state=previous or None,
active=active,
- report=lambda state: handle.progress(label=state.get('stage', '更新影片资料')))
+ report=lambda state: handle.progress(
+ label=f"{title} · {state.get('stage') or '更新影片资料'}"))
except InterruptedError:
if not active():
raise JobPaused('服务重启后继续') from None
raise
if result.get('status') not in ('complete', 'succeeded') or result.get('issue_count'):
- raise RuntimeError(result.get('error') or '影片升级补全未完成,请在活动页查看后重试')
- return {'outcome': '影片升级补全完成', 'asset_id': asset_id}
+ raise RuntimeError(result.get('error') or '影片升级补全未完成,请在任务中心查看后重试')
+ return {'outcome': '影片升级补全完成', 'name': title, 'asset_id': asset_id}
TYPE = register(FollowupType(MEDIA_TASK, '更新影片资料', True, _media_run))
diff --git a/src/peach/web_tasks.py b/src/peach/web_tasks.py
index 6a1265d29..379ad3e94 100644
--- a/src/peach/web_tasks.py
+++ b/src/peach/web_tasks.py
@@ -54,13 +54,26 @@ def _tasks(contract, args):
"runs": [payload(row) for row in rows]}
finished, has_more = contract.task_runs.finished_page(before=before, limit=limit)
if before is not None:
+ counts = contract.task_runs.followup_counts(row.id for row in finished if not row.followup)
return {"ok": True, "filtered": False, "available": True,
"finished": [payload(row) for row in finished],
- "finished_has_more": has_more}
- running = contract.task_runs.query(status="active", limit=limit)
+ "finished_has_more": has_more,
+ "followup_counts": {str(parent): states for parent, states in counts.items()}}
+ # 正在跑的一条不落,排队的取最近几条:一轮派出上千条后继时,按 id 取最近的只剩排队那些,
+ # 真正在跑的那条反而被挤掉。
+ started = contract.task_runs.query(status="running", limit=500)
+ queued = contract.task_runs.query(status="pending", limit=limit)
+ running = sorted({row.id: row for row in (*started, *queued)}.values(), key=lambda row: row.id, reverse=True)
# 还在跑的那一轮已经跑完的后继:它们挂在「正在进行」那张卡下面,不在哪一页里。
finished += contract.task_runs.settled_followups(
[row.id for row in running if not row.followup])
+ # 后继还在排或在跑、自己已经结束的父任务:它可能比「最近完成」这一页还早,不单独下发的话,
+ # 它派出的后继在页面上找不到父卡,会一条条散开。它不进 `finished`,翻页游标才不会跳过中间那段。
+ shown = {row.id for row in (*running, *finished)}
+ working = [row for row in contract.task_runs.active_parents() if row.id not in shown]
+ # 后继只下发了一部分,父任务做到哪一步、还算不算在进行,按计数回答。
+ counts = contract.task_runs.followup_counts(
+ row.id for row in (*running, *finished, *working) if not row.followup)
return {
"ok": True,
"filtered": False,
@@ -73,6 +86,8 @@ def _tasks(contract, args):
if row.status == "cancelled" and "blocked_by" in row.result_summary],
"finished": [payload(row) for row in finished],
"finished_has_more": has_more,
+ "working_parents": [payload(row) for row in working],
+ "followup_counts": {str(parent): states for parent, states in counts.items()},
"statuses": {"active": list(ACTIVE_STATUSES), "finished": list(TERMINAL_STATUSES)},
}
diff --git a/tests/test_task_center_integration.py b/tests/test_task_center_integration.py
index 936ecd392..1bac80784 100644
--- a/tests/test_task_center_integration.py
+++ b/tests/test_task_center_integration.py
@@ -437,6 +437,41 @@ class TasksEndpointTests(unittest.TestCase):
self.assertEqual([row["id"] for row in payload["running"]], [parent.id])
self.assertEqual([row["id"] for row in payload["finished"]], queued)
+ def test_a_dispatch_with_dozens_of_followups_stays_one_working_parent_with_counts(self):
+ """自动修复每批派几十条;接口只下发最近几条排队的,但在跑的、父任务与总数都不能丢。"""
+ older = self.store.start("upgrade-repairs", trigger="scheduled", mutex_key="upgrade-repairs")
+ self.store.finish(older.id, "succeeded", summary={"outcome": "已安排 30 项处理"})
+ first = self.store.enqueue_followups(
+ older.id, [(f"upgrade-library:x:{n}", "upgrade-library", f"ABC-{n:03}") for n in range(30)])["queued"]
+ self.store.finish(first[0], "succeeded")
+ claimed = self.store.claim_followup(("upgrade-library",))
+ newer = [self._settled(f"2026-10-28T09:{minute:02}:00.000Z") for minute in range(4)]
+
+ payload = web_tasks.q_tasks(self.contract, {"limit": "3"})
+
+ # 在跑的那条下发了,哪怕比它新的排队项有二十多条;排队的只取最近三条。
+ running_ids = [row["id"] for row in payload["running"]]
+ self.assertIn(claimed.id, running_ids)
+ self.assertEqual(len(running_ids), 4)
+ # 父任务比「最近完成」这一页还早,也不进这一页,单独作为进行中的父任务下发。
+ self.assertNotIn(older.id, [row["id"] for row in payload["finished"]])
+ self.assertEqual([row["id"] for row in payload["working_parents"]], [older.id])
+ self.assertEqual(payload["followup_counts"][str(older.id)],
+ {"succeeded": 1, "running": 1, "pending": 28})
+ self.assertEqual([row["id"] for row in payload["finished"]], list(reversed(newer))[:3])
+
+ def test_a_parent_returns_to_recent_runs_once_its_followups_are_done(self):
+ parent = self.store.start("upgrade-repairs", trigger="scheduled")
+ self.store.finish(parent.id, "succeeded")
+ queued = self.store.enqueue_followups(
+ parent.id, [("upgrade-library:x:1", "upgrade-library", "ABC-001")])["queued"]
+ self.assertEqual(web_tasks.q_tasks(self.contract, {})["finished"], [])
+ self.store.finish(queued[0], "succeeded")
+ payload = web_tasks.q_tasks(self.contract, {})
+ self.assertEqual([row["id"] for row in payload["finished"]], [parent.id, *queued])
+ self.assertEqual(payload["working_parents"], [])
+ self.assertEqual(payload["followup_counts"][str(parent.id)], {"succeeded": 1})
+
def test_a_short_history_says_there_is_nothing_earlier(self):
self._settled("2026-09-11T10:00:00.000Z")
self.assertFalse(web_tasks.q_tasks(self.contract, {})["finished_has_more"])
diff --git a/tests/test_upgrade_repairs.py b/tests/test_upgrade_repairs.py
index bda03561c..d8e99d5da 100644
--- a/tests/test_upgrade_repairs.py
+++ b/tests/test_upgrade_repairs.py
@@ -190,6 +190,10 @@ class UpgradeRepairTests(unittest.TestCase):
'peach.seed_followup.plan',return_value=[]), patch('peach.media_sources.cost_for',return_value='free'):
planned=self.service._plan(receipt)
self.assertEqual([row.key for row in planned],['upgrade-library:processing-v2:text-3:1'])
+ # 任务中心那一行写的是哪部片,不是一句笼统的「更新影片资料」。
+ with self.service.contract.database.read_connection() as connection:
+ code, name = connection.execute('SELECT code,name FROM asset WHERE id=1').fetchone()
+ self.assertEqual(planned[0].label, code or name)
def test_text_only_dispatch_does_not_plan_unrelated_people(self):
from peach.settings_file import load_config