bull icon indicating copy to clipboard operation
bull copied to clipboard

queue.empty() do not empty all jobs

Open lchenay opened this issue 11 years ago • 10 comments

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

lchenay avatar Jan 25 '15 17:01 lchenay

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.

manast avatar Jan 25 '15 18:01 manast

Ok, if i understand well, it empty jobs that have not yet being processed right?

So it miss delayed jobs.

lchenay avatar Jan 25 '15 19:01 lchenay

where is the clean method?

donsunsoft avatar Jan 30 '15 06:01 donsunsoft

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

manast avatar Jan 30 '15 07:01 manast

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 avatar Jul 21 '15 06:07 asafyish

@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.

manast avatar Jul 21 '15 09:07 manast

Yeah you are completely right. The moment I got it working I posted it here, without much thinking :grin:

asafyish avatar Jul 21 '15 09:07 asafyish

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); }

jerradpatch avatar Jan 09 '20 20:01 jerradpatch

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 avatar Jun 20 '21 18:06 nilooy

@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

manast avatar Jun 21 '21 06:06 manast