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?
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 toqueue.process
is applied to the job, producing a result that can be accessed withawait 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.