Bird
Raised Fist0
NestJSframework~20 mins

Queue consumers (processors) in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Queue Mastery in NestJS
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a NestJS queue consumer throws an error?

Consider a NestJS queue consumer that processes jobs. What is the default behavior if the consumer function throws an error during job processing?

NestJS
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('email')
export class EmailProcessor {
  @Process('send')
  async handleSendEmail(job: Job) {
    throw new Error('Failed to send email');
  }
}
AThe job is marked as failed and retried automatically if retry options are set.
BThe job is silently ignored and removed from the queue.
CThe entire queue stops processing further jobs until the error is fixed.
DThe job is marked as completed despite the error.
Attempts:
2 left
💡 Hint

Think about how Bull handles job failures and retries by default.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a NestJS queue processor for a 'notifications' queue?

Choose the correct code snippet that defines a NestJS queue processor for a queue named 'notifications'.

A
import { Processor, Process } from '@nestjs/bull';

@Processor('notifications')
export class NotificationsProcessor {
  @Process('send')
  async handleSend(job: Job) {
    // process job
  }
}
B
import { Processor, Process } from '@nestjs/bull';

@Processor('notifications')
export class NotificationsProcessor {
  @Process()
  async handle(job: Job) {
    // process job
  }
}
C
import { Processor, Process } from '@nestjs/bull';

@Processor()
export class NotificationsProcessor {
  @Process('notifications')
  async handle(job: Job) {
    // process job
  }
}
D
import { Processor, Process } from '@nestjs/bull';

@Processor('notifications')
export class NotificationsProcessor {
  @Process('notifications')
  async handle(job: Job) {
    // process job
  }
}
Attempts:
2 left
💡 Hint

The @Processor decorator takes the queue name. The @Process decorator takes the job name.

❓ state_output
advanced
2:00remaining
What is the value of job.attemptsMade after 2 failed retries?

Given a NestJS queue job configured with 3 attempts, what will be the value of job.attemptsMade inside the processor when the job is being retried for the third time?

NestJS
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('tasks')
export class TasksProcessor {
  @Process('run')
  async handleRun(job: Job) {
    console.log(job.attemptsMade);
    throw new Error('Fail');
  }
}

// Job options: { attempts: 3 }
A1
B3
C2
D0
Attempts:
2 left
💡 Hint

Remember that attemptsMade counts how many times the job has been tried before the current attempt.

🔧 Debug
advanced
2:00remaining
Why does this NestJS queue consumer never process jobs?

Review the following NestJS queue consumer code. Why does it never process any jobs?

NestJS
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('orders')
export class OrdersProcessor {
  @Process('')
  async handleOrder(job: Job) {
    console.log('Processing order', job.id);
  }
}
AThe Job type is imported incorrectly.
BThe @Process decorator has an empty string, so it does not match any job name.
CThe @Processor decorator is missing the queue name.
DThe handleOrder method is not async.
Attempts:
2 left
💡 Hint

Check the @Process decorator argument and how job names are matched.

🧠 Conceptual
expert
2:00remaining
Which statement about NestJS queue processors and concurrency is true?

In NestJS using Bull queues, which statement about concurrency in queue processors is correct?

AConcurrency is automatically handled by Bull and cannot be configured in NestJS.
BConcurrency is controlled only by the number of queue processors registered, not by any decorator option.
CNestJS queue processors process jobs strictly one at a time, concurrency is not supported.
DSetting concurrency in @Process decorator allows multiple jobs of the same type to be processed in parallel within the same processor instance.
Attempts:
2 left
💡 Hint

Think about how Bull and NestJS allow parallel job processing.

Practice

(1/5)
1. What is the main purpose of a queue consumer (processor) in a NestJS application?
easy
A. To send HTTP requests to external APIs
B. To render UI components on the client side
C. To listen to a queue and process jobs asynchronously in the background
D. To manage database migrations synchronously

Solution

  1. Step 1: Understand the role of queue consumers

    Queue consumers listen to queues and handle jobs asynchronously, allowing background processing.
  2. Step 2: Compare with other options

    Sending HTTP requests, rendering UI, and managing migrations are unrelated to queue consumers.
  3. Final Answer:

    To listen to a queue and process jobs asynchronously in the background -> Option C
  4. Quick Check:

    Queue consumer = background job handler [OK]
Hint: Queue consumers handle background jobs, not UI or HTTP [OK]
Common Mistakes:
  • Confusing queue consumers with HTTP controllers
  • Thinking queue consumers run synchronously
  • Assuming queue consumers manage UI rendering
2. Which decorator is used to mark a class as a queue consumer (processor) in NestJS?
easy
A. @Processor
B. @Queue
C. @Process
D. @Consumer

Solution

  1. Step 1: Identify the decorator for queue consumers

    The @Processor decorator marks a class as a queue consumer in NestJS.
  2. Step 2: Differentiate from other decorators

    @Process marks methods, @Queue and @Consumer are not valid NestJS decorators for this purpose.
  3. Final Answer:

    @Processor -> Option A
  4. Quick Check:

    Class decorator for consumers = @Processor [OK]
Hint: Class decorator for consumers is @Processor, method decorator is @Process [OK]
Common Mistakes:
  • Using @Process on the class instead of methods
  • Confusing @Processor with @Queue
  • Assuming @Consumer is a valid decorator
3. Given the following NestJS queue consumer code, what will be logged when a job with data { name: 'Alice' } is processed?
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('email')
export class EmailProcessor {
  @Process('send')
  async handleSend(job: Job) {
    console.log(`Sending email to ${job.data.name}`);
  }
}
medium
A. No output because handleSend is not called
B. Sending email to Alice
C. Error: job.data is not defined
D. Sending email to undefined

Solution

  1. Step 1: Understand job data usage

    The job object contains data with a name property 'Alice'. The method logs this value.
  2. Step 2: Confirm method is called on 'send' jobs

    The @Process('send') decorator ensures handleSend runs for jobs named 'send' in the 'email' queue.
  3. Final Answer:

    Sending email to Alice -> Option B
  4. Quick Check:

    job.data.name = 'Alice' logged correctly [OK]
Hint: Job data is accessed via job.data inside @Process methods [OK]
Common Mistakes:
  • Assuming job.data is undefined
  • Thinking method won't run without explicit call
  • Confusing queue name with job name
4. Identify the error in this NestJS queue consumer code snippet:
import { Processor, Process } from '@nestjs/bull';
import { Job } from 'bull';

@Processor('notifications')
export class NotificationProcessor {
  @Process()
  async handleNotification(job: Job) {
    console.log('Notification:', job.data.message);
  }
}
medium
A. Job parameter type should be any, not Job
B. The class should not have @Processor decorator
C. Console.log syntax is incorrect
D. @Process decorator is missing the job name argument

Solution

  1. Step 1: Check @Process decorator usage

    @Process requires a job name string to specify which jobs to handle; it's missing here.
  2. Step 2: Validate other parts

    @Processor on class is correct, Job type is valid, console.log syntax is correct.
  3. Final Answer:

    @Process decorator is missing the job name argument -> Option D
  4. Quick Check:

    @Process needs job name argument [OK]
Hint: @Process must specify job name string inside parentheses [OK]
Common Mistakes:
  • Omitting job name in @Process decorator
  • Removing @Processor from class
  • Changing Job type unnecessarily
5. You want to create a NestJS queue consumer that processes two different job types: 'email' and 'sms' in the same queue 'notifications'. Which is the correct way to define the processor class and methods?
hard
A. Use @Processor('notifications') on class, and two methods with @Process('email') and @Process('sms') decorators
B. Use two separate classes each with @Processor('notifications') and one @Process method
C. Use @Processor('email') and @Processor('sms') on the same class with one @Process method
D. Use @Process('notifications') on methods without @Processor on class

Solution

  1. Step 1: Understand queue and job type separation

    The queue is 'notifications'. The job types are 'email' and 'sms'. One class can handle multiple job types with separate @Process decorators.
  2. Step 2: Validate correct decorator usage

    @Processor('notifications') marks the queue. Methods use @Process('email') and @Process('sms') to handle each job type.
  3. Final Answer:

    Use @Processor('notifications') on class, and two methods with @Process('email') and @Process('sms') decorators -> Option A
  4. Quick Check:

    One class, multiple @Process methods for job types [OK]
Hint: One @Processor class can have many @Process methods for different jobs [OK]
Common Mistakes:
  • Creating multiple classes for same queue unnecessarily
  • Using multiple @Processor decorators on one class
  • Omitting @Processor on class and only using @Process
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