Yii2 queue的队列使用详解
框架(架构)  /  管理员 发布于 6年前   157
少废话主要看文档 官方文档 yii2-queue 的使用 1.安装 2.配置,在 common/config/main.php 中配置 redis作为驱动 File 作为驱动 3.新建 frontend/components/DownloadJob 4.控制台 控制台用于监听和处理队列任务。 cmd 下 监听队列 5.添加到队列 将任务添加到队列: 将任务推送到队列中延时5分钟运行: 6.测试 执行 5 中的程序,控制台监听到,便会后台自动 下载http://example.com/image.jpg到本地为/tmp/image.jpg 启动worker 可以使用Supervisor或Systemd 来启动多进程worker,也可以使用 Cron,我们这里主要说一下Supervisor centos7 supervisor的使用 1.安装supervisor 2.supervisor 命令 3.添加配置文件 Supervisor 配置文件通常在 /etc/supervisord.d 目录下. 你可以创建一些配置文件在这里. 注:文件名是.ini结尾 下面就是个例子: 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。composer require --prefer-dist yiisoft/yii2-queue
return [ 'bootstrap' => [ 'queue', // 把这个组件注册到控制台 ], 'components' => [ 'redis' => [ 'class' => \yii\redis\Connection::class, // ... ], 'queue' => [ 'class' => \yii\queue\redis\Queue::class, 'as log' => \yii\queue\LogBehavior::class,//错误日志 默认为 console/runtime/logs/app.log 'redis' => 'redis', // 连接组件或它的配置 'channel' => 'queue', // Queue channel key ], ],];
return [ 'bootstrap' => [ 'queue', // 把这个组件注册到控制台 ], 'components' => [ 'queue' => [ 'class' => \yii\queue\file\Queue::class, 'as log' => \yii\queue\LogBehavior::class,//错误日志 默认为 console/runtime/logs/app.log 'path' => '@runtime/queue', ], ],];
class DownloadJob extends BaseObject implements \yii\queue\JobInterface{ public $url; public $file; public function execute($queue) { file_put_contents($this->file, file_get_contents($this->url)); }}
yii queue/listen
Yii::$app->queue->push(new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg',]));
Yii::$app->queue->delay(5 * 60)->push(new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg',]));
yum updateyum install epel-releaseyum install -y supervisor#开机启动systemctl enable supervisord#启动systemctl start supervisord
supervisorctl status 查看进程状态 supervisorctl reload 重启supervisord supervisorctl start|stop|restart 启动关闭重启进程
[program:yii-queue-worker]process_name=%(program_name)s_%(process_num)02dcommand=/usr/bin/php /var/www/my_project/yii queue/listen --verbose=1 --color=0autostart=trueautorestart=trueuser=www-datanumprocs=4redirect_stderr=truestdout_logfile=/var/www/my_project/log/yii-queue-worker.log
您可能感兴趣的文章:
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
Copyright·© 2019 侯体宗版权所有·
粤ICP备20027696号