如何使用 beanstalkd 启动 Laravel 队列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17407304/
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 to fire Laravel Queues with beanstalkd
提问by Tyrone
I'm pretty new to the whole Queue'd jobs thing in Laravel 4. I have some process heavy tasks I need the site to run in the background after being fired by the user doing a particular action.
我对 Laravel 4 中的整个 Queue'd 作业很陌生。我有一些处理繁重的任务,我需要网站在被用户执行特定操作后在后台运行。
When I was doing the local development for my site I was using this:
当我为我的网站进行本地开发时,我使用的是:
Queue::push('JobClass', array('somedata' => $dataToBeSent));
And I was using the local "sync" driver to do it. (The jobs would just automatically fire, impacting on the user experience but I assumed when going into the production phase I could switch it to beanstalkd and they would then be run in the background)
我正在使用本地“同步”驱动程序来做到这一点。(作业会自动触发,影响用户体验,但我假设在进入生产阶段时,我可以将其切换到 beanstalkd,然后它们将在后台运行)
Which brings me to where I'm at now. I have beanstalkd set up with the dependencies installed with composer and the beanstalkd process listening for new jobs. I installed a beanstalk admin interface and can see my jobs going into the queue, but I have no idea how to actually get them to run!
这让我回到了现在的位置。我已经使用 composer 安装的依赖项设置了 beanstalkd,并且 beanstalkd 进程侦听新作业。我安装了 beanstalk 管理界面,可以看到我的作业进入队列,但我不知道如何真正让它们运行!
Any help would be apprieciated, thanks!
任何帮助将不胜感激,谢谢!
回答by Nico Kaag
This is actually a really badly documented feature in Laravel.
这实际上是 Laravel 中一个非常糟糕的记录功能。
What you actually need to do is have the JobClass.php in a folder that is auto-loaded, I use app/commands, but they can also be in app/controllers or app/models if you like. And this function needs to have a fire event that takes the $job and $data argument.
您实际需要做的是将 JobClass.php 放在自动加载的文件夹中,我使用 app/commands,但如果您愿意,它们也可以在 app/controllers 或 app/models 中。这个函数需要有一个带 $job 和 $data 参数的 fire 事件。
To run these, simply execute php artisan queue:listen --timeout=60
in your terminal, and it will be busy emptying the queue, until it's empty, or it's been running for longer then 60 seconds. (Small note: The timeout is the time-limit to starta queue, so it may run for 69 seconds if 1 job takes 10 seconds.
要运行这些,只需php artisan queue:listen --timeout=60
在您的终端中执行,它将忙于清空队列,直到它为空,或者它已运行超过 60 秒。(小注意:超时是启动队列的时间限制,因此如果1个作业需要10秒,它可能会运行69秒。
If you only want to run 1 job (perfect for testing), run php artisan queue:work
如果您只想运行 1 个作业(非常适合测试),请运行 php artisan queue:work
There are tools like Supervisordthat make sure your job handlers keep running, but I recommend to just make a Cron task that starts every X minutes based on how fast the data needs to be processed, and on how much data comes in.
有像Supervisord这样的工具可以确保您的作业处理程序继续运行,但我建议只根据需要处理数据的速度和传入的数据量,每 X 分钟启动一个 Cron 任务。
回答by Jason Brodie
Keep in mind you need to path your artisan.
请记住,您需要找到您的工匠。
php /some/path/to/artisan queue:work
php /some/path/to/artisan queue:work