Polling Laravel Queue after All Jobs are Complete(在所有作业完成后轮询 Laravel 队列)
问题描述
是否有任何最佳实践来确定作业/工作人员何时完成处理 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 队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在所有作业完成后轮询 Laravel 队列
基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 如何替换eregi() 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
