noctcore-nestjs/worker-concurrency
noctcore-nestjs/worker-concurrency
- Package
- nestjs v0.2.0
- Preset
- none opt-in
- Severity
- off
- Autofix
- No
- Suggestions
- No
- Options
- Optional
- Type info
- Not needed
A literal BullMQ worker concurrency, on new Worker(...) or @Processor(queue, options), is a positive integer.
A BullMQ worker runs one job at a time unless concurrency says otherwise. The option is a count of
jobs to run in parallel, so a literal 0, a negative number or a fraction is a configuration
defect, not a tuning choice. It is usually a typo or a half-finished edit that nobody notices until
the queue backs up:
import { Worker } from 'bullmq';
export const mailWorker = new Worker('mail', sendMail, { connection, concurrency: 0 });import { Worker } from 'bullmq';
export const mailWorker = new Worker('mail', sendMail, { connection, concurrency: 2.5 });State a whole number of at least 1:
import { Worker } from 'bullmq';
export const mailWorker = new Worker('mail', sendMail, { connection, concurrency: 5 });What it flags
Section titled “What it flags”A concurrency that is a number literal of 0 or below (0, -0, -1) or not a whole number
(2.5), in the options of:
- a
new Worker(name, processor, options)whereWorkeris imported frombullmq, under its own name or a renamed one; - the second argument of
@Processor(queue, options)or@ns.Processor(queue, options).
A value that is 0 or below and also fractional (-2.5) is reported once, as not positive.
@Processor(QUEUE, { concurrency: -1 })export class MailProcessor extends WorkerHost {}@Processor(QUEUE, { concurrency: 4 })export class MailProcessor extends WorkerHost {}What it does not flag
Section titled “What it does not flag”- A
concurrencythe rule cannot read as a literal: an identifier (concurrency: WORKERS), a member access, a call (config.get('concurrency')), and a string literal ('5'). - Options passed by name instead of an object literal, and a spread in the options or in the arguments: the key may come from there.
- A
Workerthat is not imported frombullmq, including a local class of that name. new bullmq.Worker(...)through a namespace import andrequire('bullmq'): only a named import is resolved.- The
worker.concurrency = nsetter after construction. - The first argument of
@Processor({ name, ... }): in that form it holds the processor options and the worker options are the second argument, so only the second is read.
import { Worker } from 'bullmq';import * as bullmq from 'bullmq';
new Worker('mail', sendMail, { concurrency: WORKERS });new Worker('mail', sendMail, { concurrency: config.get('concurrency') });new Worker('mail', sendMail, workerOptions);new Worker('mail', sendMail, { ...base, ...override });new bullmq.Worker('mail', sendMail, { concurrency: 0 });@Processor({ name: QUEUE, concurrency: 0 })export class MailProcessor extends WorkerHost {}Options
Section titled “Options”| Option | Type | Default | Meaning |
|---|---|---|---|
requireExplicit |
boolean |
false |
Also report a worker whose options object literal has no concurrency key, or that has no options at all. |
'noctcore-nestjs/worker-concurrency': ['error', { requireExplicit: true }]With requireExplicit, every worker states how many jobs it runs at once. Options passed by name
and an options literal holding a spread are still accepted, since the key may come from there:
// rule options: {"requireExplicit":true}import { Worker } from 'bullmq';
new Worker('mail', sendMail, { connection });// rule options: {"requireExplicit":true}import { Worker } from 'bullmq';
new Worker('mail', sendMail, { connection, concurrency: 1 });new Worker('mail', sendMail, { ...baseOptions });When not to use it
Section titled “When not to use it”When worker settings come from shared configuration the rule cannot read, such as a factory that
builds every options object: the rule then has nothing literal to check, and requireExplicit
would ask for a key that the factory already sets. The default check costs nothing in that case.
Related
Section titled “Related”worker-must-listen-failedandworker-must-implement-closepolice the samenew Worker(...)for a failure listener and a shutdown.