Skip to content
Quickstart

Quickstart

Payload-blind durable work scheduling and settlement core

This runs one job through an in-memory node:sqlite database. The owner object is the whole host contract for the SQLite entrypoint: run a statement, and run a synchronous transaction.

quickstart.ts
import { DatabaseSync } from "node:sqlite";
import { fromNodeSqlite } from "@fungi.computer/watchdog/node-sqlite";
import { Watchdog } from "@fungi.computer/watchdog";
const database = new DatabaseSync(":memory:");
const owner = fromNodeSqlite(database);
const watchdog = await Watchdog.make({
owner,
execute: async (job, signal) => {
if (signal.aborted) return { kind: "cancelled" };
console.log(`sending ${job.jobId}`, job.payload);
return { kind: "completed" };
},
// This process owns every claim, so a running job is alive while it runs.
isAlive: async () => true,
// Arm your real timer or alarm here. Ticking by hand is enough for a demo.
wake: { recompute: async () => {} },
});
await watchdog.ingest({
jobId: "welcome-email-1",
queue: "email",
lane: "default",
priority: 0,
payload: { to: "ada@example.com" },
state: "queued",
});
const tick = await watchdog.tick();
console.log(tick.status); // "settled"
const read = await watchdog.read("welcome-email-1");
if (read.status === "found" && read.job.state === "settled")
console.log(read.job.outcome); // "completed"
database.close();

Run tick() again and it returns { status: "no_ready" }. Ingest the same job again and it returns { status: "duplicate" }.

In a Durable Object, ctx.storage is already the SQLite owner.