问题
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.
回答1:
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.
}
}
}
回答2:
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
来源:https://stackoverflow.com/questions/41228745/polling-laravel-queue-after-all-jobs-are-complete