Recipes
How-to guides for tasks that come up when you write a pipeline as jobs. For every export, see the API reference; for why job behaves as it does, see the concepts.
How to need a job only when another succeeded
This guide shows you how to pull a job into the graph only when an earlier outcome calls for it: here, signing a release only when its version is stable.
- Declare the need the decision depends on with
p.needs, and keep it in the plan you return. - Build a guard on it:
succeeded(need),failed(need), orsucceededWith({ need, where, describe })to test the need's value. - Declare the conditional need with
p.needsIf({ job, when: guard }). Inrun, it is the job's value when the guard held, andundefinedwhen it didn't.
import { defineJobs, runJobs, Succeeded, succeededWith } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
const { job } = defineJobs((_meta: { readonly version: string }) => ({}));
const version = job({ name: "version", run: (r) => r.meta.version });
const sign = job({ name: "sign", run: () => "signature" });
const publish = job({
name: "publish",
plan: (p) => {
const v = p.needs(version);
const stable = succeededWith({
need: v,
where: (value) => !value.startsWith("0."),
describe: "a stable version",
});
return { version: v, signature: p.needsIf({ job: sign, when: stable }) };
},
run: (_r, { version, signature }) => `${version} ${signature ?? "unsigned"}`,
});
const stable = (
await runJobs({ jobs: { publish }, meta: { version: "1.4.0" } })
).unwrap();
assertEquals(
stable.outcomes.publish,
Succeeded("1.4.0 signature"),
"a stable version should pull sign in",
);
const early = (
await runJobs({ jobs: { publish }, meta: { version: "0.3.0" } })
).unwrap();
assertEquals(
early.outcomes.publish,
Succeeded("0.3.0 unsigned"),
"a 0.x version should leave the signature out",
);
assertEquals(
early.all.has("sign"),
false,
"a false guard's job should not be part of the run",
);When the guard is false, sign isn't part of the run at all. When it holds, sign counts like any other need: if it fails, publish is Blocked.
A guard may read only the unconditional needs of its own plan, which settle before any guard is tested. A need you made with p.needs but left out of the returned plan doesn't count, and neither does another needsIf. Reading one is a graph error, reported before anything runs:
import {
defineJobs,
runJobs,
succeeded,
UndeclaredGuardRead,
} from "@sekhmet/job";
import { assertEquals } from "@std/assert";
const { job } = defineJobs(() => ({}));
const version = job({ name: "version", run: () => "1.4.0" });
const sign = job({ name: "sign", run: () => "signature" });
const publish = job({
name: "publish",
plan: (p) => {
const v = p.needs(version);
// `v` isn't in the returned plan, so the guard can't read it
return { signature: p.needsIf({ job: sign, when: succeeded(v) }) };
},
run: () => {},
});
const result = await runJobs({ jobs: { publish } });
assertEquals(
result.unwrapErr(),
UndeclaredGuardRead({
job: "publish",
guard: "version succeeded",
read: "version",
}),
"a guard reading a need left out of the plan should be a graph error",
);To fix it, return v in the plan too, as the first example does.
The built-in guards are enough for most graphs. If you write your own, it is an object with a describe string for dry runs, the reads it looks at, and a test that receives a view of those needs' outcomes. Asking the view for a need outside reads panics.
How to clean up or notify after a failure
This guide shows you how to run a job when something upstream broke, or whatever happened.
- For a notifier, set
runIf: failure(). It runs only when some need is unhealthy:Failed, orBlockedby a failure. Otherwise it isSkipped. - For a cleanup, set
runIf: always(). - In
run, read each need as aJobEnd: under anyrunIfother than the default,rungets outcomes, not values. - Add
continueOnError: trueto a cleanup whose own failure shouldn't fail the run.
import { always, defineJobs, failure, runJobs, Succeeded } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
const { job } = defineJobs((_meta: { readonly log: string[] }) => ({}));
const build = job({
name: "build",
run: (): string => {
throw new Error("compile error");
},
});
const notify = job({
name: "notify",
plan: (p) => ({ build: p.needs(build) }),
runIf: failure(),
run: (r, { build }) => {
const error = build.error().map(String).unwrapOr("blocked");
r.meta.log.push(`paged: ${error}`);
},
});
const cleanup = job({
name: "cleanup",
plan: (p) => ({ build: p.needs(build) }),
runIf: always(),
continueOnError: true,
run: (r) => r.meta.log.push("removed dist"),
});
const log: string[] = [];
const report = (
await runJobs({ jobs: { notify, cleanup }, meta: { log } })
).unwrap();
assertEquals(
log,
["paged: Error: compile error", "removed dist"],
"the failure() job should page and the always() job clean up",
);
assertEquals(
report.outcomes.notify,
Succeeded(undefined),
"the notifier should succeed",
);Both conditions also decide what happens after a cancel. An always() job still runs, with r.signal already aborted, so keep a cleanup's work short or independent of the signal. A failure() job runs after a cancel only if a need is unhealthy.
How to clean up resources
This guide shows you how to release what a job opens (a lock, a connection, a temporary directory) however the job ends, and where a resource the whole run shares belongs.
- In
run, register what to release onr.cleanup:defer(fn)for a callback,use(resource)for a value withSymbol.asyncDisposeorSymbol.dispose, andadopt(value, fn)for a value that isn't disposable itself. They mean whatAsyncDisposableStack's methods of the same names mean. - For a resource every job's context holds, open it in the context factory and register it on the factory's
cleanup. Each job that starts gets its own, and the factory's registrations are released last. - Put a resource the whole run shares, such as a pool, in
meta. It is yours, not the runner's: release it aroundrunJobs, for example withawait using. - Return data, never a resource you registered: dependents receive the return value after the job's cleanup has run.
import { defineJobs, runJobs, Succeeded } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
interface Connection extends Disposable {
readonly query: (sql: string) => string;
}
interface Pool extends AsyncDisposable {
readonly connect: (name: string) => Connection;
}
const log: string[] = [];
/** A stand-in for a database pool that logs what happens to it. */
function openPool(): Pool {
log.push("pool opened");
return {
connect: (name) => {
log.push(`${name} connected`);
return {
query: (sql) => `${name} ran ${sql}`,
[Symbol.dispose]: () => {
log.push(`${name} disconnected`);
},
};
},
[Symbol.asyncDispose]: async () => {
log.push("pool closed");
},
};
}
const { job } = defineJobs(
(meta: { readonly pool: Pool }, { job, cleanup }) => {
return { db: cleanup.use(meta.pool.connect(job)) };
},
);
const migrate = job({
name: "migrate",
run: (r) => {
log.push("migrate locked");
r.cleanup.defer(() => {
log.push("migrate unlocked");
});
return r.ctx.db.query("migrations");
},
});
{
await using pool = openPool();
const report = (
await runJobs({ jobs: { migrate }, meta: { pool } })
).unwrap();
assertEquals(
report.outcomes.migrate,
Succeeded("migrate ran migrations"),
"the job should succeed",
);
}
assertEquals(
log,
[
"pool opened",
"migrate connected",
"migrate locked",
"migrate unlocked",
"migrate disconnected",
"pool closed",
],
"the pool should outlive the job, and the job's cleanup run in reverse",
);The job's cleanup runs once run settles, whether it succeeded, failed or was cancelled, and it finishes before the job's terminal event and before any job that needs it starts. A cleanup that throws makes the job Failed. If the body threw as well, the outcome's error is a SuppressedError whose error is the cleanup's and whose suppressed is the body's, and several cleanup throws chain the same way. On a runtime without SuppressedError, the error is a plain Error of the same shape and name.
There is no timeout on cleanup yet: a cleanup that never settles holds its job, and the run, open.
How to write your own runIf
This guide shows you how to combine the default rule with a check of your own: here, a coverage report that runs only when the needs succeeded and the run's metadata asks for it.
- Pass
runIfa function of{ meta, cancelled, needs }.needsis your plan with each need replaced by itsJobEnd. There is no context yet: a job's context is built only once it is known to run. - Rebuild the default rule with
all(needs).isSucceeded(), and add!cancelledunless the job should run after a cancel: a hand-writtenrunIfdecides that too. - In
run, read each need as an outcome, as with anyrunIfbut the default.
import { all, defineJobs, runJobs, Skipped, Succeeded } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
const { job } = defineJobs((_meta: { readonly reportsCoverage: boolean }) => {
return {};
});
const test = job({ name: "test", run: () => 0.92 });
const coverage = job({
name: "coverage",
plan: (p) => ({ test: p.needs(test) }),
runIf: ({ meta, cancelled, needs }) => {
return !cancelled && meta.reportsCoverage && all(needs).isSucceeded();
},
run: (_r, { test }) => `${test.value().unwrapOr(0) * 100}%`,
});
const ci = (
await runJobs({ jobs: { coverage }, meta: { reportsCoverage: true } })
).unwrap();
assertEquals(
ci.outcomes.coverage,
Succeeded("92%"),
"in CI, the coverage job should run",
);
const local = (
await runJobs({ jobs: { coverage }, meta: { reportsCoverage: false } })
).unwrap();
assertEquals(
local.outcomes.coverage,
Skipped(),
"locally, the coverage job should be skipped",
);A hand-written runIf that returns false makes the job Skipped, whatever its needs did. Only the default success() turns a failed need into Blocked.
How to generate jobs from data
This guide shows you how to make one job per item, when the items come from data: one publish job per package, each needing its dependencies' publish jobs.
- Call
family({ key, define }).definebuilds the job for an item, andkeynames it. The same key gives the same job, however many times you ask, so the run runs it once. - Give each job a name that differs per key.
- When the family needs itself, annotate it with its type. TypeScript can't infer a value that refers to itself.
- Collect the jobs' needs in an array; needs resolve wherever they sit in plain arrays and records.
import { defineJobs, type Job, planJobs } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
interface Package {
readonly name: string;
readonly deps: readonly Package[];
}
const primordials: Package = { name: "primordials", deps: [] };
const enumPkg: Package = { name: "enum", deps: [primordials] };
const panic: Package = { name: "panic", deps: [primordials] };
const schema: Package = { name: "schema", deps: [enumPkg, panic] };
const { job, family } = defineJobs(() => ({}));
const publish: (pkg: Package) => Job<string, unknown> = family({
key: (pkg: Package) => pkg.name,
define: (pkg) => {
return job({
name: `publish ${pkg.name}`,
plan: (p) => ({ deps: pkg.deps.map((dep) => p.needs(publish(dep))) }),
run: () => `${pkg.name}@1.0.0`,
});
},
});
const graph = planJobs({ jobs: { schema: publish(schema) } }).unwrap();
assertEquals(
[...graph.jobs.keys()],
["publish schema", "publish enum", "publish primordials", "publish panic"],
"each package's publish should appear once, primordials included",
);primordials is needed by both enum and panic, but the family gives the same job for its key, so it appears and runs once.
A matrix is the same tool over a list of cases:
import { always, defineJobs, runJobs, Succeeded } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
const RUNTIMES = ["node", "bun", "deno"] as const;
type Runtime = (typeof RUNTIMES)[number];
const { job, family } = defineJobs((_meta: { readonly broken: Runtime }) => {
return {};
});
const suite = family({
key: (runtime: Runtime) => runtime,
define: (runtime) => {
return job({
name: `test (${runtime})`,
run: (r) => {
if (r.meta.broken === runtime) throw new Error("tests failed");
},
});
},
});
const summary = job({
name: "summary",
plan: (p) => ({
results: RUNTIMES.map((runtime) => ({
runtime,
outcome: p.needs(suite(runtime)),
})),
}),
runIf: always(),
run: (_r, { results }) => {
return results.map(({ runtime, outcome }) => {
return `${outcome.isSucceeded() ? "pass" : "FAIL"} ${runtime}`;
});
},
});
const meta: { readonly broken: Runtime } = { broken: "bun" };
const report = (await runJobs({ jobs: { summary }, meta })).unwrap();
assertEquals(
report.outcomes.summary,
Succeeded(["pass node", "FAIL bun", "pass deno"]),
"the summary should report every runtime, the failing one included",
);Two keys that build jobs with the same name are a DuplicateName error, and one key that builds two differently named jobs is a DuplicateKey error. Both are reported before anything runs.
How to preview a run
This guide shows you how to see the whole graph without running a job, for a --dry-run flag or a test of the graph's shape.
- Call
planJobs({ jobs, meta })with the roots and metadata you would pass torunJobs. It calls every plan, and no context factory orrun. - Read
graph.jobs, a map from each job's name to itsrunIf, itscontinueOnError, and its needs. A conditional need carries its guard's description inwhen.
import { always, defineJobs, planJobs, succeeded } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
const { job } = defineJobs(() => ({}));
let ran = false;
const build = job({
name: "build",
run: () => {
ran = true;
return "dist";
},
});
const smoke = job({ name: "smoke", run: () => {} });
const deploy = job({
name: "deploy",
plan: (p) => {
const built = p.needs(build);
return { built, smoke: p.needsIf({ job: smoke, when: succeeded(built) }) };
},
run: () => {},
});
const cleanup = job({
name: "cleanup",
plan: (p) => ({ deploy: p.needs(deploy) }),
runIf: always(),
run: () => {},
});
const graph = planJobs({ jobs: { cleanup } }).unwrap();
assertEquals(
[...graph.jobs.keys()],
["cleanup", "deploy", "build", "smoke"],
"a dry run should list every job, guarded ones included",
);
assertEquals(
graph.jobs.get("cleanup")?.runIf,
"always()",
"a dry run should show each job's runIf",
);
assertEquals(
graph.jobs.get("deploy")?.needs,
[
{ job: "build", when: undefined },
{ job: "smoke", when: "build succeeded" },
],
"a dry run should show each need with its guard",
);
assertEquals(ran, false, "a dry run should run no job");A dry run plans the targets of every needsIf, since it can't know yet which guards will hold, and it returns the same graph errors as a run would. A plan must be pure for this to be safe: it runs during the dry run too.
How to cancel a run
This guide shows you how to stop a run from outside, stop it at the first failure, and keep a failure from stopping it.
- Pass
signaltorunJobs. Aborting it abortsr.signalin every job. - In each body, hand
r.signalto whatever does the work (fetch, a subprocess, a timer). Cancellation is cooperative: a body that ignores its signal runs to the end. - A body that rejects with the signal's reason, or with an
AbortError, endsCancelled. Any other error isFailed, even after the abort.
import { always, Cancelled, defineJobs, runJobs } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
/** Waits `ms`, or rejects with the signal's reason when it aborts. */
function sleep({
ms,
signal,
}: {
readonly ms: number;
readonly signal: AbortSignal;
}): Promise<void> {
return new Promise((resolve, reject) => {
const timer = setTimeout(resolve, ms);
signal.addEventListener(
"abort",
() => {
clearTimeout(timer);
reject(signal.reason);
},
{ once: true },
);
});
}
const { job } = defineJobs(() => ({}));
const download = job({
name: "download",
run: (r) => sleep({ ms: 60_000, signal: r.signal }),
});
const cleanup = job({
name: "cleanup",
plan: (p) => ({ download: p.needs(download) }),
runIf: always(),
run: (r, { download }) => [download.isCancelled(), r.signal.aborted],
});
const controller = new AbortController();
setTimeout(() => controller.abort(new Error("interrupted")), 10);
const report = (
await runJobs({
jobs: { cleanup },
signal: controller.signal,
})
).unwrap();
assertEquals(
report.all.get("download"),
Cancelled(),
"the running download should be cancelled",
);
assertEquals(
report.outcomes.cleanup.value().unwrap(),
[true, true],
"the always() cleanup should run, seeing the cancel and an aborted signal",
);
assertEquals(report.passed, false, "a cancelled run should fail");To stop at the first failure, pass failFast: true. A job that fails then cancels the run, as if you had aborted the run's signal. A job with continueOnError: true is exempt: its failure doesn't cancel the run, and doesn't fail the run's verdict.
import { Cancelled, defineJobs, runJobs, Succeeded } from "@sekhmet/job";
import { assert, assertEquals } from "@std/assert";
const { job } = defineJobs(() => ({}));
const slow = job({
name: "slow",
run: (r) => {
return new Promise<string>((resolve, reject) => {
const timer = setTimeout(() => resolve("done"), 50);
r.signal.addEventListener("abort", () => {
clearTimeout(timer);
reject(r.signal.reason);
});
});
},
});
const lint = job({
name: "lint",
run: (): void => {
throw new Error("lint failed");
},
});
const flaky = job({
name: "flaky",
continueOnError: true,
run: (): void => {
throw new Error("flaky");
},
});
const strict = (
await runJobs({ jobs: { lint, slow }, failFast: true })
).unwrap();
assertEquals(
strict.outcomes.slow,
Cancelled(),
"with failFast, a failure should cancel the slow sibling",
);
const lenient = (
await runJobs({ jobs: { flaky, slow }, failFast: true })
).unwrap();
assertEquals(
lenient.outcomes.slow,
Succeeded("done"),
"a continueOnError failure should not trigger failFast",
);
assert(lenient.outcomes.flaky.isFailed(), "the flaky job should still fail");
assert(lenient.passed, "a continueOnError failure should keep the run passing");continueOnError changes the verdict, not the graph. A job that needs the flaky one is still Blocked when it fails, and that blocked job fails the verdict.
How to hand an outcome to Result code
This guide shows you how to pass a job's outcome to code that speaks Result, from @sekhmet/enum/result.
- Call
toResult()on the outcome. A success becomesOkwith its value; anything else becomesErrholding the outcome, so the reason survives. - To keep the outcome but treat a failure as data, call
catchFailure(). A success holdsOk(value), a failure becomes a success holdingErr(error), andFailedis gone from the type. TheResultit holds is a main one, read with methods.
import { match } from "@sekhmet/enum/core/mini";
import { Blocked, Failed, Succeeded } from "@sekhmet/job/end";
import { assert, assertEquals } from "@std/assert";
assertEquals(
Succeeded("dist").toResult().unwrap(),
"dist",
"a success should become Ok of its value",
);
const blocked = Blocked<string>(["lint"]).toResult();
assert(blocked.isErr(), "a blocked job should become Err");
assertEquals(
blocked.unwrapErr().blockedBy().unwrap(),
["lint"],
"the Err should hold the outcome, naming what blocked it",
);
const caught = Failed<string>(new Error("boom")).catchFailure();
const line = match(caught)({
Succeeded: (result) => (result.isErr() ? "failed, caught" : "built"),
Skipped: () => "skipped",
Blocked: () => "blocked",
Cancelled: () => "cancelled",
});
assertEquals(
line,
"failed, caught",
"a caught failure should be a success holding Err",
);The outcome constructors take the success type when it can't be inferred, as in Blocked<string>(["lint"]), the way enum's None<T>() does.
How to report progress from inside a job
This guide shows you how to show the parts of a long job as they happen, for a CLI that prints a line per step.
- Wrap each part of the body in
r.step({ name, run }). It returns whatrunreturns. - Pass
onEventtorunJobs. It receives aJobEventas each job and step starts and ends. - Render each event with enum's
match. KeeponEventfrom throwing: a throw cancels the run, andrunJobsrejects with it once every job has settled.
import { match } from "@sekhmet/enum/core/mini";
import { defineJobs, type JobEvent, runJobs } from "@sekhmet/job";
import { assertEquals } from "@std/assert";
const { job } = defineJobs(() => ({}));
const build = job({
name: "build",
run: async (r) => {
await r.step({ name: "compile", run: () => {} });
return await r.step({ name: "bundle", run: () => "dist" });
},
});
const lines: string[] = [];
const render = (event: JobEvent): string => {
return match(event)({
Started: ({ job }) => `▶ ${job}`,
StepStarted: ({ job, step }) => ` ▶ ${job} / ${step}`,
StepSucceeded: ({ job, step }) => ` ✓ ${job} / ${step}`,
StepFailed: ({ job, step }) => ` ✗ ${job} / ${step}`,
Finished: ({ job, outcome }) => {
return `${outcome.isSucceeded() ? "✓" : "✗"} ${job}`;
},
Skipped: ({ job }) => `- ${job}`,
Blocked: ({ job, blockedBy }) => `✗ ${job} (blocked by ${blockedBy})`,
Cancelled: ({ job }) => `✗ ${job} (cancelled)`,
});
};
(
await runJobs({
jobs: { build },
onEvent: (event) => lines.push(render(event)),
})
).unwrap();
assertEquals(
lines,
[
"▶ build",
" ▶ build / compile",
" ✓ build / compile",
" ▶ build / bundle",
" ✓ build / bundle",
"✓ build",
],
"each job and step should be rendered as it starts and ends",
);A step that throws is reported as StepFailed, then its error is rethrown into the body, which fails the job unless the body catches it. A job's terminal event (Finished, Skipped, Blocked or Cancelled) always comes after the terminal events of every job it needs.
How to match on an outcome
This guide shows you how to handle every way a job can end.
- Import
matchfrom@sekhmet/enum/core/mini.JobEndhas nomatchmethod of its own: matching is the one place outcome code uses enum's function. - Give one arm per variant:
Succeeded,Failed,Skipped,BlockedandCancelled. The compiler rejects a missing arm.
import { match } from "@sekhmet/enum/core/mini";
import {
Blocked,
Cancelled,
Failed,
type JobEnd,
Skipped,
Succeeded,
} from "@sekhmet/job/end";
import { assertEquals } from "@std/assert";
const describe = (outcome: JobEnd<string>): string => {
return match(outcome)({
Succeeded: (value) => `built ${value}`,
Failed: (error) => `failed: ${String(error)}`,
Skipped: () => "skipped",
Blocked: (blockedBy) => `blocked by ${blockedBy.join(", ")}`,
Cancelled: () => "cancelled",
});
};
assertEquals(
describe(Succeeded("dist")),
"built dist",
"a success should describe its value",
);
assertEquals(
describe(Blocked(["lint", "test"])),
"blocked by lint, test",
"a block should name what blocked it",
);
assertEquals(
describe(Failed(new Error("boom"))),
"failed: Error: boom",
"a failure should describe its error",
);
assertEquals(describe(Skipped()), "skipped", "a skip should be described");
assertEquals(
describe(Cancelled()),
"cancelled",
"a cancel should be described",
);When you care about one case, a predicate reads better: isSucceeded(), isBlocked() and the rest narrow the outcome, and the pairs isPassing() / isFailing() and isHealthy() / isUnhealthy() group the variants. See the outcome algebra for what each pair means.