Background jobs for NestJS
Jobs without the traps.
A thin layer over BullMQ 6 for NestJS. Typed queues, a dedupe on every enqueue, jittered and bounded retries, sweeps that run once per tick, and a shutdown that waits for running jobs.
bun add @noctcore/jobs bullmq ioredis
0.x on npm. NestJS 11 or 12, BullMQ 6, Node 22 or later.
The traps, closed by default.
Most BullMQ incidents are not bugs in BullMQ but defaults left unchanged. Each row is one of them and the part of the package that closes it, with a link to the page that explains how.
- TrapAn enqueue hangs the request while Redis restarts
producer connectionThe producer has its own connection: no offline queue, one retry, a 3 s bound. During a known outage add() refuses at once. - TrapA dedupe stops working once the job is removed
dedupeEvery add() picks a dedupe, held in its own Redis key, so retention never ends a window early. Keys are hashed. - TrapOne outage turns into a synchronised retry storm
defineQueueA retrying policy needs expo() or fixed(), jittered by default. Zero jitter or a hand-written backoff does not compile. - TrapCompleted and failed jobs fill Redis
keepRetention for both is required and always has an age. - TrapA failure alert fires on every attempt, or never
JobHostonFinalFailure and the error reporter run once, on the attempt that ends the job. - TrapA cron runs once per replica, or moves with the server’s zone
@SweepSweeps are BullMQ job schedulers: one run per tick however many replicas upsert them, and a cron needs its tz. - TrapA deploy kills jobs halfway
installGracefulShutdownWorkers drain, bounded, before the app closes; then the queues and the producer close and telemetry flushes.
One file, both sides.
A queue is defined once, with its policy and its jobs, in a file that producers and workers both import. Every field BullMQ would leave to a default is required here.
attemptsis a literal from 2 to 20, or'once'.- A retry needs
expo()orfixed(), jittered by default. keepbounds completed and failed jobs, always with an age.
// mail.queue.tsimport { defineQueue, expo } from '@noctcore/jobs';
export type MailJobs = { welcome: { userId: string }; receipt: { orderId: string };};
export const mail = defineQueue<MailJobs>('mail', { attempts: 5, backoff: expo('2s'), // 2s, 4s, 8s, 16s, jittered keep: { completed: '1d', failed: '14d' }, concurrency: 10, pace: { max: 50, per: '1s' }, // all replicas});What it refuses to do
- No silent default
- Attempts, backoff, retention, concurrency and a dedupe on every
add()are choices you write down. A single attempt is spelled'once'; no dedupe is spelleddedupe.none(). - No caller jobId
- BullMQ assigns every id. A dedupe goes through its own Redis key, hashed with SHA-256, so it never depends on how long a job is kept.
- No hanging enqueue
- Producers and workers get opposite Redis policies on separate connections. An
add()either lands or fails within its bound. - No zoneless cron
- A sweep's cron needs its
tz, and a renamed sweep is removed from Redis at boot. - No vendor SDK
- Metrics, tracing and error reporting are three small interfaces. The adapters take the prom-client, OpenTelemetry or Sentry module you already have.
Six things to learn
defineQueueA queue, its typed jobs and its policy, in one file both sides import.JobsModuleforRoot or forRootAsync once, forFeature where the queues are used.queue.addTyped by the queue, with a required dedupe, bounded by the producer timeout.@Worker + JobHostOne worker class per queue: handle, onFinalFailure, classify, pauseQueue.@SweepA method on a schedule, once per tick across every replica.installGracefulShutdownDrains workers before the app closes, then flushes telemetry.