feat(scheduler): every background run is a durable row
What a job last did lived in the API process's memory and was gone at every restart; the audit log kept a run only when it was manual, failed or changed something. Now each run is a job_runs row written at its start and its end in its own session: who started it, its outcome as data, what it spent of the YouTube quota (quota_events.job_run_id) and the lines it logged (log_events.run_id, filterable in the log console). - a run a stopped process left open is marked interrupted at startup - "run now" is a one-shot job on the scheduler, which now always starts (interval jobs only with SCHEDULER_ENABLED); the manual actor is reset after the run, since pool threads are reused - the dashboard's last run comes from job_runs, so it survives a deploy - runs leave the audit log: migration 0109 moves the old job_run rows over (and back on downgrade); the audit page's run folding goes - job_run_retention_days (default 30) prunes old runs in audit_gc
This commit is contained in:
@@ -1,72 +0,0 @@
|
||||
// The audit log's first screen was one job's "done" ten times over. A job's successful runs fold
|
||||
// into one row whatever ran between them (the owner: a fold broken by every other job read as the same
|
||||
// row five times); only that job's own failure or skip breaks it.
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { AuditRow } from "./api";
|
||||
import { foldJobRuns } from "./auditGroups";
|
||||
|
||||
let nextId = 1;
|
||||
const run = (job: string, status = "ok"): AuditRow => ({
|
||||
id: nextId++,
|
||||
created_at: null,
|
||||
actor: null,
|
||||
action: "job_run",
|
||||
target_type: "job",
|
||||
target_id: job,
|
||||
summary: null,
|
||||
before: null,
|
||||
after: { status },
|
||||
});
|
||||
const admin = (): AuditRow => ({ ...run("x"), action: "config_set", target_type: "config" });
|
||||
|
||||
const shape = (rows: ReturnType<typeof foldJobRuns>) =>
|
||||
rows.map((r) => `${r.target_id}:${String(r.after?.status)}×${r.runs?.length ?? 1}`);
|
||||
|
||||
describe("folding the job runs", () => {
|
||||
it("folds a job's successful runs into one row, newest first", () => {
|
||||
const rows = [run("enrich"), run("enrich"), run("enrich"), run("people")];
|
||||
const out = foldJobRuns(rows);
|
||||
expect(shape(out)).toEqual(["enrich:ok×3", "people:ok×1"]);
|
||||
expect(out[0]!.id).toBe(rows[0]!.id);
|
||||
expect(out[0]!.runs!.map((r) => r.id)).toEqual(rows.slice(0, 3).map((r) => r.id));
|
||||
});
|
||||
|
||||
it("never folds a failed or skipped run, and lets it break the run", () => {
|
||||
const rows = [run("rss"), run("rss", "error"), run("rss", "error"), run("rss"), run("rss")];
|
||||
expect(shape(foldJobRuns(rows))).toEqual([
|
||||
"rss:ok×1",
|
||||
"rss:error×1",
|
||||
"rss:error×1",
|
||||
"rss:ok×2",
|
||||
]);
|
||||
expect(shape(foldJobRuns([run("rss", "skipped"), run("rss", "skipped")]))).toEqual([
|
||||
"rss:skipped×1",
|
||||
"rss:skipped×1",
|
||||
]);
|
||||
});
|
||||
|
||||
it("folds across other jobs and an admin's own action, each at its own place", () => {
|
||||
const rows = [run("enrich"), run("plex"), run("enrich"), admin(), run("shorts"), run("enrich")];
|
||||
rows.push(run("plex"));
|
||||
const out = foldJobRuns(rows);
|
||||
expect(shape(out)).toEqual(["enrich:ok×3", "plex:ok×2", "x:ok×1", "shorts:ok×1"]);
|
||||
expect(out[0]!.runs!.map((r) => r.id)).toEqual([rows[0]!.id, rows[2]!.id, rows[5]!.id]);
|
||||
});
|
||||
|
||||
it("starts a new fold below the job's own failure, which another job's does not", () => {
|
||||
const rows = [run("enrich"), run("enrich", "error"), run("enrich"), run("rss", "error")];
|
||||
rows.push(run("enrich"));
|
||||
expect(shape(foldJobRuns(rows))).toEqual([
|
||||
"enrich:ok×1",
|
||||
"enrich:error×1",
|
||||
"enrich:ok×2",
|
||||
"rss:error×1",
|
||||
]);
|
||||
});
|
||||
|
||||
it("leaves the rows it was given as they were", () => {
|
||||
const rows = [run("enrich"), run("enrich")];
|
||||
foldJobRuns(rows);
|
||||
expect("runs" in rows[0]!).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,48 +0,0 @@
|
||||
import type { AuditRow } from "./api";
|
||||
|
||||
// The audit log's scheduler noise, folded (the owner's pick): the background jobs write a row per
|
||||
// run that changed something, so an hour of "enrichment done" filled the first screen and pushed the
|
||||
// admins' own actions down. A job's SUCCESSFUL runs become one row that names how many there were and
|
||||
// opens to each — whatever ran between them (another job, an admin's action): the owner read a fold
|
||||
// that broke at every other job as the same row five times. Only that job's own failed or skipped run
|
||||
// breaks it: that row stays on its own, at its moment, where it is seen, and the job's older successes
|
||||
// start a new fold below it. Rows arrive newest first; a fold stands at its newest run's place.
|
||||
|
||||
/** An audit row as the table lists it: a lone row, or the newest of a job's folded runs (`runs`, the
|
||||
* whole fold newest first, the row itself included). */
|
||||
export type AuditListRow = AuditRow & { runs?: AuditRow[] };
|
||||
|
||||
/** The job a row is a run of, or null for any other row. */
|
||||
function jobOf(row: AuditRow): string | null {
|
||||
return row.action === "job_run" && row.target_type === "job" && row.target_id
|
||||
? row.target_id
|
||||
: null;
|
||||
}
|
||||
|
||||
export function foldJobRuns(rows: readonly AuditRow[]): AuditListRow[] {
|
||||
const out: AuditListRow[] = [];
|
||||
// Each job's open fold: the row its next older success joins.
|
||||
const open = new Map<string, AuditListRow>();
|
||||
for (const row of rows) {
|
||||
const job = jobOf(row);
|
||||
if (job === null) {
|
||||
out.push(row);
|
||||
continue;
|
||||
}
|
||||
if (row.after?.status !== "ok") {
|
||||
// A failure or a skip stands alone and closes the job's fold.
|
||||
open.delete(job);
|
||||
out.push(row);
|
||||
continue;
|
||||
}
|
||||
const head = open.get(job);
|
||||
if (head) {
|
||||
head.runs = [...(head.runs ?? [head]), row];
|
||||
continue;
|
||||
}
|
||||
const fresh: AuditListRow = { ...row };
|
||||
open.set(job, fresh);
|
||||
out.push(fresh);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
Reference in New Issue
Block a user