Best method to manually poll for jobs? #462
-
I've just tried to implement a manual job getter, but it seems to return Before going further down this path, I wanted to check if this is a rational method to poll for jobs, or if there is a better way? Without any real benchmarking my concern is that this approach across hundreds of workers seems like it would be thrashing fairly hard with a sufficiently small (Note: The reason for requesting jobs manually is to allow workers to return a completed job result, but not accept a new job until a secondary process is triggered.) /**
* Loops on a heartbeat until a job lock is acquired.
*
* @param {BullWorker} worker
* @param {string} uuid
* @param {number} delay
* @return {Promise<SocketRequestJob>}
*/
export const acquireJob = async (
worker: BullWorker,
{ uuid, delay }: JobAcquisitionOptions
): Promise<SocketRequestJob> => {
// Set up the local variable to receive inbound jobs.
let job: SocketRequestJob | undefined
// Loop until a job lock is acquired.
while (job === undefined) {
job = (await worker.getNextJob(uuid)) || undefined
// If no job was acquired, delay for a short delay before trying again.
if (job === undefined) {
await waitForTimeout(delay)
}
}
// Return acquired job.
return job
}
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 8 replies
-
I'd have to devote a few brain cells, but my approach would be to leverage events: https://docs.bullmq.io/guide/events See the following test: bullmq/src/test/test_events.ts Line 29 in 297ec30 |
Beta Was this translation helpful? Give feedback.
-
You do not need a delay, since "getNextJob" is already a blocking operation that waits "drainDelay" before it expires. The default value for drainDelay is 5 seconds (note it is expressed in seconds not milliseconds). You can increase this value to reduce the loop interval. "getNextJob" is the most efficient way to get jobs from the queue, I do not recommend using events for this usecase. |
Beta Was this translation helpful? Give feedback.
-
@manast Both the server and the worker (appear to be) connected to the |
Beta Was this translation helpful? Give feedback.
-
Just to close this out for anyone stumbling across this in the future, I misunderstood @manast explanation around the The delay isn't related to a "polling" cycle, it refers to how long the worker stays connected to the queue waiting for a job before giving up. So a long number = waiting for a job for a long time (which is what I wanted in this situation). |
Beta Was this translation helpful? Give feedback.
You do not need a delay, since "getNextJob" is already a blocking operation that waits "drainDelay" before it expires. The default value for drainDelay is 5 seconds (note it is expressed in seconds not milliseconds). You can increase this value to reduce the loop interval. "getNextJob" is the most efficient way to get jobs from the queue, I do not recommend using events for this usecase.