patternjavascriptModerate
BullMQ: Job Scheduling and Cron Jobs
Viewed 0 times
bullmq cronrepeatable jobjob schedulercron expressionrecurring task
Problem
You need to run a task on a schedule (e.g., every day at 3am, every 5 minutes) without running a separate cron daemon.
Solution
Use BullMQ's repeat option with a cron expression or interval. BullMQ schedules the job in Redis and fires it automatically. Run only one scheduler process, or use the getRepeatableJobs / removeRepeatableByKey API to avoid duplicate schedules.
Why
BullMQ repeatable jobs are stored in Redis, so they survive process restarts. The schedule is enforced even if the worker was down and comes back up.
Gotchas
- If you add the same repeatable job definition multiple times (e.g., on every deploy), BullMQ creates duplicate schedules. Always check for existing repeatable jobs before adding, or use removeRepeatableByKey first.
- Cron expressions in BullMQ use 6 fields (with seconds) by default — not the standard 5-field Unix cron. Pass tz option for timezone-aware schedules.
- Repeatable jobs do not carry dynamic data well. If you need to pass different data on each run, prefer a separate scheduler service that adds regular jobs.
Code Snippets
BullMQ repeatable cron job
await reportQueue.add(
'generateDailyReport',
{ type: 'daily' },
{
repeat: { pattern: '0 3 * * *', tz: 'America/New_York' }, // 3am ET daily
jobId: 'daily-report-unique', // stable ID prevents duplicates on redeploy
}
);Revisions (0)
No revisions yet.