queue.empty() do not empty all jobs
Seems to do nothing with delayed, completed and active job.
Cf doc : https://github.com/OptimalBits/bull# Cf code : https://github.com/OptimalBits/bull/blob/master/lib/queue.js#L202
well, I would say the method does what the documentation says. But its open to discussion if this is what we want or not. If you want to remove everything then a different method called clean would be better.
Ok, if i understand well, it empty jobs that have not yet being processed right?
So it miss delayed jobs.
where is the clean method?
Note that a perfect clean method would be very hard to implement due to the distributed nature of bull: https://github.com/OptimalBits/bull/issues/92
We are using something like this, and it seems to clean the queue correctly, even when running with multiple workers:
queue.on('completed', function (job, isFinished) {
if (isFinished) {
job.queue.pause().then(function () {
job.queue.clean(0, 'completed').then(function () {
job.queue.clean(0, 'active').then(function () {
job.queue.clean(0, 'delayed').then(function () {
job.queue.clean(0, 'failed').then(function () {
job.queue.empty().then(function () {
job.queue.close();
});
});
});
});
});
});
}
});
the only thing that is left is "bull:my-queue:id" key.
@asafyish yes but please do not write code like that, you miss the point of using promises:
queue.on('completed', function (job, isFinished) {
if (isFinished) {
job.queue.pause().then(function () {
return job.queue.clean(0, 'completed');
}).then(function () {
return job.queue.clean(0, 'active');
}).then(function () {
return job.queue.clean(0, 'delayed');
}).then(function () {
return job.queue.clean(0, 'failed');
}).then(function () {
return job.queue.empty();
}).then(function () {
return job.queue.close();
});
}
});
Even better:
queue.on('completed', function (job, isFinished) {
if (isFinished) {
var clean = job.queue.clean.bind(job.queue, 0);
job.queue.pause()
.then(clean('completed'))
.then(clean('active'))
.then(clean('delayed'))
.then(clean('failed'))
.then(function () {
return job.queue.empty();
}).then(function () {
return job.queue.close();
});
}
});
Of course since the whole operation is not atomic, the queue may contain items after calling this method.
Yeah you are completely right. The moment I got it working I posted it here, without much thinking :grin:
I use this code
async function clearRedis(queue){ await queue.empty(); await queue.clean(0, 'active'); await queue.clean(0, 'completed'); await queue.clean(0, 'delayed'); await queue.clean(0, 'failed'); return testQueue(queue); } async function testQueue(queue){ let ll = await queue.getJobCounts(); expect(ll.active + ll.completed + ll.delayed + ll.failed).to.eq(0); }
i got success with this one, i added the setTimeout to see last log, i have dynamically added cron job that repeats every 4 secs,
timeSeriesQueue.on("global:completed", function (jobId, result) {
console.log(`Job ${jobId} completed! Result: ${result}`);
timeSeriesQueue.getJob(jobId).then(function (job) {
setTimeout(() => {
job.remove();
}, 5000);
});
});```
@nilooy that seems like the same functionality we already have when using the "removeOnComplete" option https://docs.bullmq.io/guide/jobs/fifo#default-job-options