加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

Yii2 队列 shmilyzxt/yii2-queue 简单概述

发布时间:2021-02-28 14:36:35 所属栏目:编程 来源:网络整理
导读:shmilyzxt/yii2-queue 简朴表明: 1.我用的yii2高级版,我们从设置开始看代码,这里我用的是mysql行列,起首设置文件,我把queue设置项写在根目次 commonconfigmain-local.php 下的 components 数组下,变动一下数据库设置.复制 composer 安装后复制 2个sql文件

shmilyzxt/yii2-queue 简朴表明:

1.我用的yii2高级版,我们从设置开始看代码,这里我用的是mysql行列,起首设置文件,我把queue设置项写在根目次commonconfigmain-local.php下的 components数组下,变动一下数据库设置.复制composer安装后复制

2个sql文件到数据库中成立行列数据表和执利用命失败时的数据表.

2.推送使命开始语法:Yii::$app->queue->pushOn(new SendMial(),['email'=>'49783121@qq.com','title'=>'test','content'=>'email test'],'email'); 我们到vendorshmilyzxtqueuequeuesDatabaseQueue.php去看看代码,pushOn()要领写在了DatabaseQueue类的父类vendorshmilyzxtqueuebaseQueue.php中:

canPush()) { //beforePush 入行列前的变乱 $this->trigger(self::EVENT_BEFORE_PUSH); //入行列 $ret = $this->push($job,$data,$queue); //afterPush 入行列后的变乱 $this->trigger(self::EVENT_AFTER_PUSH); return $ret; } else { throw new Exception("max jobs number exceed! the max jobs number is {$this->maxJob}"); } }

注释:这里最好去看看yii2 event变乱类,http://www.digpage.com/event.html

关于入行列: $this->push($job,$queue);,这里在共同queue类文件查察,相干函数跳转,处理赏罚一下数据记录到数据库中.(函数走向:getQueue()-->createPayload()-->pushToDatabase()),pushOn()最终返回数据插入数据库的功效,乐成$ret是1.

3.靠山运行呼吁处理赏罚行列,例:php ./yii worker/listen default 10 128 3 0 个中default是行列的名称,上面推送了一个email行列 应该改为email.

启动呼吁后,我们来看代码:起首执行:WorkerController节制器 actionListen要领,我们随着代码进入到 vendorshmilyzxtqueueWorker.php -- listen要领中,这里着实就是一向在轮回,执行操纵行列的使命:

pop($queueName); }catch (Exception $e){ throw $e; continue; } if($job instanceof Job){ //判定执行错误的次数是否大于传入的执行次数 if($attempt > 0 && $job->getAttempts() > $attempt){ $job->failed(); }else{ try{ //throw new Exception("test failed"); $job->execute(); }catch (Exception $e){ //执行失败,判定是否被删除,从头入队 if (! $job->isDeleted()) { $job->release($delay); } } } }else{ self::sleep($sleep); } if (self::memoryExceeded($memory)) { self::stop(); } } }

注释:在$queue->pop($queueName);vendorshmilyzxtqueuequeuesDatabaseQueue.php要领内行使事宜执行SQL,而且建设vendorshmilyzxtqueuejobsDatabaseJob.php的实例

getQueue($queue); if (!is_null($this->expire)) { //$this->releaseJobsThatHaveBeenReservedTooLong($queue); } $tran = $this->connector->beginTransaction(); //判定是否有一个可用的使命必要执行 if ($job = $this->getNextAvailableJob($queue)) { $this->markJobAsReserved($job->id); $tran->commit(); $config = array_merge($this->jobEvent,[ 'class' => 'shmilyzxtqueuejobsDatabaseJob','queue' => $queue,'job' => $job,'queueInstance' => $this,]); return Yii::createObject($config); } $tran->commit(); return false; }

至于:$job->execute();是DatabaseJob担任父类Job执行的,顺着代码找下去是yiibaseComponent trigger执行的变乱,

trigger(self::EVENT_BEFORE_EXECUTE,new JobEvent(["job" => $this,'payload' => $this->getPayload()]));//beforeExecute 执利用命之前的一个变乱 在JobEvent中并没有什么可执行的代码 $this->resolveAndFire();//真正执行的使命的要领 } /** * 真正使命执行要领(挪用hander的handle要领) * @param array $payload * @return void */ protected function resolveAndFire() { $payload = $this->getPayload(); $payload = unserialize($payload); //反序列化数据 $type = $payload['type']; $class = $payload['job']; if ($type == 'closure' && ($closure = (new Serializer())->unserialize($class[1])) instanceof Closure) { $this->handler = $this->getHander($class[0]); $this->handler->closure = $closure; $this->handler->handle($this,$payload['data']); } else if ($type == 'classMethod') { $payload['job'][0]->$payload['job'][1]($this,$payload['data']); } else if ($type == 'staticMethod') { $payload['job'][0]::$payload['job'][1]($this,$payload['data']); } else {//执行的`SendMail`类的`handle($job,$data)`要领 $this->handler = $this->getHander($class); $this->handler->handle($this,$payload['data']); } //执行完使命后删除 if (!$this->isDeletedOrReleased()) { $this->delete(); } }

最后到了执行的SendMail类的handle($job,$data),在这里就是推送到行列的工具和数据,接着就是我们的处理赏罚逻辑了.

getAttempts() > 3){ $this->failed($job); } $payload = $job->getPayload(); echo '
';print_r($payload);
  //$payload即使命的数据,你拿到使命数据后就可以执行发邮件了
  //TODO 发邮件
 }

总结

以上所述是小编给各人先容的Yii2 行列 shmilyzxt/yii2-queue简介,但愿对各人有所辅佐。措施员碰着题目城市上(编程之家52php.cn)查找题目解答要领!假如认为站点还不错,顺手转发给措施员伴侣一下!

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读