Overview
Delayed jobs are placed into a special “delayed set” instead of being processed immediately. After the delay time has passed, the job moves to the waiting queue and is processed as a regular job.The delay time is quite accurate in most cases, but it’s not guaranteed to be exact. Processing depends on worker availability and the number of other delayed jobs scheduled at that time.
Basic Usage
Use thedelay option with the amount of time in milliseconds to delay the job:
src/classes/job.ts:88:
Scheduling for a Specific Time
To process a job at a specific point in time, calculate the delay from now:Change Delay
You can reschedule a delayed job after inserting it using thechangeDelay method:
src/classes/job.ts:1048:
Delayed Job Lifecycle
When a delayed job is added:- Job is added to the “delayed” sorted set with the timestamp when it should be processed
- Workers periodically check for delayed jobs whose timestamp has passed
- When the delay expires, the job moves to the “wait” or “prioritized” list
- The job is then processed normally
src/classes/job.ts:985:
Move to Delayed Programmatically
You can manually move an active job to the delayed state:src/classes/job.ts:1413:
Job Options Interface
Fromsrc/interfaces/base-job-options.ts:21:
Best Practices
Sync Clocks
For accurate delays, ensure worker and producer clocks are synchronized (use NTP).
Avoid Long Delays
For delays longer than a few hours, consider using repeatable jobs or external schedulers.
Monitor Delayed Jobs
Use
getDelayed() to monitor the number of delayed jobs and their timestamps.Handle Clock Skew
Be aware that clock differences between servers can affect delay accuracy.
Common Use Cases
Retry with Delay
Retry with Delay
Automatically retry failed jobs after a delay:
Scheduled Notifications
Scheduled Notifications
Send notifications at a specific time:
Rate Limiting
Rate Limiting
Delay jobs to implement rate limiting:
API Reference
View the Change Delay API Reference
