bullmq icon indicating copy to clipboard operation
bullmq copied to clipboard

Expose API to get delayed waiting job count

Open urossmolnik opened this issue 1 year ago • 3 comments

Is your feature request related to a problem? Please describe.

BullMQ queue currently exposes getWaitingCount() that can be used to monitor queue length. What we noticed is this method does include delayed jobs that should already be executed.

Describe the solution you'd like

Add ability to get number of delayed jobs that should already be executed by extending getWaitingCount(includeDelayedWaiting?) or adding a separate getDelayedWaitingCount() method to queue.

Describe alternatives you've considered

I did not found any other performant way of getting this count.

urossmolnik avatar Jul 25 '24 21:07 urossmolnik

hi @urossmolnik we have this method https://api.docs.bullmq.io/classes/v5.Queue.html#getJobCountByTypes, you could use it like: const count = await queue.getJobCountByTypes('waiting','delayed');

roggervalf avatar Jul 26 '24 01:07 roggervalf

Hi @roggervalf,

Aware of getJobCountByTypes, but this is not something that covers our use case. What we want to be able to get is number of delayed jobs that should already be processed but are not because queue is backed up. queue.getJobCountByTypes('waiting','delayed') will just return number of waiting + delayed jobs.

To explain with example

    await queue.add("test", { random: Math.random() }, {});
    await queue.add("test", { random: Math.random() }, { delay: 1000 });
    await queue.add("test", { random: Math.random() }, { delay: 10000 });

    await wait(2000);

    const waitingCount = await queue.getWaitingCount(); 
    // Waiting count is 1, which is ok as this ignores delayed jobs

    const delayedWaiting = ???;
    // How to get number of delayed jobs that should already be executed.
    // In this case it should return number 1.

urossmolnik avatar Jul 26 '24 02:07 urossmolnik

@roggervalf I think we need a new method or expand the current getDelayedCount so that it takes in a "timestamp" and returns the count of all the delayed jobs with a smaller timestamp. So for example, if we call it like this:

const count await = getDelayedCount( Date.now() )

It would return the count of all delayed jobs with a scheduled timestamp smaller than "now" and therefore returning the count of jobs that should be executed as soon as possible when a worker is available.

manast avatar Jul 26 '24 07:07 manast