如何在 Laravel 中检查队列的状态?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38813979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check the status of a queue in Laravel?
提问by Brendan White
I need to submit a number of jobs to a Laravel queueto process some uploaded CSV files. These jobs could be finished in one second if the files are small, or a few seconds if they're bigger, or possibly up to a minute if the CSV files are very big. And I can't tell in advance how big the files will be.
我需要向Laravel 队列提交一些作业来处理一些上传的 CSV 文件。如果文件很小,这些作业可以在一秒钟内完成,如果文件较大,则可以在几秒钟内完成,如果 CSV 文件非常大,则可能长达一分钟。而且我无法提前知道文件有多大。
When the user goes to the "results" page, I need to display the results - but only if the queue has finished the jobs. If the queue is still processing, I need to display a "try again later" message.
当用户进入“结果”页面时,我需要显示结果——但前提是队列已经完成了作业。如果队列仍在处理中,我需要显示“稍后再试”消息。
So - is there a way to check, from a controller, whether the queue has finished?
那么 - 有没有办法从控制器检查队列是否已完成?
I'm currently using Laravel 5.1 but would happily upgrade if that helps. And I'm currently using the database queue driver. Ideally I'd love to find a general technique that works for all queue drivers, but if the only way to do it is to check a database table then I guess that's what I have to do.
我目前正在使用 Laravel 5.1,但如果有帮助,我会很乐意升级。我目前正在使用数据库队列驱动程序。理想情况下,我很想找到一种适用于所有队列驱动程序的通用技术,但如果唯一的方法是检查数据库表,那么我想这就是我必须做的。
Thanks!
谢谢!
回答by Robert Norman
I know this is a year old, but why not create a new queue per upload with a unique key based on that request.
我知道这已经有一年了,但为什么不根据该请求使用唯一键为每次上传创建一个新队列。
$job = (new ProcessCSVJob($data))->onQueue($uniqueQueueName);
You can then simply either do a count in the database on the queue name field if you want a DB only solution.
如果您需要仅数据库的解决方案,您可以简单地在数据库中的队列名称字段中进行计数。
To work across all queue types you can use the Queue size method to return the queue size.
要处理所有队列类型,您可以使用 Queue size 方法返回队列大小。
$queue = App::make('queue.connection');
$size = $queue->size($uniqueQueueName);
This is in Laravel 5.4. Not sure how backwards compatible this is.
这是在 Laravel 5.4 中。不确定这是如何向后兼容的。