Job options (delay, attempts, priority) in NestJS Bull Queue
📖 Scenario: You are building a task queue in a NestJS application to process background jobs like sending emails or generating reports. You want to control when jobs run, how many times they retry on failure, and their priority in the queue.
🎯 Goal: Create a Bull queue job in NestJS with specific job options: a delay of 5000 milliseconds, 3 retry attempts, and priority level 2.
📋 What You'll Learn
Create a Bull queue job data object with a task property set to 'sendEmail'
Define a job options object with delay set to 5000, attempts set to 3, and priority set to 2
Add the job to the queue using the data and options objects
Use the bull package and NestJS Bull integration patterns
💡 Why This Matters
🌍 Real World
Background job processing is common in web apps to handle tasks like sending emails, processing images, or generating reports without blocking user requests.
💼 Career
Understanding job options like delay, attempts, and priority is essential for backend developers working with task queues to build reliable and efficient systems.
Progress0 / 4 steps
1
Create job data object
Create a constant called jobData and set it to an object with a property task equal to the string 'sendEmail'.
NestJS
Hint
Think of jobData as the information your job needs to run. Here, it just says what task to do.
2
Define job options with delay, attempts, and priority
Create a constant called jobOptions and set it to an object with these exact properties: delay set to 5000, attempts set to 3, and priority set to 2.
NestJS
Hint
These options tell the queue to wait 5 seconds before running, retry 3 times if it fails, and give this job priority 2.
3
Add the job to the Bull queue
Use the queue object and call its add method with jobData and jobOptions as arguments.
NestJS
Hint
This line sends your job with its options to the queue to be processed later.
4
Complete NestJS Bull queue setup with imports and injection
Add the import statement import { InjectQueue } from '@nestjs/bull'; and import { Queue } from 'bull'; at the top. Then, inside a class constructor, inject the queue with constructor(@InjectQueue('email') private readonly queue: Queue) {}.
NestJS
Hint
This completes the NestJS pattern to use Bull queues with dependency injection.
Practice
(1/5)
1. In NestJS job queues, what does the delay option do when adding a job?
easy
A. It sets the maximum number of retries for the job.
B. It postpones the job execution by a specified time.
C. It assigns the job a priority level.
D. It cancels the job immediately.
Solution
Step 1: Understand the purpose of delay in job options
The delay option is used to postpone when a job starts running after being added to the queue.
Step 2: Compare with other options
attempts controls retries, priority controls order, so they do not delay execution.
Final Answer:
It postpones the job execution by a specified time. -> Option B
Quick Check:
delay = postpone job start [OK]
Hint: Delay means wait before running the job [OK]
Common Mistakes:
Confusing delay with attempts (retry count)
Thinking delay sets job priority
Assuming delay cancels the job
2. Which of the following is the correct way to add a job with 3 retry attempts and a 5-second delay in NestJS?
easy
A. queue.add('jobName', data, { retries: 3, wait: 5000 })
B. queue.add('jobName', data, { delay: 3, attempts: 5000 })
C. queue.add('jobName', data, { attempts: '3', delay: '5000' })
D. queue.add('jobName', data, { attempts: 3, delay: 5000 })
Solution
Step 1: Identify correct option keys and values
The correct keys are attempts for retries and delay for postponing in milliseconds.
Step 2: Check value types and order
Values must be numbers, not strings. queue.add('jobName', data, { attempts: 3, delay: 5000 }) uses correct keys and numeric values.
A. The attempts option is invalid and should be removed.
B. The job name must be an object, not a string.
C. The delay value should be a number, not a string.
D. The data parameter is missing.
Solution
Step 1: Check types of job options
delay expects a number in milliseconds, but here it is a string '1000'.
Step 2: Validate other parameters
attempts is valid, job name as string is correct, data is present.
Final Answer:
The delay value should be a number, not a string. -> Option C
Quick Check:
delay must be number, not string [OK]
Hint: Delay must be number type, not string [OK]
Common Mistakes:
Passing delay as string instead of number
Removing valid attempts option
Thinking job name must be object
5. You want to add a job that should run after 10 seconds, retry up to 4 times if it fails, and run before other jobs with default priority. Which job options object is correct?
hard
A. { delay: 10000, attempts: 4 }
B. { delay: 10000, attempts: 4, priority: 1 }
C. { attempts: 4, priority: 0 }
D. { delay: 10000, priority: 0 }
Solution
Step 1: Understand default priority
If priority is not set, jobs have default priority (usually 0 or normal). To run before others, priority must be lower number.
Step 2: Check options for delay and attempts
Delay 10000 ms (10 seconds) and attempts 4 are required. Priority is optional if default is acceptable.
Step 3: Analyze options
{ delay: 10000, attempts: 4, priority: 1 } sets priority to 1 (lower priority than default 0), so it runs after default priority jobs. { delay: 10000, attempts: 4 } omits priority, so default priority applies, running before jobs with priority 1.