Skip to content
Logic SDK
Esc
↑↓navigate↵open⌘Jpreview
On this page

Node workers

Run due jobs and tick crons in a long-running Node or Bun process.

work(registry) polls the jobs table and ticks crons on the minute.

Set up the database queue

Add the jobs table to your migrations. Add the cron runs table if you define crons.

import { databaseQueue, drizzleAdapter } from "logic-sdk/drizzle/pg";

export const registry = createRegistry({
  modules,
  database: { adapter: drizzleAdapter(db) },
  jobs: { adapter: databaseQueue(), scope: { store, load } },
});

Start the worker

For a local worker, start work in the web process. For a separate worker, set databaseQueue({ runIn: "worker" }) in both processes. Start work in the worker process.

import { work } from "logic-sdk/node";
import { registry } from "./registry";

const stop = work(registry, { concurrency: 5 });

process.on("SIGTERM", async () => {
  await stop({ timeout: 30 });
  process.exit(0);
});
  • Slots. concurrency defaults to 1. Give the pool concurrency + 1 connections for job work and lease renewal.
  • Polling. An idle slot waits pollEvery seconds, default 2. A local commit wakes it. When a sweep throws, the slot doubles the wait, up to 30 seconds, and calls the worker’s onError.
  • Queues. List queues in priority order to dedicate a process to selected queues. Omit the list to run all queues.
  • Ticks. The worker starts them every minute. It reports and skips the next minute when ten ticks remain in progress.
  • Shutdown. stop() waits for running jobs and ticks. Its default timeout is 30 seconds.

Check delivery

Run a mutation that queues a job. The worker takes it and the request returns without waiting.

With the default runIn and no local worker, the request runs due-now jobs or hands them to waitUntil. Sweep regularly if no worker polls the table.

See also

Was this page helpful?