Yii2 queue的隊(duì)列使用詳解
少廢話主要看文檔
yii2-queue 的使用
1.安裝
composer require --prefer-dist yiisoft/yii2-queue
2.配置,在 common/config/main.php 中配置
redis作為驅(qū)動(dòng)
return [
'bootstrap' => [
'queue', // 把這個(gè)組件注冊(cè)到控制臺(tái)
],
'components' => [
'redis' => [
'class' => \yii\redis\Connection::class,
// ...
],
'queue' => [
'class' => \yii\queue\redis\Queue::class,
'as log' => \yii\queue\LogBehavior::class,//錯(cuò)誤日志 默認(rèn)為 console/runtime/logs/app.log
'redis' => 'redis', // 連接組件或它的配置
'channel' => 'queue', // Queue channel key
],
],
];
File 作為驅(qū)動(dòng)
return [
'bootstrap' => [
'queue', // 把這個(gè)組件注冊(cè)到控制臺(tái)
],
'components' => [
'queue' => [
'class' => \yii\queue\file\Queue::class,
'as log' => \yii\queue\LogBehavior::class,//錯(cuò)誤日志 默認(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.控制臺(tái)
控制臺(tái)用于監(jiān)聽和處理隊(duì)列任務(wù)。
cmd 下 監(jiān)聽隊(duì)列
yii queue/listen
5.添加到隊(duì)列
將任務(wù)添加到隊(duì)列:
Yii::$app->queue->push(new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
將任務(wù)推送到隊(duì)列中延時(shí)5分鐘運(yùn)行:
Yii::$app->queue->delay(5 * 60)->push(new frontend\components\DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
6.測試
執(zhí)行 5 中的程序,控制臺(tái)監(jiān)聽到,便會(huì)后臺(tái)自動(dòng) 下載http://example.com/image.jpg到本地為/tmp/image.jpg
啟動(dòng)worker
可以使用Supervisor或Systemd 來啟動(dòng)多進(jìn)程worker,也可以使用 Cron,我們這里主要說一下Supervisor
centos7 supervisor的使用
1.安裝supervisor
yum update yum install epel-release yum install -y supervisor #開機(jī)啟動(dòng) systemctl enable supervisord #啟動(dòng) systemctl start supervisord
2.supervisor 命令
supervisorctl status 查看進(jìn)程狀態(tài) supervisorctl reload 重啟supervisord supervisorctl start|stop|restart 啟動(dòng)關(guān)閉重啟進(jìn)程
3.添加配置文件
Supervisor 配置文件通常在 /etc/supervisord.d 目錄下. 你可以創(chuàng)建一些配置文件在這里.
注:文件名是.ini結(jié)尾
下面就是個(gè)例子:
[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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
PHP獲取IP地址所在地信息的實(shí)例(使用純真IP數(shù)據(jù)庫qqwry.dat)
下面小編就為大家?guī)硪黄狿HP獲取IP地址所在地信息的實(shí)例(使用純真IP數(shù)據(jù)庫qqwry.dat)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-11-11
PHP輸出英文時(shí)間日期的安全方法(RFC 1123格式)
這篇文章主要介紹了PHP輸出英文時(shí)間日期的安全方法,本文所指的英文時(shí)間日期為RFC 1123格式,總結(jié)了一個(gè)不受setlocale影響的函數(shù)gmdate,需要的朋友可以參考下2014-06-06
thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
這篇文章主要介紹了thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法,涉及thinkPHP針對(duì)表格與Excel文件的操作技巧,需要的朋友可以參考下2015-12-12
tp5.1 框架數(shù)據(jù)庫-數(shù)據(jù)集操作實(shí)例分析
這篇文章主要介紹了tp5.1 框架數(shù)據(jù)庫-數(shù)據(jù)集操作,結(jié)合實(shí)例形式分析了tp5.1 框架數(shù)據(jù)庫查詢結(jié)果數(shù)據(jù)集獲取、遍歷相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-05-05
php微信公眾號(hào)開發(fā)(3)php實(shí)現(xiàn)簡單微信文本通訊
這篇文章主要介紹了php微信公眾號(hào)開發(fā)第三課,php實(shí)現(xiàn)簡單微信文本通訊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
laravel利用中間件防止未登錄用戶直接訪問后臺(tái)的方法
今天小編就為大家分享一篇laravel利用中間件防止未登錄用戶直接訪問后臺(tái)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-09-09
ThinkPHP實(shí)現(xiàn)帶驗(yàn)證碼的文件上傳功能實(shí)例
這篇文章主要介紹了ThinkPHP實(shí)現(xiàn)帶驗(yàn)證碼的文件上傳功能,實(shí)例相關(guān)類的導(dǎo)入與調(diào)用步驟,是非常實(shí)用的技巧,需要的朋友可以參考下2014-11-11

