[Bug]: Repeatable job activated twice on the same second
Version
4.18.2
Platform
NodeJS
What happened?
A repeatable job has been triggered twice, despite only one job being stored with the same ID and timestamps, leading to double processing. The locking mechanism appears to fail in certain cases, causing duplicate job activations. Notably, the Node process remains active, does not exceed the lock duration, and the job is not stalled, as referenced in the documentation.
This issue leads to data corruption on our end, even though we enforce database-level locking using Mongo flags. However, since the job is triggered within the same minute and second, the DB lock fails to prevent duplicates due to a race condition. The logging system confirms this by recording two logs for the same job in the onActive event.
How to reproduce.
Steps to Reproduce: 1. Create a repeatable job that executes with some cadence, for example every minute. 2. Add logging to track the job execution. 3. Simulate a scenario where locking should prevent duplicate executions. 4. Observe if the job runs twice within the same execution window. 5. Please note, we have very high amount of repeatable jobs and this issue is not happening intermittently but for some jobs only.
import { Queue, Worker, QueueScheduler } from 'bullmq';
import { setTimeout } from 'timers/promises';
const queueName = 'repeatable-job-test';
const queue = new Queue(queueName);
const scheduler = new QueueScheduler(queueName); // Ensures repeatable jobs are processed properly
async function setupJob() {
await queue.add('repeat-job', { id: 1 }, { repeat: { every: 60000 }});
console.log('Repeatable job added.');
}
const worker = new Worker(queueName, async (job) => {
console.log(`[${new Date().toISOString()}] Processing job:`, job.id, job.data);
await setTimeout(500);
}, {
concurrency: 2,
});
worker.on('active', (job) => {
console.log(`[onActive] Job id:${args.jobId} activated.`);
});
(async () => {
await setupJob();
})();
Relevant log output
@OnQueueEvent('active')
onActive(args: { jobId: string; prev?: string }) {
console.log(`[onActive] Job id:${args.jobId} activated.`);
}
Mar 10 08:38:46.965 [onActive] job repeat:0073cfea5c888708e27065134e4b72d5:1741581526926 has been activated
Mar 10 08:38:46.964 [onActive] job repeat:0073cfea5c888708e27065134e4b72d5:1741581526926 has been activated
Code of Conduct
- [x] I agree to follow this project's Code of Conduct
Thanks for this reports. A couple of things, the QueueScheduler instance is not required after version 2.0 of BullMQ. Secondly, repeatable jobs are much more stable in v5, so this would be the first I would suggest, to upgrade to the latest version and see if you still get the same issue.
We have added a service level caching of active jobs with Redis ,so it is resolved now but we would expect BullMQ to handle this case. We plan to change to new version but will need to handle breaking changes.
pls @annieTechno let us know if upgrading fixes your issue. If not you can reopen this issue