Yii2 queue的隊列使用詳解
少廢話主要看文檔
yii2-queue 的使用
1.安裝
composer require --prefer-dist yiisoft/yii2-queue
2.配置,在 common/config/main.php 中配置
redis作為驅(qū)動
return [ 'bootstrap' => [ 'queue', // 把這個組件注冊到控制臺 ], 'components' => [ 'redis' => [ 'class' => \yii\redis\Connection::class, // ... ], 'queue' => [ 'class' => \yii\queue\redis\Queue::class, 'as log' => \yii\queue\LogBehavior::class,//錯誤日志 默認(rèn)為 console/runtime/logs/app.log 'redis' => 'redis', // 連接組件或它的配置 'channel' => 'queue', // Queue channel key ], ], ];
File 作為驅(qū)動
return [ 'bootstrap' => [ 'queue', // 把這個組件注冊到控制臺 ], 'components' => [ 'queue' => [ 'class' => \yii\queue\file\Queue::class, 'as log' => \yii\queue\LogBehavior::class,//錯誤日志 默認(rèn)為 console/runtime/logs/app.log 'path' => '@runtime/queue', ], ], ];
3.新建 frontend/components/DownloadJob
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)); } }
4.控制臺
控制臺用于監(jiān)聽和處理隊列任務(wù)。
cmd 下 監(jiān)聽隊列
yii queue/listen
5.添加到隊列
將任務(wù)添加到隊列:
Yii::$app->queue->push(new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
將任務(wù)推送到隊列中延時5分鐘運行:
Yii::$app->queue->delay(5 * 60)->push(new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
6.測試
執(zhí)行 5 中的程序,控制臺監(jiān)聽到,便會后臺自動 下載http://example.com/image.jpg到本地為/tmp/image.jpg
啟動worker
可以使用Supervisor或Systemd 來啟動多進程worker,也可以使用 Cron,我們這里主要說一下Supervisor
centos7 supervisor的使用
1.安裝supervisor
yum update yum install epel-release yum install -y supervisor #開機啟動 systemctl enable supervisord #啟動 systemctl start supervisord
2.supervisor 命令
supervisorctl status 查看進程狀態(tài) supervisorctl reload 重啟supervisord supervisorctl start|stop|restart 啟動關(guān)閉重啟進程
3.添加配置文件
Supervisor 配置文件通常在 /etc/supervisord.d 目錄下. 你可以創(chuàng)建一些配置文件在這里.
注:文件名是.ini結(jié)尾
下面就是個例子:
[program:yii-queue-worker] process_name=%(program_name)s_%(process_num)02d command=/usr/bin/php /var/www/my_project/yii queue/listen --verbose=1 --color=0 autostart=true autorestart=true user=www-data numprocs=4 redirect_stderr=true stdout_logfile=/var/www/my_project/log/yii-queue-worker.log
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Thinkphp 框架基礎(chǔ)之入口文件功能、定義與用法分析
這篇文章主要介紹了Thinkphp 框架基礎(chǔ)之入口文件功能、定義與用法,結(jié)合實例形式分析了Thinkphp入口文件基本功能、原理、定義與操作注意事項,需要的朋友可以參考下2020-04-04Laravel路由中不固定數(shù)量的參數(shù)如何實現(xiàn)?
最近在學(xué)習(xí)laravel的時候發(fā)現(xiàn)了一個有趣的地方,下面和大家分享下,這篇文章主要給大家介紹了關(guān)于Laravel路由中不固定數(shù)量的參數(shù)是如何實現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12ThinkPHP通過AJAX返回JSON的兩種實現(xiàn)方法
這篇文章主要介紹了ThinkPHP通過AJAX返回JSON的兩種實現(xiàn)方法,分析了ThinkPHP中內(nèi)置的ajaxReturn函數(shù)的用法,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12Zend Framework框架實現(xiàn)類似Google搜索分頁效果
這篇文章主要介紹了Zend Framework框架實現(xiàn)類似Google搜索分頁效果,結(jié)合實例形式分析了Zend Framework框架實現(xiàn)分頁效果所涉及的基本查詢、判斷與分頁效果構(gòu)造相關(guān)操作技巧,需要的朋友可以參考下2016-11-11Thinkphp中volist標(biāo)簽mod控制一定記錄的換行BUG解決方法
這篇文章主要介紹了Thinkphp中volist標(biāo)簽mod控制一定記錄的換行BUG解決方法,涉及針對標(biāo)簽執(zhí)行語句順序的修改,非常具有實用價值,需要的朋友可以參考下2014-11-11