import type { JobGraph } from "../config/job-graph.ts"; import type { GateDecision } from "./environment-gate.ts"; import { assertMove, blocksDependents, InvalidJobTransition, isTerminal, runStateOf, type JobState, type RunState } from "./states.ts"; export interface JobRecord { readonly name: string; readonly state: JobState; readonly reason: string | null; } /** The environment gate for a job, or null when the job names no environment. */ export type Gate = (job: string) => GateDecision | null; export interface Advance { readonly start: readonly { readonly job: string; readonly waitSeconds: number }[]; readonly awaitingApproval: readonly { readonly job: string; readonly reason: string }[]; readonly skipped: readonly string[]; readonly refused: readonly { readonly job: string; readonly reason: string }[]; } export type Outcome = "succeeded" | "failed" | "timed_out"; export class UnknownJob extends Error { readonly job: string; constructor(job: string) { super(`The run has no job named "${job}".`); this.job = job; } } /** * The jobs of one run and the rules that move them. It does no I/O: RunHub loads it, calls one method, and saves * `records()`. Reports that arrive after a job finished, such as an exit after a cancel, are ignored, not errors. */ export class Run { readonly #graph: JobGraph; readonly #jobs: Map; constructor(graph: JobGraph, records: readonly JobRecord[] = []) { this.#graph = graph; const saved = new Map(records.map((record) => [record.name, record])); this.#jobs = new Map(graph.names.map((name) => [name, saved.get(name) ?? { name, state: "pending", reason: null }])); } get state(): RunState { return runStateOf([...this.#jobs.values()].map((job) => job.state)); } records(): readonly JobRecord[] { return [...this.#jobs.values()]; } job(name: string): JobRecord { const job = this.#jobs.get(name); if (!job) throw new UnknownJob(name); return job; } /** Moves every pending job whose needs are settled: skip, refuse, wait for approval, or queue to start. Repeats until nothing moves, so a skip reaches every job after it. */ advance(gate: Gate): Advance { const start: { job: string; waitSeconds: number }[] = []; const awaitingApproval: { job: string; reason: string }[] = []; const skipped: string[] = []; const refused: { job: string; reason: string }[] = []; for (let moved = true; moved; ) { moved = false; for (const job of this.#jobs.values()) { if (job.state !== "pending") continue; const needs = this.#graph.needsOf(job.name).map((need) => this.job(need).state); if (needs.some(blocksDependents)) { this.#move(job.name, "skipped", "A job it needs did not succeed."); skipped.push(job.name); moved = true; } else if (needs.every((state) => state === "succeeded")) { const decision = gate(job.name); if (!decision || decision.kind === "open") { this.#move(job.name, "queued", null); start.push({ job: job.name, waitSeconds: decision?.waitSeconds ?? 0 }); } else if (decision.kind === "needs_approval") { this.#move(job.name, "waiting_approval", decision.reason); awaitingApproval.push({ job: job.name, reason: decision.reason }); } else { this.#move(job.name, "failed", decision.reason); refused.push({ job: job.name, reason: decision.reason }); } moved = true; } } } return { start, awaitingApproval, skipped, refused }; } approve(job: string, approver: string): void { this.#decide(job, "queued", `${approver} approved it.`); } reject(job: string, rejecter: string): void { this.#decide(job, "cancelled", `${rejecter} rejected it.`); } #decide(job: string, to: JobState, reason: string): void { const from = this.job(job).state; if (from !== "waiting_approval") throw new InvalidJobTransition(job, from, to); this.#move(job, to, reason); } waitForImage(job: string): void { this.#move(job, "waiting_image", null); } imageReady(job: string): void { if (this.job(job).state === "waiting_image") this.#move(job, "pending", null); } imageFailed(job: string, reason: string): void { if (this.job(job).state === "waiting_image") this.#move(job, "failed", reason); } started(job: string): "applied" | "ignored" { if (isTerminal(this.job(job).state)) return "ignored"; this.#move(job, "running", null); return "applied"; } failedToStart(job: string, reason: string): "applied" | "ignored" { if (isTerminal(this.job(job).state)) return "ignored"; this.#move(job, "failed", reason); return "applied"; } finished(job: string, outcome: Outcome, reason: string | null = null): "applied" | "ignored" { if (isTerminal(this.job(job).state)) return "ignored"; this.#move(job, outcome, reason); return "applied"; } /** Cancels every job that has not finished. Returns the jobs that had a container, so the caller can stop them. */ cancel(reason: string): readonly string[] { const live: string[] = []; for (const job of this.#jobs.values()) { if (isTerminal(job.state)) continue; if (job.state === "running" || job.state === "queued") live.push(job.name); this.#move(job.name, "cancelled", reason); } return live; } #move(name: string, to: JobState, reason: string | null): void { const job = this.job(name); assertMove(name, job.state, to); this.#jobs.set(name, { name, state: to, reason }); } }