Laravel Artisan CLI安全地停止守护程序队列工作者 [英] Laravel Artisan CLI safely stop daemon queue workers

查看:568
本文介绍了Laravel Artisan CLI安全地停止守护程序队列工作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了处理大量作业,我根据要完成的工作量运行可变数量的队列工作器.我不想雇用过多的工人来完成在我们认为合适的时间段内需要完成的工作.

In order to process large numbers of jobs, I run a variable number of queue workers depending on howmuch work there is to complete. I don't want to run more workers than are necessary to complete the work that needs to be done in a time period that we deem appropriate.

目前,我出于测试目的启动了5个守护程序队列工作器,但是在生产中,该数目可能在25到25之间. 100名工人,可能还会更多.我了解在部署时,我必须先通过使用php artisan down将框架置于维护模式来停止队列工作程序,因为--daemon标志导致框架仅在工作程序启动时才加载框架,因此新代码不会在部署期间生效,直到工作线程重新启动.

At the moment, I start 5 daemon queue workers for testing purposes, however in production this number could be between 25 & 100 workers, possibly more. I understand that when deploying, I have to stop the queue workers by first placing the framework in maintenance mode by using php artisan down, because the --daemon flag causes the framework to only load when the worker starts up, hence new code would not take affect during the deploy until the worker restarts.

如果我由于某种原因需要停止工作,我可以使用php artisan down将应用程序置于维护模式,这将导致工作人员在完成当前工作(如果正在工作)后死亡.但是,有时我可能想杀死工人,而没有将整个应用程序置于维护模式.

If i needed to stop the workers for some reason, I could put the application in maintenance mode using php artisan down which will cause the workers to die once they have finished processing their current job (if they are working one). However, there may be times where I want to kill the workers without putting the whole application in maintenance mode.

是否存在一种安全的方法来停止工作人员,使其继续处理当前工作,然后死亡,而无需将整个应用程序置于维护模式?

基本上,我需要的是php artisan queue:stop,它的行为类似于php artisan queue:restart,但是一旦完成工作就不会重新启动工作程序.

Essentially what I need is a php artisan queue:stop, which behaves like php artisan queue:restart, but does not restart the worker once the job is done.

我期望有一个类似php artisan queue:stop的命令可以执行此操作,但事实并非如此.

I was expecing there to be a like php artisan queue:stop command that would do this, but that doesn't seem to be the case.

使用ps aux | grep php我可以获取工作人员的进程ID,并且可以通过这种方式终止该进程,但是我不想在正在工作的过程中终止该进程.

Using ps aux | grep php I am able to get the process id's for the workers, and I could kill the processes that way, but I don't want to kill the process in the middle of it working on a job.

谢谢.

推荐答案

我们已经在应用程序中实现了类似的东西-但这并不是Laravel本身内置的.您必须编辑此文件 ,方法是在if块中添加另一个条件,以便它将调用stop函数.您可以通过在Worker类中设置一个静态变量来执行此操作,该类在每次运行必须执行的自定义命令时都会更改(即php artisan queue:pause),或者通过在某个位置检查原子值(即在其中设置它)一些缓存,例如redis,memcached,APC甚至MySQL,尽管这意味着您使用相同的自定义命令设置的while循环的每个周期都会有一个MySQL查询.

We've implemented something like this in our application - but it was not something that was built-in to Laravel itself. You would have to edit this file, by adding another condition to the if-block so that it would call the stop function. You can do this by either setting a static variable in the Worker class that gets changed whenever you run a custom command that you'll have to make (i.e. php artisan queue:pause) or by checking an atomic value somewhere (i.e. set it in some cache like redis, memcached, APC or even MySQL, though this would mean you'll have one MySQL query for every cycle of this while-loop) that you set using the same custom command.

这篇关于Laravel Artisan CLI安全地停止守护程序队列工作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆