Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BullMQ: How to attach one worker with multiple queues in nodejs using bullmq?

Tags:

node.js

bullmq

I have to utilize one worker for more than one Queue. Is this possible in bullmq? As I went through the documentation I found option to provide only one queue name for worker. Is there any way using that I can allocate single worker to process multiple queues?

Thanks...

like image 978
Lalit Kolate Avatar asked Oct 15 '25 09:10

Lalit Kolate


2 Answers

You can subscribe the worker to one queue
but when publishing a job give it a specific name to distinguish between different handlers.

For instance here's how you can publish jobs with different name to the same worker:

const queue = new Queue('math');
await queue.add('factorial', { number: 42 });
await queue.add('check_prime', { number: 31 });

And here's how you can subcribe and process them:

const worker = new Worker('math', async (job) => {
  if (job.name === 'factorial') {
    // Do something
  } else if (job.name === 'check_prime') {
    // Do something else
  }
});
like image 106
Pavel Bely Avatar answered Oct 18 '25 00:10

Pavel Bely


You can reuse a processor that goes into the worker across different queues but not the worker itself.

const processor = (job) => {
  if(job.queueName === 'Queue1') {
    // Job logic for Queue1
  }else {
    // Job logic for Queue2
  }
}

And then create two worker instances for each queue with the same processor.

const worker1 = new Worker('Queue1', processor, { connection });
const worker2 = new Worker('Queue2', processor, { connection });

All though this is possible I would recommend keeping the worker logic separate for each queue as the only advantage of this flow would be that you save a couple of lines of code and add in a lot more complexity to the code.

like image 31
Dan Philip Avatar answered Oct 17 '25 23:10

Dan Philip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!