Brief description of Yii2 queue Shmilyzxt/yii2-queue

Source: Internet
Author: User
Tags sql using yii
This article mainly introduces the YII2 queue Shmilyzxt/yii2-queue Simple overview, the need for friends can refer to the next. We hope to help you.

Shmilyzxt/yii2-queue Simple Explanation:

1. I use the YII2 Premium version, we start from the configuration to see the code, here I use the MySQL queue, the first configuration file, I write the queue configuration item in the root directory under common\config\main-local.php the components array, change the database configuration. Replication composer after installation

Vendor\shmilyzxt\yii2-queue\jobs\jobs.sqlvendor\shmilyzxt\yii2-queue\failed\failed.sql

2 SQL files into the database to set up the queue data table and the data table when the task fails to execute.

2. Push task start syntax: \ Yii::$app->queue->pushOn(new SendMial(),['email'=>'49783121@qq.com','title'=>'test','content'=>'email test'],'email'); We go to vendor\shmilyzxt\queue\queues\DatabaseQueue.php see the code, the Pushon () method is written in the DatabaseQueue class's parent class vendor\shmilyzxt\queue\base\Queue.php :

into the queue public function Pushon ($job, $data = ", $queue = null) {//canpush Checks if the queue has reached the maximum task amount if ($this->canpush ()) {   //be Forepush Event  $this->trigger (Self::event_before_push) before entering the queue;   into  the queue $ret = $this->push ($job, $data, $queue);  Afterpush Event  $this->trigger (Self::event_after_push) after entering the queue;  return $ret; } else {  throw new \exception ("Max jobs number exceed! The max jobs number is {$this->maxjob} "); } }

Note: It's best to take a look at the Yii2 event class, http://www.digpage.com/event.html

About into the queue: $this->push($job, $data, $queue);, here in conjunction with the Queue class file view, related functions jump, processing data records into the database. Function direction: The getQueue()-->createPayload()-->pushToDatabase()),pushOn() result of the final return data inserted into the database, the successful $ret is 1.

3. Background Run command processing queue, example: php ./yii worker/listen default 10 128 3 0 where default is the name of the queue, the above pushes an email queue should be changed to email.

After the

Start command, let's look at the code: first execute: workercontroller Controller A Ctionlisten method, we followed the code into vendor\ In the shmilyzxt\queue\worker.php-listen method, this is actually the task that has been looping, performing the operation queue:

/** * enable a queue background listener task * @param queue $queue * @param string $queueName The name of the listener queue (in Pushon When the task is pushed to which queue, you need to listen to the queue to get the task) * @param int $attempt Queue task failed attempts, 0 is unlimited * @param int $memory The maximum memory allowed to be used * @param int $sleep per Time interval of the second detection */public static function Listen (Queue $queue, $queueName = ' Default ', $attempt = ten, $memory = +, $sleep = 3,  $delay = 0) {while (true) {try{//databasequeue takes an available task (instance) from the database queue and updates the task $job = $queue->pop ($queueName);  }catch (\exception $e) {throw $e;  Continue   if ($job instanceof Job) {//determines if the number of execution errors is greater than the incoming execution count if ($attempt > 0 && $job->getattempts () > $attempt) {  $job->failed ();   }else{try{//throw New \exception ("Test Failed");   $job->execute ();   }catch (\exception $e) {//execution failed, judging whether it was deleted, re-queued if (! $job->isdeleted ()) {$job->release ($delay);  }}}}else{Self::sleep ($sleep);  } if (self::memoryexceeded ($memory)) {self::stop (); } } }

Note: $queue->pop($queueName); vendor\shmilyzxt\queue\queues\DatabaseQueue.php executing SQL using transactions within a method, and creating vendor\shmilyzxt\queue\jobs\DatabaseJob.php an instance

Remove a task public function pop ($queue = null) {$queue = $this->getqueue ($queue); if (!is_null ($this->expire)) {  / /$this->releasejobsthathavebeenreservedtoolong ($queue); } $tran = $this->connector->begintransaction (); Determine if there is an available task that needs to execute if ($job = $this->getnextavailablejob ($queue)) {  $this->markjobasreserved ($job->id) ;  $tran->commit ();  $config = Array_merge ($this->jobevent, [  ' class ' = ' Shmilyzxt\queue\jobs\databasejob ',  ' queue ' = = $ Queue,  ' job ' = $job,  ' queueinstance ' = $this,  ]);  Return \yii::createobject ($config); } $tran->commit (); return false; }

As for: $job->execute() ; is the Databasejob inherits the parent job execution, follows the code to go down is yii\base\Component trigger executes the event,

/** * Perform task */public function execute () {$this->trigger (Self::event_before_execute, New jobevent (["Job" = = $this, ' P Ayload ' + $this->getpayload ()]);//beforeexecute an event prior to performing a task has no executable code in jobevent $this->resolveandfire ( );//The method of the really executed task}/** * True task execution method (call hander Handle method) * @param array $payload * @return void */protected function resolveandf  IRE () {$payload = $this->getpayload (); $payload = Unserialize ($payload);  Deserialization of data $type = $payload [' type '];  $class = $payload [' Job ']; if ($type = = ' closure ' && ($closure = (new Serializer ())->unserialize ($class [1])) instanceof \closure) {$thi   S->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 {//executedThe ' Handle ($job, $data) ' method of the ' SendMail ' class $this->handler = $this->gethander ($class);  $this->handler->handle ($this, $payload [' data '];  }//Delete if after executing task (! $this->isdeletedorreleased ()) {$this->delete (); } }

Finally to the SendMail class of execution handle($job,$data) , here is the object and data pushed to the queue, and then our processing logic.

Public function handle ($job, $data) {  if ($job->getattempts () > 3) {   $this->failed ($job);  }  $payload = $job->getpayload ();  Echo ' <pre> ';p rint_r ($payload);  $payload is the task data, you get the task data will be able to execute e-mail//todo e-  Mail}

Related recommendations:

YII2 integrated Fast Search for efficient Chinese word search

Yii solves the problem of DeleteAll even table deletion error

How Yii Filters Bad code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.