Writing your own source
When the input is neither MLLP nor an HTTP POST, define the source yourself
with defineSource from @health-samurai/interbox/chain. It returns a
function that takes the source's config, and a pipeline reads it like any
other source:
import { env } from "@health-samurai/interbox";
import { defineSource, defineState, pipeline, postgresStore, type SourceRunContext } from "@health-samurai/interbox/chain";
const cursor = defineState<string>("lab-feed.cursor");
const labFeed = defineSource({
type: "lab-feed",
kind: "pull",
start: async (
{ url }: { url: string; pollIntervalMs: number },
{ receive, state, signal }: SourceRunContext<string>,
) => async () => {
const after = (await state(cursor).get("last")) ?? "";
const res = await fetch(`${url}?after=${encodeURIComponent(after)}`, { signal });
for (const result of (await res.json()) as { id: string; message: string }[]) {
state(cursor).set("last", result.id);
await receive(result.message);
}
},
});
pipeline("lab-results")
.source(labFeed({ url: env("LAB_FEED_URL"), pollIntervalMs: 60_000 }))
.store(postgresStore({ codec: "text" }))
.sender(/* ... */);
A source is one of two kinds:
| kind | you give | the engine |
|---|---|---|
| push, the default | run(config, ctx), which listens or loops until signal aborts | runs it once. When run returns on its own, the source has finished: it shows as exited and is not restarted until the next workspace reload. A listener that only opens a socket must still await signal before it returns |
"pull" | start(config, ctx), which returns a poll function | calls start once, then poll at once and again every pollIntervalMs of the config: 30 s when the config sets none, never more often than every second. Prefer it to a loop in run for anything that polls |
The context:
receive(input)runs the steps before the store on one input and commits the units they make. It resolves with{ received, skipped }once they are stored, and that is the moment to acknowledge the input upstream. It rejects when the input is refused, for instance when a step throws, and then nothing of it is stored. The refusal counts in the pipeline's refusals like a refused MLLP or webhook input.state(handle)is the pipeline's state. Itssetanddelare buffered and written by the nextreceive, in that input's transaction, so a cursor set beforereceiveadvances only if the input is stored. A refusedreceivedrops them.signalaborts when the engine stops the source, at shutdown or on a workspace reload. Pass it tofetchand stop listening when it aborts. An error the source throws after the stop ends it as stopped. An error thrown before is a crash, and the engine restarts the source after a backoff.
The config's type is the one run or start declares for its first
parameter, and the rows' type is the Row of SourceRunContext<Row> on the
second, as above; type arguments, defineSource<"lab-feed", Config, string>,
say the same. A pipeline may pass any string of the config as an env()
reference, though not a number. run and start get the config with them
resolved, and an unset variable
parks the pipeline. type names
the source in the dashboard and follows the pipeline name rules.
A source written with defineSource runs on the
active replica only, one copy of it. MLLP and
webhook sources listen on every replica. A replica that takes the active lease
starts each source again, so a source resumes from the state its last receive
committed, and even a finite source must keep its progress there. A replica
that loses the active lease stops its sources, which aborts signal, and a
receive it still runs rejects without storing anything. Pass signal to external operations: a
write or an acknowledgement already accepted upstream can still finish, so it
must tolerate a retry.