Bull: Cannot define a handler more than once per Queue instance

4.9k Views Asked by At

I'm using bull with node.js. I was thinking of using a master-slave model, where one thread puts stuff in the queue, and another just pulls it from the queue.

However, when my worker threads try to pull from the queue, they get this error:

Error: Cannot define a handler more than once per Queue instance

when I call this function

var doJobs = function() {
    return Queue.process(job, jobDone) {
        console.log("job done");
        jobDone();
    });
}

Can anyone tell me what this error means?

3

There are 3 best solutions below

0
On

queue.process(job => {/* ... */}) registers a processing handler for a queue that describes how a job should be performed. It doesn't actually trigger a job to be performed on the spot as you may think.

The error prevents you from redefining or mistakenly adding additional processing handlers -- a job can only have one way to be processed.

When you add jobs to the queue with queue.add(), the handler you've previously registered by passing to queue.process is applied to the job, producing a result that can be accessed with await job.finished(), queue.on("completed", (job, result) => {/* ... */}) or a completion queue, for example.

By the way, your "master-slave model" is actually a producer-consumer model. Master-slave is when the slave is under the full control of the master, but in producer-consumer, the consumer doesn't have to do anything and isn't typically under the control of the producer at all.

0
On

The doJobs function was being hit multiple times. I thought this was correct, in reality, you only need to hit it once.

0
On

I had this issue using Bull in NestJs. https://docs.nestjs.com/techniques/queues

The exact error was:

(node:12887) UnhandledPromiseRejectionWarning: Error: Cannot define the same handler twice default

I was using the same name for different processors (consumers). So, they were more than one processor for a Bull queue.

My copy/paste mistake !

Once I defined different names for each processor and queue, everything was fixed.