OptimalBits / bull

Premium Queue package for handling distributed jobs and messages in NodeJS.
Other
15.58k stars 1.43k forks source link

Adding job for retry with LIFO option not working as intended. #2757

Closed Haris-Ali closed 2 months ago

Haris-Ali commented 4 months ago

I am working with bull, and have a use case which so far hasn't worked in my testing. Suppose there's three jobs in a FIFO queue A, B and C. Now if job B fails, I want to immediately retry it by putting it in front of C i.e. LIFO and only when it has failed or completed will I process job C. Below is a test code that I have tried which doesn't seem to work as it processes all jobs in FIFO, doesn't add the failed job in the queue again and doesn't process it the second time as well. Any help regarding fixing this would be appreciated.

const queueOptions = {
    redis: { host: REDIS_HOST, port: REDIS_PORT },
    defaultJobOptions: {
        removeOnComplete: true
    },
};

const testQueue = new Bull("test-Queue4", queueOptions);

testQueue.process(async (job, done) => {
    console.log(await testQueue.getJobCounts());
    console.log(`Processing job ${job.id} with data: ${JSON.stringify(job.data)}`);
    console.log(`This job's attemptsMade count is: ${job.attemptsMade}`);
    setTimeout(() => {
        try {
            if (Math.random() > 0.25) throw new Error("error");
            else done();
        } catch (e) {
            done(e);
        }
    }, 5000);
});

testQueue.on('completed', async (job, result) => {
    console.log(`Job ${job.id} completed with result ${result}`);
    console.log(await testQueue.getJobCounts());
});

testQueue.on('failed', async (job, err) => {
    console.log(`Job ${job.id} failed with error ${err.message || err}`);
    console.log(`Job attempts: ${job.attemptsMade}`);
    if (job.attemptsMade === 1) testQueue.add(job.data, { jobId: job.id, lifo: true, delay: 0 });
    else console.log(`All attempts failed`);
    console.log(await testQueue.getJobCounts());
});

const jobs = [...new Array(3)].map((_, i) => ({
    text: "Hello World!",
    id: i
}));

jobs.forEach((job) => testQueue.add(job));

Bull version

4.12.9

stale[bot] commented 2 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.