Job options help control how and when background tasks run. They let you delay jobs, retry failed jobs, and set job importance.
Job options (delay, attempts, priority) in NestJS
queue.add('jobName', data, { delay: number, attempts: number, priority: number })delay is in milliseconds and delays job start.
attempts sets the maximum number of attempts for the job (including the first one).
priority is a number where lower means higher priority.
queue.add('sendEmail', { to: 'user@example.com' }, { delay: 60000 })
queue.add('processOrder', { orderId: 123 }, { attempts: 3 })
queue.add('generateReport', {}, { priority: 1 })
This example shows a job that sends a welcome email. It waits 5 seconds before starting, tries up to 2 times, and has medium priority (5).
If the job ultimately fails after all attempts, the @OnQueueFailed handler logs the failure.
import { Processor, Process, OnQueueFailed } from '@nestjs/bull'; import { Job } from 'bull'; @Processor('email') export class EmailProcessor { @Process('sendWelcome') async handleSendWelcome(job: Job) { console.log(`Sending welcome email to ${job.data.email}`); if (Math.random() < 0.5) { throw new Error('Random failure'); } return 'Email sent'; } @OnQueueFailed() onFailed(job: Job, error: Error) { console.log(`Job ${job.id} failed: ${error.message}`); } } // Adding a job with options queue.add('sendWelcome', { email: 'user@example.com' }, { delay: 5000, attempts: 2, priority: 5 });
Delay uses milliseconds, so 1000 means 1 second.
Attempts help handle temporary errors by retrying jobs automatically.
Priority lets you control job order: lower numbers run first.
Job options control timing, retries, and order of background tasks.
Use delay to wait before running a job.
Use attempts to retry failed jobs automatically.
Use priority to run important jobs first.
Practice
delay option do when adding a job?Solution
Step 1: Understand the purpose of
Thedelayin job optionsdelayoption is used to postpone when a job starts running after being added to the queue.Step 2: Compare with other options
attemptscontrols retries,prioritycontrols order, so they do not delay execution.Final Answer:
It postpones the job execution by a specified time. -> Option BQuick Check:
delay = postpone job start [OK]
- Confusing delay with attempts (retry count)
- Thinking delay sets job priority
- Assuming delay cancels the job
Solution
Step 1: Identify correct option keys and values
The correct keys areattemptsfor retries anddelayfor postponing in milliseconds.Step 2: Check value types and order
Values must be numbers, not strings. queue.add('jobName', data, { attempts: 3, delay: 5000 }) uses correct keys and numeric values.Final Answer:
queue.add('jobName', data, { attempts: 3, delay: 5000 }) -> Option DQuick Check:
Correct keys and numeric values = queue.add('jobName', data, { attempts: 3, delay: 5000 }) [OK]
- Using wrong keys like retries or wait
- Passing numbers as strings
- Swapping delay and attempts values
queue.add('job1', data1, { priority: 2 });
queue.add('job2', data2, { priority: 1 });
queue.add('job3', data3, { priority: 3 });Solution
Step 1: Understand priority values
Lower priority number means higher priority and runs first.Step 2: Order jobs by priority
job2 has priority 1 (highest), job1 priority 2, job3 priority 3 (lowest).Final Answer:
job2, job1, job3 -> Option AQuick Check:
Lower priority number runs first = job2, job1, job3 [OK]
- Assuming higher number means higher priority
- Ignoring priority and using add order
- Mixing up job names and priorities
queue.add('emailJob', data, { delay: '1000', attempts: 5 });Solution
Step 1: Check types of job options
delayexpects a number in milliseconds, but here it is a string '1000'.Step 2: Validate other parameters
attemptsis valid, job name as string is correct, data is present.Final Answer:
The delay value should be a number, not a string. -> Option CQuick Check:
delay must be number, not string [OK]
- Passing delay as string instead of number
- Removing valid attempts option
- Thinking job name must be object
Solution
Step 1: Understand default priority
If priority is not set, jobs have default priority (usually 0 or normal). To run before others, priority must be lower number.Step 2: Check options for delay and attempts
Delay 10000 ms (10 seconds) and attempts 4 are required. Priority is optional if default is acceptable.Step 3: Analyze options
{ delay: 10000, attempts: 4, priority: 1 } sets priority to 1 (lower priority than default 0), so it runs after default priority jobs. { delay: 10000, attempts: 4 } omits priority, so default priority applies, running before jobs with priority 1.Final Answer:
{ delay: 10000, attempts: 4 } -> Option AQuick Check:
Delay + attempts with default priority = { delay: 10000, attempts: 4 } [OK]
- Setting priority higher than default to run first
- Forgetting delay or attempts
- Assuming priority 0 is higher than 1
Ready to prove it?
Pass the test and this pattern moves unknown
