0
0
NestJSframework~30 mins

Job options (delay, attempts, priority) in NestJS - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

This completes the NestJS pattern to use Bull queues with dependency injection.