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...
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
}
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With