📖 Scenario: You are building a NestJS application that processes background tasks using Bull queues. You want to organize your jobs by giving each job a specific name. This helps you identify and manage different types of jobs easily.
🎯 Goal: Create a Bull queue in NestJS and add named jobs to it. Then, set up a processor that listens for jobs by their names and handles them accordingly.
📋 What You'll Learn
Create a Bull queue named emailQueue
Add a named job sendWelcomeEmail with data containing userId: 123
Add a named job sendPasswordReset with data containing userId: 456
Create a processor that listens for sendWelcomeEmail jobs and logs the userId
Create a processor that listens for sendPasswordReset jobs and logs the userId
💡 Why This Matters
🌍 Real World
Named jobs help organize background tasks like sending different types of emails or processing different workflows in a clear way.
💼 Career
Understanding named jobs in queues is important for backend developers working with task queues, microservices, and scalable applications.
Progress0 / 4 steps
1
Setup Bull queue in NestJS
Import BullModule in your module and register a queue named emailQueue using BullModule.registerQueue.
NestJS
Hint
Use BullModule.registerQueue({ name: 'emailQueue' }) inside the imports array.
2
Add named jobs to the queue
Inject the emailQueue using @InjectQueue('emailQueue') in a service. Add two named jobs: sendWelcomeEmail with data { userId: 123 } and sendPasswordReset with data { userId: 456 } using emailQueue.add.
NestJS
Hint
Use this.emailQueue.add('sendWelcomeEmail', { userId: 123 }) and similarly for sendPasswordReset.
3
Create processors for named jobs
Create a processor class decorated with @Processor('emailQueue'). Add two methods decorated with @Process('sendWelcomeEmail') and @Process('sendPasswordReset'). Each method receives a job and logs job.data.userId.
NestJS
Hint
Use @Processor('emailQueue') on the class and @Process('sendWelcomeEmail') and @Process('sendPasswordReset') on methods.
4
Trigger job addition in application
In the EmailService, add a method called start that calls addJobs(). Then, in the AppModule, inject EmailService and call start() in the constructor to trigger job addition when the app starts.
NestJS
Hint
Create a start method in EmailService that calls addJobs(). Then call this.emailService.start() in AppModule constructor.
Practice
(1/5)
1. What is the main benefit of using named jobs in NestJS queues?
easy
A. They automatically speed up job processing.
B. They help identify and manage specific background tasks easily.
C. They encrypt job data for security.
D. They allow jobs to run without a queue.
Solution
Step 1: Understand the purpose of named jobs
Named jobs provide a clear label to background tasks, making them easier to find and manage.
Step 2: Compare options with the purpose
Only They help identify and manage specific background tasks easily. matches this purpose. Options B, C, and D describe unrelated features.
Final Answer:
They help identify and manage specific background tasks easily. -> Option B
Quick Check:
Named jobs = easier management [OK]
Hint: Named jobs label tasks for easy tracking [OK]
Common Mistakes:
Thinking named jobs improve speed
Confusing named jobs with security features
Assuming named jobs remove the need for queues
2. Which of the following is the correct way to add a named job to a queue in NestJS?
easy
A. queue.pushJob('emailJob', { to: 'user@example.com' });
B. queue.createJob('emailJob', { to: 'user@example.com' });
C. queue.runJob('emailJob', { to: 'user@example.com' });
D. queue.add('emailJob', { to: 'user@example.com' });
Solution
Step 1: Recall the NestJS queue method for adding jobs
The correct method to add a job with a name is queue.add(name, data).
Step 2: Match the method with options
Only queue.add('emailJob', { to: 'user@example.com' }); uses queue.add with a job name and data correctly.
Final Answer:
queue.add('emailJob', { to: 'user@example.com' }); -> Option D
Quick Check:
Use queue.add(name, data) [OK]
Hint: Use queue.add('jobName', data) to name jobs [OK]
Common Mistakes:
Using non-existent methods like createJob or pushJob
Forgetting to pass the job name as first argument
Confusing job execution with job creation
3. Given this code snippet, what will be the output when checking the job name?