154 lines · 5.5 KBRaw
| 1 | import type { JobGraph } from "../config/job-graph.ts"; |
| 2 | import type { GateDecision } from "./environment-gate.ts"; |
| 3 | import { assertMove, blocksDependents, InvalidJobTransition, isTerminal, runStateOf, type JobState, type RunState } from "./states.ts"; |
| 4 | |
| 5 | export interface JobRecord { |
| 6 | readonly name: string; |
| 7 | readonly state: JobState; |
| 8 | readonly reason: string | null; |
| 9 | } |
| 10 | |
| 11 | /** The environment gate for a job, or null when the job names no environment. */ |
| 12 | export type Gate = (job: string) => GateDecision | null; |
| 13 | |
| 14 | export interface Advance { |
| 15 | readonly start: readonly { readonly job: string; readonly waitSeconds: number }[]; |
| 16 | readonly awaitingApproval: readonly { readonly job: string; readonly reason: string }[]; |
| 17 | readonly skipped: readonly string[]; |
| 18 | readonly refused: readonly { readonly job: string; readonly reason: string }[]; |
| 19 | } |
| 20 | |
| 21 | export type Outcome = "succeeded" | "failed" | "timed_out"; |
| 22 | |
| 23 | export class UnknownJob extends Error { |
| 24 | readonly job: string; |
| 25 | |
| 26 | constructor(job: string) { |
| 27 | super(`The run has no job named "${job}".`); |
| 28 | this.job = job; |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * The jobs of one run and the rules that move them. It does no I/O: RunHub loads it, calls one method, and saves |
| 34 | * `records()`. Reports that arrive after a job finished, such as an exit after a cancel, are ignored, not errors. |
| 35 | */ |
| 36 | export class Run { |
| 37 | readonly #graph: JobGraph; |
| 38 | readonly #jobs: Map<string, JobRecord>; |
| 39 | |
| 40 | constructor(graph: JobGraph, records: readonly JobRecord[] = []) { |
| 41 | this.#graph = graph; |
| 42 | const saved = new Map(records.map((record) => [record.name, record])); |
| 43 | this.#jobs = new Map(graph.names.map((name) => [name, saved.get(name) ?? { name, state: "pending", reason: null }])); |
| 44 | } |
| 45 | |
| 46 | get state(): RunState { |
| 47 | return runStateOf([...this.#jobs.values()].map((job) => job.state)); |
| 48 | } |
| 49 | |
| 50 | records(): readonly JobRecord[] { |
| 51 | return [...this.#jobs.values()]; |
| 52 | } |
| 53 | |
| 54 | job(name: string): JobRecord { |
| 55 | const job = this.#jobs.get(name); |
| 56 | if (!job) throw new UnknownJob(name); |
| 57 | return job; |
| 58 | } |
| 59 | |
| 60 | /** 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. */ |
| 61 | advance(gate: Gate): Advance { |
| 62 | const start: { job: string; waitSeconds: number }[] = []; |
| 63 | const awaitingApproval: { job: string; reason: string }[] = []; |
| 64 | const skipped: string[] = []; |
| 65 | const refused: { job: string; reason: string }[] = []; |
| 66 | for (let moved = true; moved; ) { |
| 67 | moved = false; |
| 68 | for (const job of this.#jobs.values()) { |
| 69 | if (job.state !== "pending") continue; |
| 70 | const needs = this.#graph.needsOf(job.name).map((need) => this.job(need).state); |
| 71 | if (needs.some(blocksDependents)) { |
| 72 | this.#move(job.name, "skipped", "A job it needs did not succeed."); |
| 73 | skipped.push(job.name); |
| 74 | moved = true; |
| 75 | } else if (needs.every((state) => state === "succeeded")) { |
| 76 | const decision = gate(job.name); |
| 77 | if (!decision || decision.kind === "open") { |
| 78 | this.#move(job.name, "queued", null); |
| 79 | start.push({ job: job.name, waitSeconds: decision?.waitSeconds ?? 0 }); |
| 80 | } else if (decision.kind === "needs_approval") { |
| 81 | this.#move(job.name, "waiting_approval", decision.reason); |
| 82 | awaitingApproval.push({ job: job.name, reason: decision.reason }); |
| 83 | } else { |
| 84 | this.#move(job.name, "failed", decision.reason); |
| 85 | refused.push({ job: job.name, reason: decision.reason }); |
| 86 | } |
| 87 | moved = true; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | return { start, awaitingApproval, skipped, refused }; |
| 92 | } |
| 93 | |
| 94 | approve(job: string, approver: string): void { |
| 95 | this.#decide(job, "queued", `${approver} approved it.`); |
| 96 | } |
| 97 | |
| 98 | reject(job: string, rejecter: string): void { |
| 99 | this.#decide(job, "cancelled", `${rejecter} rejected it.`); |
| 100 | } |
| 101 | |
| 102 | #decide(job: string, to: JobState, reason: string): void { |
| 103 | const from = this.job(job).state; |
| 104 | if (from !== "waiting_approval") throw new InvalidJobTransition(job, from, to); |
| 105 | this.#move(job, to, reason); |
| 106 | } |
| 107 | |
| 108 | waitForImage(job: string): void { |
| 109 | this.#move(job, "waiting_image", null); |
| 110 | } |
| 111 | |
| 112 | imageReady(job: string): void { |
| 113 | if (this.job(job).state === "waiting_image") this.#move(job, "pending", null); |
| 114 | } |
| 115 | |
| 116 | imageFailed(job: string, reason: string): void { |
| 117 | if (this.job(job).state === "waiting_image") this.#move(job, "failed", reason); |
| 118 | } |
| 119 | |
| 120 | started(job: string): "applied" | "ignored" { |
| 121 | if (isTerminal(this.job(job).state)) return "ignored"; |
| 122 | this.#move(job, "running", null); |
| 123 | return "applied"; |
| 124 | } |
| 125 | |
| 126 | failedToStart(job: string, reason: string): "applied" | "ignored" { |
| 127 | if (isTerminal(this.job(job).state)) return "ignored"; |
| 128 | this.#move(job, "failed", reason); |
| 129 | return "applied"; |
| 130 | } |
| 131 | |
| 132 | finished(job: string, outcome: Outcome, reason: string | null = null): "applied" | "ignored" { |
| 133 | if (isTerminal(this.job(job).state)) return "ignored"; |
| 134 | this.#move(job, outcome, reason); |
| 135 | return "applied"; |
| 136 | } |
| 137 | |
| 138 | /** Cancels every job that has not finished. Returns the jobs that had a container, so the caller can stop them. */ |
| 139 | cancel(reason: string): readonly string[] { |
| 140 | const live: string[] = []; |
| 141 | for (const job of this.#jobs.values()) { |
| 142 | if (isTerminal(job.state)) continue; |
| 143 | if (job.state === "running" || job.state === "queued") live.push(job.name); |
| 144 | this.#move(job.name, "cancelled", reason); |
| 145 | } |
| 146 | return live; |
| 147 | } |
| 148 | |
| 149 | #move(name: string, to: JobState, reason: string | null): void { |
| 150 | const job = this.job(name); |
| 151 | assertMove(name, job.state, to); |
| 152 | this.#jobs.set(name, { name, state: to, reason }); |
| 153 | } |
| 154 | } |