如何为 Laravel 部署重启主管?

How to restart supervisor for a Laravel deployment?

我目前使用 cron 作业每分钟调用 php artisan queue:work --once 来处理我的生产作业队列。

我想改用 supervisor 来处理我的队列。

supervisor-configuration 部分的文档中指出:

Since queue workers are long-lived processes, they will not pick up changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart command:

php artisan queue:restart

This command will instruct all queue workers to gracefully "die" after they finish processing their current job so that no existing jobs are lost. Since the queue workers will die when the queue:restart command is executed, you should be running a process manager such as Supervisor to automatically restart the queue workers.

最后一句没看懂。因此,假设我已经按照 here 所述安装并配置了主管,并且我通过 ssh 手动登录到服务器并启动了主管:

sudo supervisorctl start laravel-worker:*

我需要在部署时调用 php artisan queue:restart 吗?如果是这样,那么这只会杀死所有当前的工作人员,我如何告诉主管重新启动队列工作人员?在 php artisan queue:restart 之后的部署中是否需要调用 sudo supervisorctl restart laravel-worker:*

我为此苦苦挣扎了一段时间。这有点令人困惑,文档往往相互指向而不是解释整个系统的工作原理。

在您的服务器上安装 supervisor 的全部目的是在机器上的任何 Laravel 应用程序 运行 中自动执行 queue 过程。如果您查看链接到的帮助页面上的示例文件,它所做的只是进入特定的 Laravel 实例并启动队列。

supervisor启动时,相当于:

php artisan queue:start

在您的 Laravel 文件夹中。主管是运行进程。但是,您仍然可以通过 sudo supervisorctl restart laravel-worker:* 或单个 Laravel 文件夹中的 php artisan queue:restart 来控制重新启动队列。如果您使用 aritsan 命令手动重新启动队列,则无需向主管调用重新启动 - 这将是多余的。您可以通过重新启动队列并监视代码更改更新来对此进行测试,或者查看队列本身以查看所有作业是否重新启动。

所有这一切的 'gotcha' 是,如果您引入新代码并部署它,您必须记得为该实例重新启动队列。

要使事情变得更复杂,但最终要使其变得简单,请看一下 Laravel Horizon,它基本上以一种更易于维护的方式取代了主管。