jobs: show target path in list rows; delete queued/finished jobs + clear-all history

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-23 16:31:52 +00:00
co-authored by Claude Opus 4.8
parent 5da29e4004
commit 32caa67cab
5 changed files with 95 additions and 24 deletions
+21 -1
View File
@@ -8,13 +8,16 @@ import {
getPipelineJobsForUser,
getOldestPendingJob,
countPendingJobs,
deletePipelineJob,
deleteTerminalJobsForUser,
markInterruptedJobs,
getUserById,
} from 'officerdb';
import { unlink } from 'node:fs/promises';
import { toShellUsername } from '../../data-path';
import { executePipeline } from './pipeline-executor';
import type { OutMessage } from './pipeline-executor';
import { executeScript, type ScriptEvent } from './execute-script';
import { executeScript, jobLogPath, type ScriptEvent } from './execute-script';
// Everything a job can stream — pipeline structural events plus script stdout/stderr/exit.
type JobEvent = OutMessage | ScriptEvent;
@@ -318,6 +321,23 @@ export async function getJobsForUser(userId: number) {
}));
}
// Delete a job (row + log). Refuses a running job — it must be stopped first.
export async function deleteJob(jobId: string): Promise<'deleted' | 'running' | 'noop'> {
if (liveJobs.has(jobId)) return 'running';
const job = await getPipelineJob(jobId);
if (!job) return 'noop';
await deletePipelineJob(jobId);
await unlink(jobLogPath(jobId)).catch(() => {});
return 'deleted';
}
// Clear all finished jobs (rows + logs). Running/queued jobs are untouched.
export async function clearHistory(userId: number): Promise<number> {
const ids = await deleteTerminalJobsForUser(userId);
await Promise.all(ids.map((id) => unlink(jobLogPath(id)).catch(() => {})));
return ids.length;
}
// Lightweight header-badge summary: how many of the user's jobs are running / queued, and which one
// is running (for the "running" badge's link).
export async function getCounts(userId: number): Promise<{ running: number; runningJobId: string | null; queued: number }> {
@@ -24,6 +24,8 @@ pipelineJobsRouter.get('/', async (c) => {
status: j.status,
isLive: j.isLive,
exitCode: j.exitCode,
// The file/folder the job is working on (for the list rows).
target: (j.inputs as Record<string, unknown> | null)?.file_path ?? j.cwd ?? null,
totalCost: j.totalCost,
createdAt: j.createdAt,
startedAt: j.startedAt,
@@ -107,6 +109,23 @@ pipelineJobsRouter.post('/:id/stop', async (c) => {
return c.json({ ok: true, result });
});
// DELETE /history — clear all finished jobs (rows + logs). Before /:id so it isn't captured as an id.
pipelineJobsRouter.delete('/history', async (c) => {
const user = c.get('user');
const cleared = await jobManager.clearHistory(user.id);
return c.json({ ok: true, cleared });
});
// DELETE /:id — delete a job (queued or finished). Running jobs must be stopped first (409).
pipelineJobsRouter.delete('/:id', async (c) => {
const user = c.get('user');
const job = await getPipelineJob(c.req.param('id'));
if (!job || job.userId !== user.id) return c.json({ error: 'Not found' }, 404);
const result = await jobManager.deleteJob(job.id);
if (result === 'running') return c.json({ error: 'Stop the job before deleting' }, 409);
return c.json({ ok: true, result });
});
// GET /:id — single job detail (full row).
pipelineJobsRouter.get('/:id', async (c) => {
const user = c.get('user');