Reputation: 759
Are there any best practices for identifying when jobs/workers finish processing a Laravel queue? The only approach I can think of is to poll the jobs table to see when there are no more jobs in the queue.
The challenge I have is that I'll periodically dispatch 1,000 jobs to the queue and then some time later another 1,000 and then another. I'd like to be able to trigger an event once each batch of jobs is complete, if possible.
Thanks for any suggestions or pointers.
Upvotes: 3
Views: 4148
Reputation: 1584
kregus comment is very true on the accepted answer
"once the queue has finished processing its jobs and is empty, the //Trigger your event part of the above code will keep firing. It won't just trigger once"
To trigger the event once use that. In the EventServiceProvider
public function boot()
{
Queue::after(function (JobProcessed $event) {
if(Queue::size($queueName)==0){
//trigger your event
}
});
}
Upvotes: 0
Reputation: 19781
No, there is no such functionality. However, it's simple to implement by listening for the Illuminate\Queue\Events\Looping
event (was illuminate.queue.looping
before Laravel 5.4) and check the queue size.
<?php
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Queue\Events\Looping;
class QueueSizeCheckerAndEventThingieSubscriber {
public function subscribe(Dispatcher $events) {
$events->listen(Looping::class, self::class . '@onQueueLoop'); // >= 5.4
$events->listen('illuminate.queue.looping', self::class . '@onQueueLoop'); // < 5.4
}
public function onQueueLoop() {
$queueName = 'my-queue';
$queueSize = Queue::size($queueName);
if ($queueSize === 0) {
// Trigger your event.
}
}
}
Upvotes: 1
Reputation: 1687
I think preventOverlapping will be best suited for your need.
https://laravel.com/docs/5.3/scheduling#preventing-task-overlaps
I am already using it with jobs with beanstalkd and supervisor running fine. Never had an issue till
Upvotes: 0