Delayed and Scheduled Jobs
🎯 Introduction
By the end of this article, you will be able to submit a delayed job, create a recurring schedule, and remove it after the exercise.
Reuse the Redis configuration, queue-config.mjs, and write-text.mjs from the BullMQ quick start. This course uses BullMQ 5.61.2.
🧱 What Delayed and Scheduled Jobs Do
A delayed job is a Job that becomes eligible to run later; a recurring schedule is a rule that repeatedly generates jobs. A delayed job runs once, while a recurring schedule continues to produce executions.
Delayed jobs suit reminders or checking again after an external state may have changed. Recurring schedules suit synchronization at fixed intervals and periodic cleanup. When Workers are busy, jobs may start later than scheduled. A queue is not a precise timer.
🕒 Delayed Jobs
Create a one-time delayed job in delayed-job.mjs:
import { Queue } from 'bullmq';
import { connection, queueName } from './queue-config.mjs';
const queue = new Queue(queueName, {
connection: { ...connection, maxRetriesPerRequest: 1 },
});
queue.on('error', error => console.error(error.message));
try {
const job = await queue.add('write-text', { text: 'Text generated later' }, {
delay: 5000,
});
console.log('Delayed job ID:', job.id);
} finally {
await queue.close();
}
delay is measured in milliseconds. The job becomes eligible for processing after at least 5 seconds; this does not limit how long processing takes. Keep the Worker from the previous article running, then execute:
node delayed-job.mjs
The script prints the job ID immediately. The text file is generated once the delay has elapsed and a Worker is available.
🗓️ Recurring Jobs and Job Schedulers
A Job Scheduler stores the rules for recurring jobs. When Workers have sufficient capacity, it generates subsequent jobs at the specified interval. The schedule and each generated Job are separate records.
Create schedule.mjs with a rule that generates a file every 10 seconds:
import { Queue } from 'bullmq';
import { connection, queueName } from './queue-config.mjs';
const queue = new Queue(queueName, {
connection: { ...connection, maxRetriesPerRequest: 1 },
});
queue.on('error', error => console.error(error.message));
try {
await queue.upsertJobScheduler('text-every-10s', { every: 10000 }, {
name: 'write-text',
data: { text: 'Periodically generated text' },
});
console.log('Schedule saved');
} finally {
await queue.close();
}
The first argument uniquely identifies the schedule. Calling the method again with the same ID updates the schedule instead of creating another one. every specifies the interval in milliseconds, and the following object defines each Job's name and data.
node schedule.mjs
Keep the Worker running to see files appear over time. In BullMQ 5.19 and later, a newly created schedule produces its first Job as soon as possible, then continues according to its rule. Actual processing still depends on Worker availability. See the BullMQ Job Schedulers documentation.
🕘 Repeat at Calendar Times
To use calendar times, replace the upsertJobScheduler call in schedule.mjs with:
await queue.upsertJobScheduler('text-every-10s', {
pattern: '0 0 9 * * *',
tz: 'Asia/Shanghai',
}, {
name: 'write-text',
data: { text: 'Text generated every morning' },
});
pattern is a cron expression whose six fields represent seconds, minutes, hours, day of the month, month, and day of the week. This expression specifies 9:00 AM every day in the Shanghai time zone. tz determines how calendar times are interpreted, avoiding changes caused by different server time zones.
🧹 Remove a Schedule
After the exercise, create remove-schedule.mjs to remove the schedule:
import { Queue } from 'bullmq';
import { connection, queueName } from './queue-config.mjs';
const queue = new Queue(queueName, {
connection: { ...connection, maxRetriesPerRequest: 1 },
});
queue.on('error', error => console.error(error.message));
try {
console.log(await queue.removeJobScheduler('text-every-10s'));
} finally {
await queue.close();
}
node remove-schedule.mjs
A return value of true means the schedule was removed. Removing a schedule does not automatically cancel jobs that have already been created or are running.
❓ Knowledge Check
Q1: If delay is 5 seconds, will the job finish exactly 5 seconds later?
Not necessarily. The job only becomes eligible to start after 5 seconds and will keep waiting if Workers are busy or unavailable.
Q2: Is the schedule the same object as each generated Job?
No. The schedule keeps generating jobs; each Job records one execution.
Q3: Does the schedule remain active after the script exits?
Yes. It is stored in Redis until it is removed or the queue data is cleared.
🧪 Exercise
Change the interval from 10 seconds to 30 seconds, run the script, then run the removal script. Observe the difference between existing jobs and the schedule.
// Write your code here
🎉 You can now schedule delayed and recurring jobs!
