Esc
Documentation
On this page

A job is background work that runs without anyone opening a page. Use one to watch a service and publish a signal when something happens, such as a finished run or a failed check. DSUI runs a job once for every service that uses your adapter.

Define a job#

A job needs an id and exactly one trigger: intervalMs for a fixed interval, or schedule for a cron expression.

ts
import { defineJob } from "@northgraindata/dsui-adapter-sdk";
import type { MyContext } from "./context.js";

export const monitorRuns = defineJob<MyContext>({
  id: "monitor-runs",
  intervalMs: 60_000,
  run: async ({ context, signal, emit, store }) => {
    // read from the service, then emit a signal
  },
});

Register the job in jobs of defineAdapter, and register the signals it emits in signals.

Options#

OptionDefaultMeaning
idRequiredKebab-case, such as monitor-runs
intervalMsRun every this many milliseconds. An integer of at least 250
scheduleA five-field cron expression, evaluated in UTC
concurrencysingletonsingleton never overlaps, with one run at a time. per-key lets runs with different input overlap, and never overlaps runs with the same input
timeoutMs300000Milliseconds that a run may take before DSUI stops it
retry.maxAttempts1Attempts for a run, including the first
retry.backoffMs1000Milliseconds to wait between attempts. A job that retries needs a positive value
inputSchemaNoneA Zod schema for the input that DSUI passes to run

Setting both intervalMs and schedule, or neither, is an error when the adapter loads.

What run receives#

FieldMeaning
contextThe adapter context for the service
inputThe validated input, when you set inputSchema
runIdAn ID for this run
signalAn AbortSignal that fires when the run times out or DSUI stops it
store(definition)The live instance of an adapter store
blobsBinary storage private to the service
emit(signal, payload, options?)Publishes a declared signal

Keep progress in a persistent store#

A job runs in a short-lived process, so nothing in memory survives to the next run. Keep what the next run needs, such as the time of the last event you saw, in a persistent store:

ts
run: ({ store, emit }) => {
  const counter = store(counterStore);
  const next = counter.get().count + 1;
  counter.actions.setCount(next);
  emit(greeted, { name: `tick-${next}` }, { idempotencyKey: `tick-${next}` });
},

This job emitted tick-1, tick-2, and so on, once every 500 milliseconds, and the counter kept counting between runs.

Emit each event once#

Pass an idempotencyKey to emit, such as the ID of the run or event that you report. If a poll sees the same thing twice, DSUI records one event. A job that emits a signal it didn’t declare in signals fails with Adapter job emitted undeclared signal.

Start from now#

A job that reports changes should not report everything that happened before it started. The first time it runs, save the current time as the cursor and emit only for events after it. The official Airflow and dbt adapters work this way.