noctcore-nestjs/worker-must-implement-close
noctcore-nestjs/worker-must-implement-close
- Package
- nestjs v0.2.0
- Preset
- recommended
- Severity
- error
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
A class that creates a BullMQ Worker in a field closes it in a shutdown method.
A BullMQ worker holds a Redis connection and a lock on every job it is running. When the process
shuts down without await worker.close(), those jobs are abandoned mid-flight: their locks expire,
BullMQ marks them stalled, and another worker runs them again from the start. A deploy then sends
the same email twice or charges the same card twice, and the dropped connection shows up as
errors on the Redis side. close() stops taking new jobs and waits for the running ones to finish.
In NestJS the place for it is onModuleDestroy (or onApplicationShutdown), which Nest calls on
shutdown when app.enableShutdownHooks() is on.
import { Worker } from 'bullmq';
export class MailWorker { private worker = new Worker('mail', sendMail, { connection });}import { Worker } from 'bullmq';
export class MailWorker { private worker = new Worker('mail', sendMail, { connection });
async onModuleDestroy() { await this.worker.close(); }}What it flags
Section titled “What it flags”- A class whose instance field is initialised with a
new Worker(...)imported frombullmq, or that assignsthis.<field> = new Worker(...)(or??=,||=) in one of its methods (the constructor included), when none of its close methods closesthis.<field>. - A close method that exists but never closes that worker, for example one that only logs, or closes a queue and forgets the worker.
- A close method that overrides the base class’s close method without calling
super, when only the base closed the worker.
import { Queue, Worker } from 'bullmq';
export class MailWorker { private queue = new Queue('mail', { connection }); private worker!: Worker;
onModuleInit() { this.worker = new Worker('mail', sendMail, { connection }); }
async onModuleDestroy() { await this.queue.close(); }}import { Queue, Worker } from 'bullmq';
export class MailWorker { private queue = new Queue('mail', { connection }); private worker!: Worker;
onModuleInit() { this.worker = new Worker('mail', sendMail, { connection }); }
async onModuleDestroy() { await Promise.all([this.worker.close(), this.queue.close()]); }}What it does not flag
Section titled “What it does not flag”- A NestJS
@Processorclass extendingWorkerHost: Nest creates and closes that worker, so the class owns none. - A close call anywhere in a close method’s body: awaited or not, inside
Promise.all, throughthis.worker?.close()orthis.worker!.close(), or through a local alias (const { worker } = this; await worker.close(),const self = this; await self.worker.close()). - A worker a close method hands off to be closed: passed to a function
(
await closeQuietly(this.worker)) or put in an array literal (await Promise.all([this.worker, this.queue].map((w) => w.close()))). The rule trusts that the function or the array closes it. - A close in a helper the close method calls on
thisorsuper, at any depth:onModuleDestroy() { return this.stopWorkers(); }counts whenstopWorkerscloses the worker. - A close method inherited from a base class declared in the same file.
- A class that extends a base class from another module or a
declare class, since that base may close the worker, and a class whose close method is abstract, since a subclass implements it. - A worker held in a local variable, an array, a map, a static field or a
#privatefield. Only plain instance fields are tracked. this.worker = new Worker(...)inside a plain nestedfunction, wherethisis not the class.- A
Workerthat is not imported frombullmq, such as the one fromnode:worker_threads. A renamed import (import { Worker as BullWorker } from 'bullmq') is followed; a namespace import orrequire('bullmq')is not.
import { Processor, WorkerHost } from '@nestjs/bullmq';
@Processor('mail')export class MailProcessor extends WorkerHost { async process(job) { await sendMail(job.data); }}import { Queue, Worker } from 'bullmq';
export class MailWorker { private queue = new Queue('mail', { connection }); private worker = new Worker('mail', sendMail, { connection });
async onModuleDestroy() { await this.stopAll(); }
private async stopAll() { const { worker, queue } = this; await Promise.all([worker.close(), queue.close()]); }}Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
closeMethodNames |
string[] |
close, shutdown, dispose, onModuleDestroy, onApplicationShutdown, beforeApplicationShutdown |
Methods one of which must close this.<field>, directly or through a helper. Replaces the defaults, so list every name you use. The message points to onModuleDestroy when the list has it, else to the first name. |
'noctcore-nestjs/worker-must-implement-close': ['error', { closeMethodNames: ['stop'] }]// rule options: {"closeMethodNames":["stop"]}import { Worker } from 'bullmq';
export class MailWorker { private worker = new Worker('mail', sendMail, { connection });
async onModuleDestroy() { await this.worker.close(); }}// rule options: {"closeMethodNames":["stop"]}import { Worker } from 'bullmq';
export class MailWorker { private worker = new Worker('mail', sendMail, { connection });
async stop() { await this.worker.close(); }}When not to use it
Section titled “When not to use it”If your workers are closed from outside the class that creates them, for example by a shutdown
coordinator that collects every worker and closes them together, the rule reports each class.
Name the method the coordinator calls in closeMethodNames and close the worker there, or turn
the rule off for those files.