28
NovLaravel Queue Debouncer package allows any queue job or chain in your Laravel application to be debounced, meaning that no matter how many times it’s dispatched within the specified timeout window, it will only run once.
For example, imagine you dispatch a job to rebuild a cache every time a record is updated, but the job is resource intensive. Debouncing the job with a five-minute wait would ensure that the cache is only rebuilt once, five minutes after you finish making changes.
You can install this package via composer by running this command.
composer require mpbarlow/laravel-queue-debouncer
Debounced jobs can largely be treated like any other dispatched job. The debouncer takes two arguments, the actual $job
you want to run, and the $wait
period.
As with a regular dispatch, $job can be either a class implementing Illuminate\Foundation\Bus\Dispatchable
, a chain, or a closure. $wait
accepts any argument that the delay method on a dispatch accepts (i.e. a DateTimeInterface
or a number of seconds).
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.
There are several ways to use the debouncer:
Dependency Injection
use App\Jobs\MyJob;
use Mpbarlow\LaravelQueueDebouncer\Debouncer;
class MyController
{
public function doTheThing(Debouncer $debouncer)
{
$debouncer->debounce(new MyJob(), 30);
// The class is also invokable:
$debouncer(new MyJob(), 30);
}
}
Facade
use App\Jobs\MyJob;
use Mpbarlow\LaravelQueueDebouncer\Facade\Debouncer;
Debouncer::debounce(new MyJob, 30);
Helper function
use App\Jobs\MyJob;
debounce(new MyJob(), 30);
When monitoring the queue, you will see an entry for the package’s internal dispatcher each time the debounced job is queued, but the job itself will only run once when the final wait time has expired.
For example, assuming the following code was run at exactly 9 am:
class MyJob
{
use Dispatchable;
public function handle()
{
echo “Hello!\n”;
}
}
$job = new MyJob();
debounce($job, now()->addSeconds(5));
sleep(3);
debounce($job, now()->addSeconds(5));
sleep(3);
debounce($job, now()->addSeconds(5));
you should expect the following activity on your queue:
[2020-03-11 09:00:05][vHmqrBYeLtK3Lbiq5TsTZxBo2igaCZHC] Processing: Closure (DispatcherFactory.php:28)
[2020-03-11 09:00:05][vHmqrBYeLtK3Lbiq5TsTZxBo2igaCZHC] Processed: Closure (DispatcherFactory.php:28)
[2020-03-11 09:00:08][LXdzLvilh5qhew7akNDnibCjaXksG81X] Processing: Closure (DispatcherFactory.php:28)
[2020-03-11 09:00:08][LXdzLvilh5qhew7akNDnibCjaXksG81X] Processed: Closure (DispatcherFactory.php:28)
[2020-03-11 09:00:11][MnPIqk5fCwXjiVzuwPjkkOdPPBn0xR4d] Processing: Closure (DispatcherFactory.php:28)
[2020-03-11 09:00:11][MnPIqk5fCwXjiVzuwPjkkOdPPBn0xR4d] Processed: Closure (DispatcherFactory.php:28)
[2020-03-11 09:00:11][I2hvBoCB71qZQeD4umn5dd90zJUCAlJ5] Processing: App\Jobs\MyJob
Hello!
[2020-03-11 09:00:11][I2hvBoCB71qZQeD4umn5dd90zJUCAlJ5] Processed: App\Jobs\MyJob
For more details and source code, you can visit its complete documentation on Github.
Published at : 28-11-2022
I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.
Launch project