Bird
Raised Fist0
NestJSframework~10 mins

Job options (delay, attempts, priority) in NestJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Job options (delay, attempts, priority)
Create Job with Options
↓
Set delay timer?
Yes→Wait delay ms
↓
Delay expires
↓
Add job to queue
↓
Start job processing
↓
Process job
↓
Job succeeds
↓
Check attempts left
↓
Done
No Yes↓
Fail
↓
Process job again
This flow shows how a job is created with options like delay, attempts, and priority, then processed with retries if it fails.
Execution Sample
NestJS
queue.add('email', { to: 'user@example.com' }, { delay: 5000, attempts: 3, priority: 2 });
Adds an email job to the queue with a 5-second delay, up to 3 retry attempts, and priority level 2.
Execution Table
StepActionDelay TimerAttempts LeftPriorityJob Status
1Job created with options5000 ms32Waiting
2Delay expires after 5000 ms0 ms32Ready to process
3Job processing starts0 ms32Processing
4Job fails0 ms22Retrying
5Job processing retry 10 ms22Processing
6Job fails again0 ms12Retrying
7Job processing retry 20 ms12Processing
8Job succeeds0 ms12Completed
💡 Job succeeds at step 8 or fails after all attempts are used.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8
delay50000000
attempts33211
priority22222
jobStatusWaitingReady to processRetryingRetryingCompleted
Key Moments - 3 Insights
Why does the job wait before processing?
Because the delay option is set to 5000 ms, the job waits that time before starting processing, as shown in execution_table step 2.
What happens when a job fails but has attempts left?
The job retries processing, decreasing attempts by 1 each time, as seen in steps 4 and 6 where attempts go from 3 to 2 and then 1.
Does priority affect retry attempts?
No, priority stays the same during retries and only affects job order in the queue, not the number of attempts, as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the jobStatus at step 3?
AProcessing
BWaiting
CRetrying
DCompleted
💡 Hint
Check the 'Job Status' column in execution_table row with Step 3.
At which step does the delay timer reach zero?
AStep 1
BStep 2
CStep 4
DStep 8
💡 Hint
Look at the 'Delay Timer' column in execution_table to find when it changes from 5000 ms to 0 ms.
If attempts were set to 1 initially, how many retries would the job have?
A3 retries
B1 retry
CNo retries
D2 retries
💡 Hint
Refer to the 'Attempts Left' column in variable_tracker and how it decreases on failure.
Concept Snapshot
Job options in NestJS queues:
- delay: wait before processing (ms)
- attempts: retry count on failure
- priority: job order in queue
Jobs wait delay, then process.
Failures retry until attempts run out.
Full Transcript
This visual execution shows how NestJS job options delay, attempts, and priority work. A job is created with these options. The delay causes the job to wait before processing. When processing starts, if the job fails, it retries until attempts are used up. Priority affects job order but not retries. The execution table tracks each step, showing delay timer, attempts left, priority, and job status. Variable tracker shows how these values change over time. Key moments clarify common confusions about delay waiting, retry logic, and priority's role. The quiz tests understanding by referencing specific steps and variable states.

Practice

(1/5)
1. In NestJS job queues, what does the delay option do when adding a job?
easy
A. It sets the maximum number of retries for the job.
B. It postpones the job execution by a specified time.
C. It assigns the job a priority level.
D. It cancels the job immediately.

Solution

  1. Step 1: Understand the purpose of delay in job options

    The delay option is used to postpone when a job starts running after being added to the queue.
  2. Step 2: Compare with other options

    attempts controls retries, priority controls order, so they do not delay execution.
  3. Final Answer:

    It postpones the job execution by a specified time. -> Option B
  4. Quick Check:

    delay = postpone job start [OK]
Hint: Delay means wait before running the job [OK]
Common Mistakes:
  • Confusing delay with attempts (retry count)
  • Thinking delay sets job priority
  • Assuming delay cancels the job
2. Which of the following is the correct way to add a job with 3 retry attempts and a 5-second delay in NestJS?
easy
A. queue.add('jobName', data, { retries: 3, wait: 5000 })
B. queue.add('jobName', data, { delay: 3, attempts: 5000 })
C. queue.add('jobName', data, { attempts: '3', delay: '5000' })
D. queue.add('jobName', data, { attempts: 3, delay: 5000 })

Solution

  1. Step 1: Identify correct option keys and values

    The correct keys are attempts for retries and delay for postponing in milliseconds.
  2. 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.
  3. Final Answer:

    queue.add('jobName', data, { attempts: 3, delay: 5000 }) -> Option D
  4. Quick Check:

    Correct keys and numeric values = queue.add('jobName', data, { attempts: 3, delay: 5000 }) [OK]
Hint: Use numeric attempts and delay keys exactly [OK]
Common Mistakes:
  • Using wrong keys like retries or wait
  • Passing numbers as strings
  • Swapping delay and attempts values
3. Given this code snippet, what will be the order of job execution?
queue.add('job1', data1, { priority: 2 });
queue.add('job2', data2, { priority: 1 });
queue.add('job3', data3, { priority: 3 });
medium
A. job2, job1, job3
B. job3, job1, job2
C. job1, job2, job3
D. job1, job3, job2

Solution

  1. Step 1: Understand priority values

    Lower priority number means higher priority and runs first.
  2. Step 2: Order jobs by priority

    job2 has priority 1 (highest), job1 priority 2, job3 priority 3 (lowest).
  3. Final Answer:

    job2, job1, job3 -> Option A
  4. Quick Check:

    Lower priority number runs first = job2, job1, job3 [OK]
Hint: Lower priority number runs first [OK]
Common Mistakes:
  • Assuming higher number means higher priority
  • Ignoring priority and using add order
  • Mixing up job names and priorities
4. Identify the error in this job addition code:
queue.add('emailJob', data, { delay: '1000', attempts: 5 });
medium
A. The attempts option is invalid and should be removed.
B. The job name must be an object, not a string.
C. The delay value should be a number, not a string.
D. The data parameter is missing.

Solution

  1. Step 1: Check types of job options

    delay expects a number in milliseconds, but here it is a string '1000'.
  2. Step 2: Validate other parameters

    attempts is valid, job name as string is correct, data is present.
  3. Final Answer:

    The delay value should be a number, not a string. -> Option C
  4. Quick Check:

    delay must be number, not string [OK]
Hint: Delay must be number type, not string [OK]
Common Mistakes:
  • Passing delay as string instead of number
  • Removing valid attempts option
  • Thinking job name must be object
5. You want to add a job that should run after 10 seconds, retry up to 4 times if it fails, and run before other jobs with default priority. Which job options object is correct?
hard
A. { delay: 10000, attempts: 4 }
B. { delay: 10000, attempts: 4, priority: 1 }
C. { attempts: 4, priority: 0 }
D. { delay: 10000, priority: 0 }

Solution

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

    { delay: 10000, attempts: 4 } -> Option A
  5. Quick Check:

    Delay + attempts with default priority = { delay: 10000, attempts: 4 } [OK]
Hint: Omit priority for default; delay and attempts must be set [OK]
Common Mistakes:
  • Setting priority higher than default to run first
  • Forgetting delay or attempts
  • Assuming priority 0 is higher than 1
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