laravel-queue-debouncer icon indicating copy to clipboard operation
laravel-queue-debouncer copied to clipboard

Define queue on which closure is dispatched

Open tomvo opened this issue 1 year ago • 1 comments

Hi! I would like to create a dedicated queue for the debounce closures. We're debouncing a lot of jobs and it's messing with the main queue. I've gone through the code and haven't seen an option to define the queue name on which the closure is pushed.

What do you think of this? Could probably be an optional argument to the debounce helper / trait as well as a config setting.

tomvo avatar Apr 29 '24 07:04 tomvo

Yeah, I like this idea. Is it something you want to work on a PR for?

If not, I'm happy to implement it myself, it just might be a little while before I can get to it.

mpbarlow avatar Apr 30 '24 07:04 mpbarlow

If I'm not taken wrong, this is already fully implemented, as the Debouncer::debounce() just returns a standard Illuminate\Foundation\Bus\PendingDispatch, same as for every regular ProcessFooBar::dispatch() call. So you could just do (including Laravel's newer support for enums as queue name):

Debouncer::debounce(new ProcessFooBar($something), 5)
    ->onQueue(QueueName::long);

onlime avatar Jan 29 '25 18:01 onlime

Ahh yes, you’re completely right, from the readme:

The debouncer returns an instance of Illuminate\Foundation\Bus\PendingDispatch, meaning the debouncing process itself may be assigned to a different queue or otherwise manipulated.

Thanks @onlime, I’m getting forgetful 🫠

mpbarlow avatar Jan 29 '25 22:01 mpbarlow

Oh, I was wrong! Not about answering this question, but my example was a bit wrong. If you call Debouncer::debounce(...)->onQueue(), we would just set the PendingDispatch of this packages instantiated Debouncer job, the one that takes care of debouncing the custom job. And for the Debouncer, it doesn't really matter on which queue it runs, as anyway it would not block anything, as its only task is to dispatch the effective custom job.

So, we just need to call the onQueue() (or directly set the $queue prop in the job class itself, which is also respected) like this:

Debouncer::debounce(
    new ProcessFooBar($something)->onQueue(QueueName::long), 5
);

thanks for pointing this out, @pascalbaljet

onlime avatar Jan 30 '25 16:01 onlime