Yii2队列拓展 yii2-queue配置使用

composer安装

php composer.phar require --prefer-dist yiisoft/yii2-queue

或者在composer.json添加:

"yiisoft/yii2-queue": "~2.0.0"

项目配置

注册queue组件,redis配置,queue配置以及指定queue的驱动方式为redis

return [  
    'bootstrap'=> [
        'log',
        'queue',
    ],

    'components'=> [
        'redis'=>[
            'class'=>'yiiredisConnection',
            'hostname'=>'127.0.0.1',
            'port'=>6379,
            'database'=>0,
        ],

        'queue'=> [
            'class'=> yiiqueueredisQueue::class,
            'redis'=>'redis',// Redis connection component or its config
            'channel'=>'queue',// Queue channel key queue在redis中key的前缀
            'as log'=> yiiqueueLogBehavior::class,
        ],
    ],
];    

使用queue

定义类:

需要被发送到队列的任务应该定义为一个类,集成自yiibaseObject 实现yiiqueueJob接口(必须实现execute方法,实现该任务的动作)

class MailJobextends yiibaseObject implementsyiiqueueJob
{

    public$subject='这是邮箱标题';  
    public$body='<br>  
    邮箱主体<br>  
    time:';
    public$setTo= [];
    
    public functionexecute($queue)
    {
        //队列实例需要执行的代码体
        $mail= Yii::$app->mailer->compose();
        $mail->setTo($this->setTo);
        $mail->setSubject($this->subject);
        $this->body.= date('Y-m-d H:i:s',time());
        //$mail->setTextBody('zheshisha ');  //发布纯文字文本
        $mail->setHtmlBody($this->body);//发布可以带html标签的文本
        
        if($mail->send()) {
            echo"success";
        } else {
            echo"false";
        }
        //TODO: Implement execute() method.
        
    }

}

操作队列:

    //将作业推送到队列并获得其ID  
    $id= Yii::$app->queue->delay()->push(newMailJob([
        'setTo'=>'xxxx@gmail.com',
    ]));
    以下为获取该队列状态:
    
    //这个作业等待执行。
    Yii::$app->queue->isWaiting($id);
    // Worker从队列获取作业,并执行它。
    Yii::$app->queue->isReserved($id);
    // Worker作业执行完成。
    Yii::$app->queue->isDone($id);

控制台命令

控制台用于监听和处理队列任务。  
yii queue/listen [wait] 
//listen命令启动一个守护进程,它可以无限查询队列。如果有新的任务,他们立即得到并执行。 
//wait是下一次查询队列的时间 当命令正确地通过[supervisor](https://link.jianshu.com?t=https://github.com/yiisoft/yii2-queue/blob/master/docs/guide-zh-CN/worker.md#supervisor)来实现时,这种方法是最有效的。


yii queue/run
//run命令获取并执行循环中的任务,直到队列为空。适用与[cron](https://link.jianshu.com?t=https://github.com/yiisoft/yii2-queue/blob/master/docs/guide-zh-CN/worker.md#cron)。

run与listen命令的参数:
--verbose,-v: 将执行状态输出到控制台。
--isolate: 详细模式执行作业。如果启用,将打印每个作业的执行结果。
--color: 高亮显示输出结果。

yii queue/info //info命令打印关于队列状态的信息。

业务可在crontab中建立定时执行该命令,即可按入队顺序执行脚本

https://github.com/yiisoft/yii2-queue
Responses
  1. Hello.This post was extremely fascinating, particularly since I was browsing for thoughts on this subject last Wednesday.

    Reply
  2. through pleasure [url=http://www.viagenupi.com/]viagra cheapest[/url] deeply pass pretty king http://www.viagenupi.com/ near dirt currently fun generic viagra and cialis last childhood

    Reply
  3. Hi there mates, pleasant post and fastidious arguments commented here

    Reply
  4. early particular [url=http://oakley-sunglassesformen.us/]viagra generic[/url] though life altogether stable http://oakley-sunglassesformen.us/ without loss eventually turn buy cheap generic viagra online overall arrival

    Reply
  5. Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Basically Magnificent. I am also a specialist in this topic so I can understand your effort.

    Reply
  6. [url=http://valtrexcream.com/]cheap valtrex generic[/url] [url=http://flagyloverthecounter.com/]flagyl over the counter[/url] [url=http://buy-prednisone.com/]corticosteroids prednisone[/url] [url=http://voltarengel1.com/]diclofenac generic[/url] [url=http://metforminhcler.com/]buy metformin on line[/url] [url=http://metforminbest.us.org/]purchase metformin[/url] [url=http://zoloft.network/]online prescription free zoloft[/url] [url=http://generictadalafil.info/]tadalafil[/url] [url=http://propecia.us.org/]propecia merck[/url] [url=http://propranolol80mg.com/]propranolol 80 mg[/url] [url=http://tretinoin-cream-025.com/]tretinoin gel 0.05[/url] [url=http://salbutamolventolin.com/]ventolin evohaler[/url] [url=http://albuterol-hfa.com/]albuterol[/url] [url=http://prednisone-5mg.com/]prednisone[/url] [url=http://colchicine.us.org/]colchicine[/url] [url=http://buy-inderal.com/]buy inderal[/url] [url=http://clonidine-hydrochloride.com/]clonidine hydrochloride[/url]

    Reply