# Background jobs (/docs/starter/modules/jobs)

Choose with `--jobs pg-boss`, `--jobs bullmq`, `--jobs inngest`, or `--jobs none` (default). BullMQ and Inngest need Starter v0.20.0 or newer. Job definitions, the routes, and the dashboard's jobs card are the same whichever provider runs them, so you can switch later with `pnpm clubedge add jobs <provider>`. On [a React + Vite frontend with an API](../../cli/frontend-and-api.mdx#modules-on-these-stacks), the Hono API implements every provider, and FastAPI every provider but pg-boss, which has no Python version: see [A frontend and its API](#a-frontend-and-its-api).

| Provider | Where jobs live and run | Best for |
| --- | --- | --- |
| `pg-boss` | Your PostgreSQL, run by a worker inside the app server | One database to run, any host |
| `bullmq` | Redis, through BullMQ, run by a worker inside the app server; needs `--cache redis` | Long-running hosts that already run Redis: Railway, Fly.io, Docker |
| `inngest` | Inngest, which calls the app's `/api/inngest` to run each job | Serverless hosts such as Vercel and Netlify, with no cron to set up |

<Tabs items={['pg-boss', 'BullMQ', 'Inngest']} groupId="jobs" persist>
  <Tab value="pg-boss">
    Jobs run outside requests, with retries, in a worker that starts inside the app server: `pnpm dev` and the production server both run it, so there is no separate process to deploy. [pg-boss](https://github.com/timgit/pg-boss) stores them in your PostgreSQL, and creates and upgrades its own tables in the `pgboss` schema when the app starts.
  </Tab>
  <Tab value="BullMQ">
    Jobs run outside requests, with retries, in a worker that starts inside the app server, as with pg-boss. [BullMQ](https://docs.bullmq.io) stores them in Redis, at the `REDIS_URL` the cache also uses (Redis 6.2 or newer). Keep it on a Redis that doesn't evict keys, since jobs are data. With Docker [local services](../local-services.mdx), the Redis in `docker-compose.dev.yml` is used.
  </Tab>
  <Tab value="Inngest">
    [Inngest](https://www.inngest.com) runs the jobs: the app sends an event for each one, and Inngest calls the app's `/api/inngest` to run it, with retries and schedules. Nothing runs between requests, so serverless hosts need no cron.

    In development, start Inngest's Dev Server next to `pnpm dev`; its dashboard at [http://localhost:8288](http://localhost:8288) shows every run, with its attempts, output, and errors. `INNGEST_DEV=1` in `.env.local` points the app at it, and the app syncs its jobs with the Dev Server when it starts.

    ```sh
    pnpm jobs:dev
    ```

    In production, create an app in Inngest, set `INNGEST_EVENT_KEY` and `INNGEST_SIGNING_KEY`, and remove `INNGEST_DEV`; `pnpm check:env` requires the keys and refuses `INNGEST_DEV`. Then sync the deployed `https://<your-app>/api/inngest` in Inngest's dashboard; Inngest's Vercel and Netlify integrations do it on each deploy. The SDK checks every call with the signing key.
  </Tab>
</Tabs>

## Define a job [#define-a-job]

Define a job in `apps/web/src/jobs` and add it to the list in `apps/web/src/jobs/index.ts`:

```ts title="apps/web/src/jobs/welcome-email.ts"
import { defineJob } from "@clubedge/jobs";

export const welcomeEmail = defineJob<{ userId: string }>({
  name: "welcome-email",
  retryLimit: 3,
  async run({ userId }) {
    // Look the user up and send the email.
  },
});
```

Restart `pnpm dev` after adding or changing a job.

## Queue it [#queue-it]

Queue it from server code with `getJobs().enqueue(welcomeEmail, { userId })`, from `@/server/jobs`. With an [email module](./email.mdx), enqueue `sendEmailJob` from `@/jobs` with the message to send any email in the background, as the auth emails do.

- A thrown error fails the attempt and the job is retried (`retryLimit`, `retryDelaySeconds`, `retryBackoff`).
- An attempt that runs past `timeoutSeconds`, or whose server stops, is retried too, so make jobs safe to run twice.
- Add `schedule: "0 3 * * *"` (cron syntax, UTC) to run a job on a schedule.
- `enqueue(job, data, { key, throttleSeconds })` accepts one job per key in that window, and `delaySeconds` holds a job back.

The dashboard's jobs card queues the sample job from `apps/web/src/jobs/sample.ts` and lists its recent runs; remove both when you add your own. With Inngest, Inngest keeps the run history, and the card reads it from Inngest's API (the Dev Server's in development).

## Without a running server [#without-a-running-server]

With pg-boss or BullMQ, where no server keeps running, as on Vercel and Netlify, there is no worker: `JOBS_WORKER` defaults to `off` there, and you can set it yourself anywhere. Set `CRON_SECRET` to 16 or more random characters and call `GET /api/jobs/run` every minute or so with the header `Authorization: Bearer <CRON_SECRET>`, from Vercel Cron, a Netlify scheduled function, or any scheduler. Each call runs the jobs that are due. Each platform's [deploy page](../deploy/index.mdx) shows how, for example [Vercel](../deploy/vercel.mdx) and [Netlify](../deploy/netlify.mdx).

With Inngest, there is nothing to schedule: Inngest calls the app for each job.

## A frontend and its API [#a-frontend-and-its-api]

With the Hono API (Starter v0.25.0 or newer) and the FastAPI API (Starter v0.26.0 or newer), the jobs are the API's. Restart the API after adding or changing a job. The React + Vite dashboard has the same jobs card, which queues the sample job through `/api/jobs`.

- **Hono:** define jobs in `apps/api/src/jobs`, list them in `apps/api/src/jobs/index.ts`, and queue them from the API's code with `getJobs().enqueue(...)`, from `@/server/jobs`. The sample job is in `apps/api/src/jobs/sample.ts`.
- **FastAPI:** define jobs in `apps/api/src/clubedge_api/jobs.py` and add them to `JOBS` there, next to `sample_job`. A raised exception fails the attempt, and the options are the same in snake case, such as `retry_limit`, `timeout_seconds`, and `schedule`. Queue a job from the API's code with `await get_jobs().enqueue(welcome_email, {"userId": user_id})`, from `clubedge_api.background_jobs`, with `delay_seconds`, `key`, and `throttle_seconds` as keyword arguments. With an email module, enqueue `send_email_job` from `clubedge_api.jobs` with the message.

```python title="apps/api/src/clubedge_api/jobs.py"
async def _welcome_email(data: JobData, context: JobContext) -> None:
    # Look the user up by data["userId"] and send the email.
    ...


welcome_email = JobDefinition(name="welcome-email", run=_welcome_email, retry_limit=3)
```

With either API:

- **pg-boss and BullMQ:** the worker starts inside the API, with `pnpm dev` and the API's production server, so there is no separate process to deploy. BullMQ's `REDIS_URL` goes in `apps/api/.env.local`. To run the jobs elsewhere, set `JOBS_WORKER=off` and `CRON_SECRET` on the API, and call `GET /api/jobs/run` on the frontend, which forwards it to the API. The API stops its worker on `SIGTERM`, letting running jobs finish.
- **Inngest:** Inngest reaches the API through the frontend, which forwards `/api/inngest` to it, so the API stays private. The API tells Inngest its address from `FRONTEND_URL`. `INNGEST_DEV=1` goes in `apps/api/.env.local`, and the API syncs its jobs with the Dev Server once the frontend answers. In production, set the keys on the API and sync `https://<your-frontend>/api/inngest` in Inngest's dashboard.

pg-boss is the one provider FastAPI doesn't implement: it has no Python version, so the CLI refuses `--jobs pg-boss` with FastAPI. Use BullMQ to keep the worker inside the API, or Inngest.

## Without a website [#without-a-website]

In a project [without a website](../../cli/without-a-website.mdx) (Starter v0.28.0 or newer), the jobs are the API's, as above, and the worker runs inside it. Inngest reaches the jobs through the website's address, so the CLI refuses `--jobs inngest` there: Hono runs jobs on pg-boss or BullMQ, and FastAPI on BullMQ. To run the jobs elsewhere, set `JOBS_WORKER=off` and `CRON_SECRET` on the API, and call the API's own `GET /api/jobs/run`. `POST /api/jobs/sample` queues the sample job, and `GET /api/jobs` lists its recent runs.

## Another provider [#another-provider]

Jobs go through the provider-neutral `JobQueue` interface in `packages/jobs`. To use another queue, implement that interface in a new package and return it from `apps/web/src/server/jobs.ts` (`apps/api/src/server/jobs.ts` with the Hono API). Prove it with the conformance suite every provider runs, which covers retries, delays, throttling, timeouts, history, and the worker:

```ts title="packages/jobs-my-queue/src/index.test.ts"
import { describeJobQueue } from "@clubedge/jobs/conformance";
import { createMyQueue } from "./index";

describeJobQueue("my-queue", (jobs, { worker }) => createMyQueue({ jobs, worker }));
```

`runDue()` returns the jobs that finished and every attempt that failed, retries included. A provider that runs jobs by calling the app, as Inngest does, also implements `handler(request)`, which `/api/inngest` answers with.

With the FastAPI API, the `JobQueue` protocol is in `apps/api/src/clubedge_api/job_queue.py`, with the same methods in snake case, such as `run_due` and `handler`. Implement it in a new module and return it from `get_jobs` in `apps/api/src/clubedge_api/background_jobs.py`.
