BullMQ helpers
Measure processor health and use instrumented queues and workers with @queuebert/bullmq.
Install#
npm install @queuebert/bullmq @queuebert/nest @nestjs/bullmq@^11 @nestjs/common@^11 @nestjs/core@^11 bullmq@^5Import from @queuebert/bullmq, not @queuebert/nest/bullmq. The package exposes both Nest helpers and direct queue/worker wrappers. Its declared Nest peer dependencies still apply when using the direct wrappers.
Instrument a Nest processor#
Extend BaseQueueProcessor, which implements Nest's WorkerHost and Queuebert's processor contract. Override processJob(), not process(), so the base class can record timing and outcomes.
import { Processor } from '@nestjs/bullmq';
import { BaseQueueProcessor } from '@queuebert/bullmq';
import type { Job } from 'bullmq';
@Processor('emails', { concurrency: 10 })
export class EmailProcessor extends BaseQueueProcessor {
constructor() {
super({ queueName: 'emails' });
}
async processJob(job: Job<{ to: string }>) {
// Replace with your actual delivery operation. Throw on failure.
return { recipient: job.data.to };
}
}Register EmailProcessor in your module's providers, and pass queues: [{ name: 'emails', processor: EmailProcessor }] to QueuebertModule.forRoot().
The base class tracks durations, processed/completed/failed counts, throughput, and Redis connection status. getCustomStats() can add application-specific values; do not return tokens, customer data, or other secrets.
Create queues and workers through Nest#
QueuebertBullMQModule.forRoot() configures QueuebertBullMQService. Async registration is also available through forRootAsync({ imports, inject, useFactory }).
import { Module } from '@nestjs/common';
import { QueuebertBullMQModule } from '@queuebert/bullmq';
@Module({
imports: [
QueuebertBullMQModule.forRoot({
connection: { host: '127.0.0.1', port: 6379 },
defaultQueueOptions: {
defaultJobOptions: { attempts: 3, removeOnComplete: true },
},
statsConfig: { windowMs: 60_000, maxSamples: 1000 },
}),
],
})
export class WorkersModule {}Inject QueuebertBullMQService to call createQueue() and createWorker(). This module creates instrumented queue/worker objects; it does not by itself mount the HTTP API. Use @queuebert/nest to expose a client-facing endpoint.
Direct queue and worker wrappers#
QueuebertQueue and QueuebertWorker can run outside a Nest application. This example demonstrates dispatch and local stats; the processor only returns a result, it does not send real email.
import { QueuebertQueue, QueuebertWorker } from '@queuebert/bullmq';
const connection = { host: '127.0.0.1', port: 6379 };
const queue = new QueuebertQueue<{ to: string }>('emails', { connection });
const worker = new QueuebertWorker<{ to: string }, { recipient: string }>('emails', async (job) => ({ recipient: job.data.to }), { connection });
await queue.add('welcome', { to: 'person@example.com' });
console.log(worker.getStats());
// During application shutdown, after work is complete:
await worker.close();
await queue.close();Pause notes (next release)#
await queue.pause('Deploying API v2.3'); // queue-wide
console.log(await queue.getPauseInfo());
await queue.resume(); // clears the queue note
await worker.pause(false, 'Inspecting a job'); // only this worker
await worker.resume(); // clears this worker's noteQueuebertBullMQService.pauseWorker(queueName, doNotWaitActive?, reason?) also
accepts a reason. Notes are optional, trimmed, and limited to 200 UTF-16 code
units. Existing boolean worker-pause calls keep working. Worker notes use the
worker ID and expire from reporting when its heartbeat stops; they do not pause
other workers or survive as pause instructions after a process restart. The
Nest API exposes them separately as workerPauses[]. Calls made directly on
the underlying BullMQ objects bypass Queuebert's note lifecycle.
Interpret statistics#
Processor and worker measurements describe work observed by that process; they are not a durable, cluster-wide history. A restart resets local measurements. Redis queue counts and retained completed/failed jobs are a separate signal, and BullMQ retention policies affect those counts.
Use DurationStatsCollector and GlobalStatsCollector for custom instrumentation. QueuebertProcessorAdapter and createProcessorAdapter adapt wrapper statistics to the Nest processor interface. Use OpenTelemetry when you need to aggregate measurements across processes in your existing telemetry backend.