Background jobs
Jobs with retries and schedules, run by pg-boss, BullMQ, or Inngest behind one interface.
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, the Hono API implements every provider, and FastAPI every provider but pg-boss, which has no Python version: see 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 |
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 stores them in your PostgreSQL, and creates and upgrades its own tables in the pgboss schema when the app starts.
Define a job
Define a job in apps/web/src/jobs and add it to the list in apps/web/src/jobs/index.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 from server code with getJobs().enqueue(welcomeEmail, { userId }), from @/server/jobs. With an email module, 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, anddelaySecondsholds 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
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 shows how, for example Vercel and Netlify.
With Inngest, there is nothing to schedule: Inngest calls the app for each job.
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 inapps/api/src/jobs/index.ts, and queue them from the API's code withgetJobs().enqueue(...), from@/server/jobs. The sample job is inapps/api/src/jobs/sample.ts. - FastAPI: define jobs in
apps/api/src/clubedge_api/jobs.pyand add them toJOBSthere, next tosample_job. A raised exception fails the attempt, and the options are the same in snake case, such asretry_limit,timeout_seconds, andschedule. Queue a job from the API's code withawait get_jobs().enqueue(welcome_email, {"userId": user_id}), fromclubedge_api.background_jobs, withdelay_seconds,key, andthrottle_secondsas keyword arguments. With an email module, enqueuesend_email_jobfromclubedge_api.jobswith the message.
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 devand the API's production server, so there is no separate process to deploy. BullMQ'sREDIS_URLgoes inapps/api/.env.local. To run the jobs elsewhere, setJOBS_WORKER=offandCRON_SECRETon the API, and callGET /api/jobs/runon the frontend, which forwards it to the API. The API stops its worker onSIGTERM, letting running jobs finish. - Inngest: Inngest reaches the API through the frontend, which forwards
/api/inngestto it, so the API stays private. The API tells Inngest its address fromFRONTEND_URL.INNGEST_DEV=1goes inapps/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 synchttps://<your-frontend>/api/inngestin 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
In a project without a website (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
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:
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.
How is this guide?