The Repeat class is deprecated and will be removed in v6. Use JobScheduler instead.
Overview
The Repeat class manages repeatable jobs that execute at specified intervals using cron patterns or fixed delays.
Access via queue.repeat property. For new implementations, use queue.jobScheduler instead.
Constructor
new Repeat(
name: string,
opts: RepeatBaseOptions
)
Methods
updateRepeatableJob
Updates or creates a repeatable job.
updateRepeatableJob<T, R, N>(
name: N,
data: T,
opts: JobsOptions,
config: { override: boolean }
): Promise<Job<T, R, N> | undefined>
getRepeatableJobs
Gets all repeatable jobs.
getRepeatableJobs(
start?: number,
end?: number,
asc?: boolean
): Promise<RepeatableJob[]>
Offset of first job to return
Offset of last job to return
Return in ascending order by next execution time
removeRepeatable
Removes a repeatable job.
removeRepeatable(
name: string,
repeat: RepeatOptions,
jobId?: string
): Promise<number>
Repeat options (must match the ones used when creating)
removeRepeatableByKey
Removes a repeatable job by its key.
removeRepeatableByKey(key: string): Promise<number>
The repeatable job key (obtained from getRepeatableJobs)
getRepeatableCount
Gets the count of repeatable jobs.
getRepeatableCount(): Promise<number>
RepeatableJob
interface RepeatableJob {
key: string; // Unique key for the repeatable job
name: string; // Job name
id?: string; // Optional job ID
endDate?: number; // End date timestamp
tz?: string; // Timezone
pattern?: string; // Cron pattern
every?: string; // Interval
next?: number; // Next execution timestamp
}
Migration to JobScheduler
To migrate from Repeat to JobScheduler:
Old (Repeat)
await queue.add(
'task',
{ data: 'value' },
{
repeat: {
pattern: '0 * * * *',
},
}
);
const repeatableJobs = await queue.getRepeatableJobs();
await queue.removeRepeatable('task', { pattern: '0 * * * *' });
New (JobScheduler)
await queue.upsertJobScheduler(
'task-scheduler',
{ pattern: '0 * * * *' },
{
name: 'task',
data: { data: 'value' },
}
);
const schedulers = await queue.getJobSchedulers();
await queue.removeJobScheduler('task-scheduler');
Example
import { Queue } from 'bullmq';
const queue = new Queue('myQueue', {
connection: {
host: 'localhost',
port: 6379,
},
});
// Access the repeat instance
const repeat = await queue.repeat;
// Get all repeatable jobs
const jobs = await repeat.getRepeatableJobs();
console.log(jobs);
// [
// {
// key: 'task:::0 * * * *',
// name: 'task',
// pattern: '0 * * * *',
// next: 1234567890000
// }
// ]