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 來啟動多進(jìn)程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 查看進(jìn)程狀態(tài) supervisorctl reload 重啟supervisord supervisorctl start|stop|restart 啟動關(guān)閉重啟進(jì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)文章
PHP獲取IP地址所在地信息的實例(使用純真IP數(shù)據(jù)庫qqwry.dat)
下面小編就為大家?guī)硪黄狿HP獲取IP地址所在地信息的實例(使用純真IP數(shù)據(jù)庫qqwry.dat)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法
這篇文章主要介紹了thinkPHP導(dǎo)出csv文件及用表格輸出excel的方法,涉及thinkPHP針對表格與Excel文件的操作技巧,需要的朋友可以參考下2015-12-12
tp5.1 框架數(shù)據(jù)庫-數(shù)據(jù)集操作實例分析
這篇文章主要介紹了tp5.1 框架數(shù)據(jù)庫-數(shù)據(jù)集操作,結(jié)合實例形式分析了tp5.1 框架數(shù)據(jù)庫查詢結(jié)果數(shù)據(jù)集獲取、遍歷相關(guān)操作技巧與使用注意事項,需要的朋友可以參考下2020-05-05
php微信公眾號開發(fā)(3)php實現(xiàn)簡單微信文本通訊
這篇文章主要介紹了php微信公眾號開發(fā)第三課,php實現(xiàn)簡單微信文本通訊,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12

