完成所有作业后轮询Laravel队列 [英] Polling Laravel Queue after All Jobs are Complete
问题描述
是否存在确定作业/工人何时完成处理Laravel队列的最佳实践?我能想到的唯一方法是轮询作业表,以查看队列中何时没有更多作业.
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.
我面临的挑战是,我将定期将1000个作业分派到队列中,然后再分配1000个,然后再分配另一个.如果可能的话,我希望能够在每一批作业完成后触发一个事件.
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.
感谢任何建议或指示.
推荐答案
不,没有这样的功能.但是,通过侦听 Illuminate \ Queue \ Events \ Looping
事件(在Laravel 5.4之前为 illuminate.queue.looping
)并检查队列大小,可以轻松实现./p>
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.
}
}
}
这篇关于完成所有作业后轮询Laravel队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!