Polling Laravel Queue after All Jobs are Complete

本秂侑毒 提交于 2020-05-12 04:46:37

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!