背景
Laravel服务端开发,涉及到使用队列异步处理数据,需要使用到supervisor来守护队列进程
安装准备
由于supervisor基于python环境,先使用anaconda创建一个独立环境,来安装
# 创建虚拟环境php_test
conda create -n php_test python=3.9
# 安装过程中会提示有其他安装包将会被安装,这个时候要输入“y”,继续安装
# 切换到刚刚创建的虚拟环境
conda activate php_test
开始安装
基础安装
# 在刚刚创建的python虚拟环境php_test中
pip install supervisor
mkdir /etc/supervisor
# 生成supervisor配置文件
echo_supervisord_conf > /etc/supervisor/supervisord.conf
mkdir -p /etc/supervisor/conf.d
# 为了方便执行supervisord 和 supervisorctl命令,这里先创建两条软连接
ln -s /usr/local/anaconda3/envs/php_test/bin/supervisord /usr/bin/supervisord
ln -s /usr/local/anaconda3/envs/php_test/bin/supervisorctl /usr/bin/supervisorctl
# 上面的/usr/local/anaconda3/envs/php_test/要根据你实际的虚拟环境做更改
配置
# 修改 supervisord.conf 配置
vim /etc/supervisor/supervisord.conf
# 添加如下内容
[include]
files = /etc/supervisor/conf.d/*.conf
创建具体应用的配置文件
# 添加一个laravel-worker.conf配置文件
vim /etc/supervisor/conf.d/laravel-worker.conf
# 内容如下:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /data/www/wwwroot/blog/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=root
numprocs=1 #队列进程数
redirect_stderr=true
stdout_logfile=/data/www/wwwroot/blog/storage/logs/workers.log
注意,/data/www/wwwroot/blog/要替换成自己项目的绝对路径
给配置文件执行权限
chmod -R +x /etc/supervisor/conf.d
使用
启动supervisor
supervisord -c /etc/supervisor/supervisord.conf
依次执行如下命令,启动laravel-worker
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
验证是否正常启动了
supervisorctl
# 如果结果如下,说明启动成功(注意看:RUNNING)
(php_test) [root@ptuhlc ~]# supervisorctl
laravel-worker:laravel-worker_00 RUNNING pid 30052, uptime 4:08:13
命令合集
# 启动
supervisord -c /etc/supervisor/supervisord.conf
# 关闭
supervisorctl shutdown
# 重载配置
supervisorctl reload
# 其他客户端命令
supervisorctl status #查看程序状态
supervisorctl stop theprogramname # 停止 theprogramname
supervisorctl start theprogramname # 启动theprogramname
supervisorctl restart theprogramname # 重启
supervisorctl reread # 读取有更新(增加)的配置文件,不会启动新添加的程序
supervisorctl update # 重启配置文件修改过的程序
设置开机启动
创建服务:vim supervisord.service
内容如下:
# supervisord service for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[Unit]
Description=Supervisor daemon
[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl $OPTIONS shutdown
ExecReload=/usr/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
supervisord.service 代码仓库: https://github.com/Supervisor/initscripts/blob/master/centos-systemd-etcs
加入开机启动项
cp supervisord.service /usr/lib/systemd/system/supervisord.service
systemctl enable supervisord
systemctl is-enabled supervisord
之后即可愉快地使用命令:
systemctl start supervisord
systemctl stop supervisord
systemctl reload supervisord
systemctl status supervisord
来快速管理supervisord服务了。
最后啰嗦一句:
一般情况下,我们会有多个项目地队列需要管理,直接使用命令
supervisorctl
可以查看所有项目的队列服务状态
如下: