One job at a time in workers? #2761
-
index.mjs: import {Worker} from 'node:worker_threads'
import {cpus} from 'os'
cpus().forEach(() => { // 2 cpus
const worker = new Worker('./worker.mjs');
}) worker.mjs import Queue from 'bull';
import {setTimeout} from 'timers/promises'
const queue = new Queue('queue', "redis://127.0.0.1:6379");
queue.process(async (job) => {
console.log("start")
await setTimeout(5000);
console.log("end");
return {done: "Done!"}
})
queue.add({}) This responds like this: start // worker 1
start // worker 2
end // worker 1
end //worker 2 They dont run one at a time. How could I fix this? It should return like this: start // worker 1
end // worker 1
start // worker 2
end // worker 2
I currently serve an express server in multiple workers to load balance my server. I would like to also add this queuing system but be able to run the job in the worker where it was started from. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
hi @SupertigerDev, what you are experiencing is expected as you are creating 2 workers, each worker has a concurrency value 1 by default. Each of them will pick 1 job to be processed. Looks like what you are looking for is a global concurrency, I highly recommend you to use bullmq package https://docs.bullmq.io/guide/queues/global-concurrency where this feature is available |
Beta Was this translation helpful? Give feedback.
hi @SupertigerDev, what you are experiencing is expected as you are creating 2 workers, each worker has a concurrency value 1 by default. Each of them will pick 1 job to be processed. Looks like what you are looking for is a global concurrency, I highly recommend you to use bullmq package https://docs.bullmq.io/guide/queues/global-concurrency where this feature is available