✨ Overview
@bymax-one/nest-queue is a NestJS dynamic module that wraps BullMQ 6 behind a typed,
opinionated surface. Instead of wiring ioredis connections, per-queue registration, and
maxRetriesPerRequest policy by hand, you install one library and get typed producers,
decorator-based workers, flows, recurring Job Schedulers, native deduplication,
OpenTelemetry passthrough, and a tested bounded graceful-shutdown protocol.
The library has zero direct dependencies — bullmq, ioredis, and @nestjs/* arrive as
peer dependencies, so you control exact versions and the supply-chain surface stays minimal.
🔥 Features
📤 Producing
- ✅ One setup, every queue — a single
forRoot / forRootAsync; queues are created on demand by name, with no registerQueue() per queue
- ✅ Typed producer API —
enqueue<TData, TResult>() threads both generics through to the returned Job, so job.data and job.returnvalue need no cast
- ✅ Bounded bulk insert —
enqueueBulk adds up to 1 000 jobs in a single Redis roundtrip and rejects a larger batch outright, rather than letting one call inflate the heap
- ✅ Native deduplication — simple, throttle, debounce and keep-last-if-active, passed straight through to BullMQ; the library writes no deduplication logic of its own
- ✅ Job Schedulers — cron (5-field, or 6-field with seconds) and fixed intervals through
upsertJobScheduler; the addRepeatable API removed in BullMQ v6 is never called
- ✅ Flows — opt-in
FlowService over BullMQ's FlowProducer, with fail-parent semantics preserved
⚙️ Consuming
- ✅ Decorator workers —
@Processor, @Process, @OnWorkerEvent and @OnQueueEvent, discovered automatically from your providers
- ✅ Programmatic registry —
WorkerRegistry.register() for workers built at runtime, and registerSandboxed() for file-based out-of-process processors
- ✅ Progress reporting —
job.updateProgress() with the matching worker event wired through to your handler
🔌 Connections & Shutdown
- ✅ Dual-mode connection — bring your own
ioredis client, or let the library open its own from a URL or an options object
- ✅ Per-role retry policy —
maxRetriesPerRequest: null reaches only the duplicated Worker and QueueEvents connections; the producer keeps a finite retry limit, a 5 s command deadline and no offline queue on the connection the library opens, and enqueue has its own 10 s deadline, so a Redis outage rejects a call in seconds instead of holding the request; a shared client with maxRetriesPerRequest: null is the exception, and the library warns about it at boot
- ✅ One namespace, four objects — the configured
prefix reaches Queue, Worker, QueueEvents and FlowProducer, so producers and consumers can never end up on separate keyspaces
- ✅ Bounded graceful shutdown — a single ordered sequence drains every worker under one deadline and then stops any that are still busy, whose jobs stall and are retried; delivery is at-least-once by design, so handlers must be idempotent
- ✅ No unlistened emitter — every
Queue, Worker, QueueEvents and FlowProducer the library creates carries a fallback error listener, so a transient Redis fault cannot become an uncaught exception in your process; your own @OnWorkerEvent('error') or @OnQueueEvent('error') still runs alongside it
🧩 Developer Experience
- ✅ Stable error catalog — namespaced codes exported from both subpaths, so a frontend maps a
code without importing the server
- ✅ Dual-format output — ESM + CJS with declarations for each format, verified against the packed tarball on every PR
- ✅ Opt-in observability — OpenTelemetry passthrough via
bullmq-otel, plus a MetricsService with a configurable TTL cache
- ✅ Typed end to end — TypeScript
strict with exactOptionalPropertyTypes and noUncheckedIndexedAccess; zero any, no @ts-ignore and no eslint-disable
📦 Subpath Exports
./shared exists so a browser or edge bundle can import the job-status union and the
error-code catalog without pulling in bullmq, ioredis, or @nestjs/*. Nothing under
that subpath imports a runtime dependency.
| Subpath |
Import |
Purpose |
Dependencies |
. |
@bymax-one/nest-queue |
Full server surface: module, services, decorators, types |
NestJS · BullMQ · ioredis |
./shared |
@bymax-one/nest-queue/shared |
Job-status union, error codes, metric and scheduler types |
none |
./observability |
@bymax-one/nest-queue/observability |
Prometheus metrics, a readiness indicator and OpenTelemetry spans (optional) |
the main entry · @bymax-one/nest-core · prom-client · @opentelemetry/api |
@bymax-one/nest-queue (server — NestJS + BullMQ + ioredis)
│
└── re-exports ──▶ @bymax-one/nest-queue/shared (zero dependencies)
Every entry point ships ESM and CommonJS with declarations for each format, so a
require() consumer receives CommonJS declarations rather than ESM ones. The
pnpm check:exports gate verifies this against the packed tarball.
Peer dependency matrix
| Subpath |
Required peers |
. (server) |
@nestjs/common ^11.0.16, @nestjs/core ^11.1.18, bullmq ^6.0, ioredis ^6.0, reflect-metadata ^0.2 |
./shared |
None |
./observability |
The main entry's peers, plus @bymax-one/nest-core ^1.6.10, prom-client ^15 and @opentelemetry/api ^1.9 (all optional: install them only to import this subpath) |
| Optional |
bullmq-otel ^2 (OpenTelemetry) |
[!IMPORTANT]
reflect-metadata is a global polyfill the application owns — import it once at
the top of main.ts. This library never imports it, so it cannot be shipped twice
into your bundle; NestJS pulls it in as well, which is why a correctly wired
application already has it before a decorator runs.
[!TIP]
A full reference application lives in
bymaxone/nest-queue-example —
an API and a web console exercising every public export against a real Redis.
🚀 Quick Start
1. Install
pnpm add @bymax-one/nest-queue bullmq ioredis reflect-metadata
2. Load reflect-metadata
reflect-metadata is loaded once by your application, before anything else — this
is the standard NestJS entry point and the decorators in this library depend on it:
// main.ts
import 'reflect-metadata'
import { NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
async function bootstrap(): Promise<void> {
const app = await NestFactory.create(AppModule)
await app.listen(3000)
}
void bootstrap()
3. Register the module
Wire the module in your root AppModule:
import { Module } from '@nestjs/common'
import { BymaxQueueModule } from '@bymax-one/nest-queue'
import { EmailModule } from './email.module'
@Module({
imports: [
BymaxQueueModule.forRoot({
connection: { url: process.env.REDIS_URL ?? 'redis://localhost:6379' },
}),
EmailModule,
],
})
export class AppModule {}
4. Enqueue a job
From any service:
import { Injectable } from '@nestjs/common'
import { QueueService } from '@bymax-one/nest-queue'
@Injectable()
export class EmailService {
constructor(private readonly queue: QueueService) {}
async sendWelcome(userId: string): Promise<void> {
await this.queue.enqueue<{ userId: string }, void>('email', 'welcome', { userId })
}
}
5. Process jobs
With a decorated handler:
import { Injectable } from '@nestjs/common'
import { Processor, Process } from '@bymax-one/nest-queue'
import type { Job } from '@bymax-one/nest-queue'
@Injectable()
@Processor('email')
export class EmailProcessor {
@Process('welcome')
async handleWelcome(job: Job<{ userId: string }, void>): Promise<void> {
// handle job.data.userId
}
}
6. Register the providers
Put the service and the processor in a feature module that AppModule imports, like the EmailModule
above. The module is global by default (isGlobal defaults to true), so QueueService is injectable
there without importing BymaxQueueModule again:
import { Module } from '@nestjs/common'
import { EmailProcessor } from './email.processor'
import { EmailService } from './email.service'
@Module({
providers: [EmailService, EmailProcessor],
})
export class EmailModule {}
⚙️ Configuration
Every option passed to forRoot / forRootAsync. Only connection is required.