---
title: Node workers
description: 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.

1. **Set up the database queue**

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

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

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

2. **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.

    ```ts
    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.

3. **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](/docs/concepts/jobs#operations) if no worker polls the table.

## See also

- [Adapters](/docs/reference/adapters)
- [Jobs](/docs/concepts/jobs)
- [Crons](/docs/concepts/crons)
