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命令打印关于队列状态的信息。
- 执行
php yii queue
// 命令需要确保配置是在common中**
业务可在crontab中建立定时执行该命令,即可按入队顺序执行脚本
https://github.com/yiisoft/yii2-queue
本文由 HongXunPan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为:
2019-01-09 13:04:50
Nicely put. With thanks. Best Essay writing http://moversmovingquotes.com/ http://moversmovingquotes.com/
Hi, everyything is going nicely here and ofcourse every one is sharing information, that's really excellent, keep up writing. https://cbdwwwkratom.com will kratom make you faiil a drug test https://cbdwwwkratom.com
Highly descriptive post, I loved that bit. Will there bbe a part 2? https://essayserviceusukua.com thesis wriuting services https://orderessayusukau.com https://orderessayusukau.com/
If some one desires expert view regarding blogging and site-building then i advise him/her to visit this weblog, Keep up the nicee job. https://orderessaycheap.com/ essay phraases used in writing, Bridgette, https://admissionessaywritingservice.com https://orderessaycheap.com
What's up, after readikng this amazing piece of writing i am ass well delighted to share my know-how here with mates. https://medium.com college paper college paper https://erteref.medium.com/ https://medium.com/
Hi, off course this piece of writing is genuinely good and I havce learned llot of things from it concerning blogging. thanks. https://buycustomessay.info best essay writing service reviews, Anya, https://buyessayonlinecheap.info https://collegeapplicationessaywritingservice.info