Skip to content
Browse documentation
typescript sdk

Define and run a worker

Declare typed input and output, bind a handler, and dispatch a local run.

Declare a bounded job

Standard JSON Schema provides inference and a portable validation boundary. This example runs entirely in one process.

import { z } from "zod";
import { zodStandardJsonSchema } from "@smoothie-framework/foundation/zod";
import {
  defineWorker, InMemoryQueue, WorkerDispatcher, WorkerRuntime,
} from "@smoothie-framework/runtime/worker";

const count = defineWorker({
  id: "count",
  input: zodStandardJsonSchema(z.strictObject({ text: z.string() })),
  output: zodStandardJsonSchema(z.strictObject({ length: z.number().int() })),
});
const queue = new InMemoryQueue();
const worker = new WorkerRuntime({ queue, tenantId: "example", projectId: "demo" });
worker.register(count, input => ({ length: input.text.length }));
const jobs = new WorkerDispatcher({
  queue, tenantId: "example", projectId: "demo", requestedBy: "demo-service",
});

await worker.start();
try {
  const result = await (await jobs.start(count, { text: "Hello" })).wait();
  console.log(result);
} finally {
  await worker.stop();
}

Separate acceptance from completion

Input is checked before dispatch and before local execution. Typed output is checked before publication and when read by the dispatcher. A successful queue write means accepted transport, not completed business work.

Typed handlers cannot suspend/resume arbitrary checkpoints. Cancellation is available, but the handler and its external operations must cooperate. In-memory queues and stores are not durable.