使用Supervisor托管Laravel队列进程

supervisor的安装(Ubuntu 系统)

sudo apt-get install supervisor

supervisor的配置

一般配置文件在/etc/supervisor/conf.d 目录下
也可以运行这个命令可以生成一个默认的配置文件:

echo_supervisord_conf > /etc/supervisor/supervisord.conf

生成成功后,打开编辑这个文件,把最后的 include 块的注释打开,并修改如下:

[include]files = /etc/supervisor/*.conf

新增的 Supervisor 配置文件放在 /etc/supervisor 目录下,并且以 conf 结尾。

这时我们使用新的配置文件来启动 Supervisor:

supervisord -c /etc/supervisord.conf

如果提示已经有进程在运行,那么先 kill 掉它。

supervisor托管队列进程

首先在 /etc/supervisor 目录下新增一个 Supervisor 的配置文件,如下:
文件名laravel-worker.conf,和配置文件中指定的program保持一致

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/bin/php7 /home/vagrant/code/ccerp-v2/artisan queue:work --tries=3
autostart=true
autorestart=true
user=vagrant
numprocs=8
redirect_stderr=true
stdout_logfile=/var/log/supervisor/laravel-queue.log

这里 user 填写网站运行进程的用户,如 vagrant,numprocs 表示启动多少个进程来监听 Laravel 队列。
一切就绪后,我们使用如下命令就可以启动队列进程的监听了:

注意: 修改了配置文件以后都要进行 reload 和 update

supervisord # 先执行
sudo supervisorctl reload
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
// 或者
sudo supervisorctl start all

在这一步,可能发生错误,提示如下:

laravel-worker:laravel-worker_00: ERROR (spawn error)
laravel-worker:laravel-worker_01: ERROR (spawn error)
laravel-worker:laravel-worker_02: ERROR (spawn error)
laravel-worker:laravel-worker_03: ERROR (spawn error)
laravel-worker:laravel-worker_04: ERROR (spawn error)
laravel-worker:laravel-worker_05: ERROR (spawn error)
laravel-worker:laravel-worker_06: ERROR (spawn error)
laravel-worker:laravel-worker_07: ERROR (spawn error)

解决过程:

  1. 把 Supervisor 的日志文件,和新增的队列配置文件中的日志文件,用 chown 把用户和组设置为正确的,如本例是 chown vagrant:vagrant file_name,把日志文件权限设置为 777。
  2. 另外一个问题,artisan执行报错,路径不能用相对路径,要用绝对路径(比如 /home/vagrant/Code/laravel/artisan,不能写成 ~/Code/laravel/artisan)

再次经过上述步骤,成功开启进程管理:

aravel-worker:laravel-worker_00: started
laravel-worker:laravel-worker_01: started
laravel-worker:laravel-worker_02: started
laravel-worker:laravel-worker_03: started
laravel-worker:laravel-worker_04: started
laravel-worker:laravel-worker_05: started
laravel-worker:laravel-worker_06: started
laravel-worker:laravel-worker_07: started

發表回覆

你的電郵地址並不會被公開。 必要欄位標記為 *