Bird
Raised Fist0
NestJSframework~10 mins

Queue producers in NestJS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to inject the queue producer in the constructor.

NestJS
constructor(private readonly [1]: Queue) {}
Drag options to blanks, or click blank then click option'
AqueueService
BproducerQueue
CqueueProducer
Dqueue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that does not match the injected type.
Forgetting to mark the constructor parameter as private readonly.
2fill in blank
medium

Complete the code to send a message to the queue.

NestJS
await this.queue.[1]({ text: 'Hello' });
Drag options to blanks, or click blank then click option'
Apublish
Badd
Csend
Demit
Attempts:
3 left
💡 Hint
Common Mistakes
Using send which is not a method on the queue producer.
Using publish which is for event emitters.
3fill in blank
hard

Fix the error in the queue producer decorator.

NestJS
@[1]('email')
export class EmailProducer {}
Drag options to blanks, or click blank then click option'
AQueueProducer
BProducerQueue
CInjectQueue
DInjectQueueProducer
Attempts:
3 left
💡 Hint
Common Mistakes
Using @InjectQueue instead of @QueueProducer.
Using incorrect decorator names that do not exist.
4fill in blank
hard

Fill both blanks to correctly import and inject the queue.

NestJS
import { [1] } from '@nestjs/bull';

constructor(@[2]('notifications') private queue: Queue) {}
Drag options to blanks, or click blank then click option'
AInjectQueue
BQueueProducer
CInject
DBullModule
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing InjectQueue with QueueProducer.
Using Inject without specifying the queue name.
5fill in blank
hard

Fill all three blanks to create a job with a delay and priority.

NestJS
await this.queue.add('sendEmail', { to: 'user@example.com' }, { [1]: 5000, [2]: [3] });
Drag options to blanks, or click blank then click option'
Adelay
Bpriority
Chigh
Dtimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Using timeout instead of delay.
Setting priority to a number instead of a string like 'high'.

Practice

(1/5)
1. What is the main role of a queue producer in a NestJS application?
easy
A. To monitor the queue status and logs
B. To process tasks received from the queue
C. To delete completed tasks from the queue
D. To send tasks or messages to a queue for later processing

Solution

  1. Step 1: Understand the role of queue producer

    A queue producer is responsible for sending tasks or messages to a queue, not processing them.
  2. Step 2: Differentiate producer from consumer

    Consumers process tasks, while producers only add tasks to the queue.
  3. Final Answer:

    To send tasks or messages to a queue for later processing -> Option D
  4. Quick Check:

    Queue producer = sends tasks [OK]
Hint: Producers add tasks; consumers process them [OK]
Common Mistakes:
  • Confusing producer with consumer
  • Thinking producer processes tasks
  • Assuming producer deletes tasks
2. Which of the following is the correct way to inject a queue named 'email' in a NestJS producer service?
easy
A. @InjectQueue('email') emailQueue: Queue = new Queue();
B. @InjectQueue private emailQueue: Queue('email');
C. @InjectQueue('email') private readonly emailQueue: Queue;
D. @InjectQueue('email') private emailQueue = Queue;

Solution

  1. Step 1: Recall correct syntax for @InjectQueue

    The decorator requires the queue name as a string and the property must be typed as Queue.
  2. Step 2: Check property declaration

    It should be private readonly with type Queue, no manual instantiation needed.
  3. Final Answer:

    @InjectQueue('email') private readonly emailQueue: Queue; -> Option C
  4. Quick Check:

    @InjectQueue('name') private readonly queue: Queue [OK]
Hint: Use @InjectQueue('name') private readonly queue: Queue [OK]
Common Mistakes:
  • Missing private or readonly keywords
  • Trying to instantiate Queue manually
  • Incorrect decorator usage without queue name
3. Given the following code snippet in a NestJS producer service, what will be the output when sendEmail() is called?
async sendEmail() {
  await this.emailQueue.add('send_email', { to: 'user@example.com' });
  return 'Email task added';
}
medium
A. An error because 'send_email' is not a valid job name
B. 'Email task added'
C. The email is sent immediately
D. Nothing happens because add() is asynchronous

Solution

  1. Step 1: Understand queue.add() behavior

    The add() method adds a job to the queue asynchronously but does not send the email immediately.
  2. Step 2: Check return value of sendEmail()

    After adding the job, the function returns the string 'Email task added'.
  3. Final Answer:

    'Email task added' -> Option B
  4. Quick Check:

    queue.add() adds job, function returns confirmation [OK]
Hint: queue.add() adds job; function returns confirmation string [OK]
Common Mistakes:
  • Assuming add() sends email immediately
  • Thinking add() returns error for job name
  • Ignoring async nature of add()
4. Identify the error in this NestJS queue producer code:
@Injectable()
export class NotificationProducer {
  constructor(@InjectQueue('notifications') private notificationsQueue: Queue) {}

  async sendNotification(data: any) {
    this.notificationsQueue.add('notify', data);
  }
}
medium
A. Missing await before this.notificationsQueue.add()
B. Incorrect queue name in @InjectQueue decorator
C. sendNotification should not be async
D. notificationsQueue should be public, not private

Solution

  1. Step 1: Check async method usage

    The add() method returns a promise, so it should be awaited inside an async function to handle completion properly.
  2. Step 2: Verify other parts

    The queue name and access modifiers are correct; async keyword is appropriate.
  3. Final Answer:

    Missing await before this.notificationsQueue.add() -> Option A
  4. Quick Check:

    Async add() needs await to handle promise [OK]
Hint: Always await queue.add() inside async methods [OK]
Common Mistakes:
  • Forgetting await on async queue.add()
  • Changing queue name incorrectly
  • Removing async keyword unnecessarily
5. You want to create a NestJS queue producer that sends different types of jobs to the same queue with different priorities. Which approach correctly implements this?
hard
A. Use queue.add(jobName, data, { priority: number }) to set priority per job
B. Create multiple queues with different names for each priority level
C. Set priority globally on the queue during injection with @InjectQueue
D. Use queue.add(jobName, data) and sort jobs manually later

Solution

  1. Step 1: Understand job priority in queues

    Queue jobs can have individual priorities set when added, using options like { priority: number }.
  2. Step 2: Evaluate options

    Creating multiple queues or setting priority globally is not the recommended way; manual sorting is inefficient.
  3. Final Answer:

    Use queue.add(jobName, data, { priority: number }) to set priority per job -> Option A
  4. Quick Check:

    Set priority per job with add() options [OK]
Hint: Set priority in add() options per job [OK]
Common Mistakes:
  • Trying to set priority globally on queue
  • Creating many queues instead of one
  • Ignoring built-in priority support
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard