1. .env文件修改QUEUE_DRIVER=redis (QUEUE_CONNECTION)
2. queue.php database.php可以保持默认不做修改
(注意:在queue.php文件里面redis quque=>{default} 大括号必须加)
3. composer require "predis/predis ~1.0" 安装扩展
4. php artisan make:job SendReminderEmail 生成任务类,在app目录下生成jobs目录
5. SendReminderEmail::dispatch($post) 在控制器中利用dispatch添加redis队列
6. php artisan queue:work 命令行执行redis队列
后台执行队列需要安装Supervisor
# sudo su - #切换为root用户
# yum install epel-release
# yum install -y supervisor
# systemctl enable supervisord # 开机自启动
# systemctl start supervisord # 启动supervisord服务
# systemctl status supervisord # 查看supervisord服务状态
# ps -ef|grep supervisord # 查看是否存在supervisord进程
配置Supervisor
1.修改 /etc/supervisord.conf
文件内容
files = /etc/supervisord.d/*.conf
2.在 /etc/supervisord.d/
下创建并且以 conf 结尾的文件,添加内容如下:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/php/bin/php /home/wwwroot/cyvideo/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=root
numprocs=8
redirect_stderr=true
stdout_logfile=/home/wwwroot/worker.log
3.执行命令使配置文件生效
supervisorctl reread
supervisorctl update
4.重启 supervisord
supervisorctl reload
5.运行
supervisorctl start laravel-worker:*
6.查看运行状态
supervisorctl status
//提示RUNNING代表成功
常用命令
service supervisord start #启动
service supervisord stop #停止
service supervisord status #状态
supervisorctl reread
supervisorctl update
supervisorctl reload
supervisorctl status
supervisorctl shutdown #关闭所有任务
supervisorctl stop|start program_name #启动或停止服务
supervisorctl status #查看所有任务状态
至此 你添加到redis的队列任务就会自动执行