Bird
Raised Fist0
NestJSframework~20 mins

Named jobs in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Named Jobs Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when a named job is added twice with the same name?

Consider a NestJS Bull queue where you add a named job twice with the same name but different data. What will be the behavior?

NestJS
await queue.add('email', { to: 'a@example.com' });
await queue.add('email', { to: 'b@example.com' });
AThe second job replaces the first job in the queue.
BBoth jobs are added separately and processed independently.
COnly the first job is added; the second is ignored silently.
DAn error is thrown because job names must be unique.
Attempts:
2 left
💡 Hint

Think about how queues handle jobs with the same name but different data.

📝 Syntax
intermediate
2:00remaining
Which code correctly adds a named job with repeat options in NestJS Bull?

Choose the correct syntax to add a named job called 'cleanup' that repeats every hour.

Aawait queue.add('cleanup', {}, { repeat: { cron: '0 * * * *' } });
Bawait queue.add({ name: 'cleanup', data: {}, repeat: { cron: '0 * * * *' } });
Cawait queue.add('cleanup', {}, { repeat: '0 * * * *' });
Dawait queue.add('cleanup', { repeat: { cron: '0 * * * *' } });
Attempts:
2 left
💡 Hint

Remember the method signature: add(name, data, options).

🔧 Debug
advanced
2:00remaining
Why does this named job never run in NestJS Bull?

Given the following code, why does the named job 'processData' never execute?

NestJS
queue.process('processData', async (job) => {
  console.log('Processing:', job.data);
});

await queue.add('processdata', { id: 1 });
AThe process() method is missing a concurrency parameter, so it never runs.
BThe job data is missing required fields, so the job is skipped.
CThe job name in add() is lowercase 'processdata', but process() listens for 'processData' (case mismatch).
DThe queue is not started, so no jobs run.
Attempts:
2 left
💡 Hint

Check the exact spelling and casing of job names.

❓ state_output
advanced
2:00remaining
What is the job count after adding and removing named jobs?

Consider this sequence:

await queue.add('task1', {});
await queue.add('task2', {});
const job = await queue.getJob('task1');
await job?.remove();
const count = await queue.count();

What is the value of count?

A2
BThrows an error because getJob expects a job ID, not a name.
C0
D1
Attempts:
2 left
💡 Hint

Check the parameter type expected by getJob().

🧠 Conceptual
expert
3:00remaining
How does Bull handle concurrency for named jobs in NestJS?

In NestJS Bull, if you define multiple processors for the same named job with different concurrency values, what happens?

ABull throws an error because multiple processors for the same job name are not allowed.
BBull uses the highest concurrency value among processors for that job name.
COnly the first registered processor runs; others are ignored.
DBull runs all processors in parallel, each with its own concurrency setting.
Attempts:
2 left
💡 Hint

Think about how Bull distributes jobs to processors.

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

  1. Step 1: Understand the purpose of named jobs

    Named jobs provide a clear label to background tasks, making them easier to find and manage.
  2. 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.
  3. Final Answer:

    They help identify and manage specific background tasks easily. -> Option B
  4. 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

  1. 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).
  2. 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.
  3. Final Answer:

    queue.add('emailJob', { to: 'user@example.com' }); -> Option D
  4. 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?
const job = await queue.add('reportGeneration', { userId: 42 });
console.log(job.name);
medium
A. 'reportGeneration'
B. 'default'
C. undefined
D. Throws an error

Solution

  1. Step 1: Understand job creation with a name

    When adding a job with queue.add('reportGeneration', data), the job's name property is set to 'reportGeneration'.
  2. Step 2: Check the console.log output

    Logging job.name will print 'reportGeneration'.
  3. Final Answer:

    'reportGeneration' -> Option A
  4. Quick Check:

    Job name property = 'reportGeneration' [OK]
Hint: Job.name matches the string passed to queue.add [OK]
Common Mistakes:
  • Assuming default name is used if a name is given
  • Expecting undefined if name is not accessed properly
  • Thinking job creation throws error without processor
4. Identify the error in this NestJS queue job addition:
queue.add({ name: 'cleanupJob' }, { force: true });
medium
A. Job name should be a string, not inside an object.
B. The second argument must be a string, not an object.
C. The method add() does not exist on queue.
D. The job data cannot contain boolean values.

Solution

  1. Step 1: Review the correct syntax for named jobs

    The queue.add method expects the first argument as a string name, not an object.
  2. Step 2: Analyze the given code

    The code passes an object { name: 'cleanupJob' } instead of a string, causing a syntax error.
  3. Final Answer:

    Job name should be a string, not inside an object. -> Option A
  4. Quick Check:

    First argument to add() = string name [OK]
Hint: Pass job name as string, not object, to queue.add [OK]
Common Mistakes:
  • Passing job name inside an object instead of string
  • Confusing job data with job name argument
  • Assuming add() method is missing or invalid
5. You want to cancel all jobs named 'emailNotification' in a queue. Which approach correctly uses named jobs to achieve this?
hard
A. Restart the queue to clear all jobs named 'emailNotification'.
B. Call queue.removeJobs('emailNotification') directly without fetching jobs.
C. Use queue.getJobs(['emailNotification']) and then call remove() on each job.
D. Filter jobs by data property { type: 'emailNotification' } and delete them.

Solution

  1. Step 1: Understand how to retrieve named jobs

    Named jobs can be fetched using queue.getJobs(['jobName']) which returns jobs with that name.
  2. Step 2: Remove each fetched job

    After fetching, calling remove() on each job deletes them from the queue.
  3. Final Answer:

    Use queue.getJobs(['emailNotification']) and then call remove() on each job. -> Option C
  4. Quick Check:

    Fetch named jobs then remove [OK]
Hint: Fetch named jobs first, then remove them one by one [OK]
Common Mistakes:
  • Trying to remove jobs by name without fetching them
  • Filtering jobs by data instead of name
  • Restarting queue does not selectively remove jobs
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