问题描述
是否有任何最佳实践来确定作业/工作人员何时完成处理 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.
我面临的挑战是,我会定期将 1,000 个作业分派到队列中,然后再过一段时间再分派 1,000 个,然后再分派另一个.如果可能的话,我希望能够在每批作业完成后触发一个事件.
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.
推荐答案
不,没有这样的功能.但是,通过侦听 IlluminateQueueEventsLooping
事件(在 Laravel 5.4 之前是 illuminate.queue.looping
)并检查队列大小,实现起来很简单.
No, there is no such functionality. However, it's simple to implement by listening for the IlluminateQueueEventsLooping
event (was illuminate.queue.looping
before Laravel 5.4) and check the queue size.
<?php
use IlluminateContractsEventsDispatcher;
use IlluminateQueueEventsLooping;
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 队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!