Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
How to prevent a pending or completed job from being added to bull queue?

I'm polling(using node-cron) an external service every N minutes for an array of data, each element in the array has a unique id. How can I setup my bull queue such that if I add an element of data to be processed I cannot add(and consequently reprocess) the same element to the queue if the element of data is already being processed or is already processed/completed, however if the element of data fails to be processed for whatever reason it can be re-added to the queue to attempt processing again.

The following is a rough setup of my task.

import BullQueue from 'bull';
import cron from 'node-cron';

const myQueue = new BullQueue('my-queue');

myQueue.process((job, done) => {
    // while job is processing do not allow a new job with same uniqueLogId to be added
    console.log('job.id:'); // job id auto-generated by bull
    console.log(job.id);

    console.log('uniqueLogId:');
    console.log(job.data.id);

    const result = myProcessor(job.data);
    if (result.ok) {
        // in this case a new job with uniqueLogId should not be allowed to be created
        done();
    } else {
        // in this case a new job with uniqueLogId should be allowed to be created
        done(new Error('job failed'));
    }
});

// run every 10 minutes
cron.schedule('*/10 * * * *', () => {
    pollExternalAPI() // can return the same log results
        .then((res) => {

            if (res.ok) {
                const logs = res.logs;
                for (const log of logs) {
                    // log.id uniquely identifies a job,
                    // hence jobs with the same id should not be allowed to be created provided the last job is either pending or complete
                    // however if job failed then create another job to attempt processing the given log data
                    myQueue.add(log);
                }
            }
    });
});
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!