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. If you are going for best contents like me, simply go to see this website daily because it provides feature contents, thanks asmr 0mniartist

    Reply
  2. Hello colleagues, how is everything, and what you want to say concerning this piece of writing, in my view its really amazing for me. asmr 0mniartist

    Reply
  3. No matter if some one searches for his vital thing, therefore he/she wishes to be available that in detail, thus that thing is maintained over here. 0mniartist asmr

    Reply
  4. I know this site presents quality dependent articles and additional information, is there any other web page which presents these information in quality? asmr 0mniartist

    Reply
  5. Hey this is kinda of off topic but I was wanting to know if blogs use WYSIWYG editors or if you have to manually code with HTML. I'm starting a blog soon but have no coding know-how so I wanted to get guidance from someone with experience. Any help would be enormously appreciated! asmr 0mniartist

    Reply
  6. viagra buy online http://himpills.com cialis no prescription Ewyhrowj community health group

    Reply