This article mainly introduces a simple overview of Yii2 queue shmilyzxt/yii2-queue. Friends in need can refer to it. I hope to be helpful.
shmilyzxt/yii2-queue Simple explanation:
1. I use the yii2 advanced version. Let’s start with the configuration and look at the code. Here I The mysql queue is used. First, configure the file. I write the queue configuration items in the components
array under the root directory common\config\main-local.php
and change the database configuration. .Copycomposer
After installation, copy
vendor\shmilyzxt\yii2-queue\jobs\jobs.sql vendor\shmilyzxt\yii2-queue\failed\failed.sql
2 sql files to the database to establish the queue data table and the data table when the task fails.
2. Push the task to start Syntax:\Yii::$app->queue->pushOn(new SendMial(),['email'=>'49783121@qq.com','title'=>'test', 'content'=>'email test'],'email');
Let's go to vendor\shmilyzxt\queue\queues\DatabaseQueue.php
to see the code. The pushOn() method is written in The parent class of the DatabaseQueue
class is vendor\shmilyzxt\queue\base\Queue.php
in:
//入队列 public function pushOn($job, $data = '', $queue = null) { //canPush 检查队列是否已达最大任务量 if ($this->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}"); } }
Note: It is best to look at the yii2 event class here ,http://www.digpage.com/event.html
About entering the queue: $this->push($job, $data, $queue);,
Here is Cooperate with the queue class file to view, jump to related functions, process the data and record it in the database. (Function direction: getQueue()-->createPayload()-->pushToDatabase()), pushOn()
Finally returns the result of data insertion into the database. Successful $ret is 1.
3. Background running command processing queue, for example: php ./yii worker/listen default 10 128 3 0
where default is the name of the queue. An email queue pushed above should be changed to email.
After starting the command, let’s look at the code: First execute:WorkerController
Controller actionListen
Method, we follow the code and enter the vendor\shmilyzxt\queue\Worker.php -- listen
method. This is actually a loop that executes the tasks of the operation queue:
/** * 启用一个队列后台监听任务 * @param Queue $queue * @param string $queueName 监听队列的名称(在pushon的时候把任务推送到哪个队列,则需要监听相应的队列才能获取任务) * @param int $attempt 队列任务失败尝试次数,0为不限制 * @param int $memory 允许使用的最大内存 * @param int $sleep 每次检测的时间间隔 */ public static function listen(Queue $queue, $queueName = 'default', $attempt = 10, $memory = 512, $sleep = 3, $delay = 0){ while (true){ try{ //DatabaseQueue从数据库队列取出一个可用任务(实例),并且更新任务 $job = $queue->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(); } } }
Comments : Use transaction to execute SQL in $queue->pop($queueName);
is vendor\shmilyzxt\queue\queues\DatabaseQueue.php
method, and create vendor \shmilyzxt\queue\jobs\DatabaseJob.php
instance
//取出一个任务 public function pop($queue = null) { $queue = $this->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' => 'shmilyzxt\queue\jobs\DatabaseJob', 'queue' => $queue, 'job' => $job, 'queueInstance' => $this, ]); return \Yii::createObject($config); } $tran->commit(); return false; }
As for:$job->execute()
;DatabaseJob inherits the execution of the parent class Job, along the The code is found to be the event executed by yii\base\Component trigger
,
/** * 执行任务 */ public function execute() { $this->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(); } }
and finally the handle($job,$data) of the executed
SendMail class )
, here are the objects 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 class="brush:php;toolbar:false">';print_r($payload); //$payload即任务的数据,你拿到任务数据后就可以执行发邮件了 //TODO 发邮件 }
Related recommendations:
##Yii2 integrates Xunsou to achieve efficient Chinese word segmentation retrieval
Yii solves the problem of DeleteAll connection table deletion error reporting
The above is the detailed content of Brief description of Yii2 queue shmilyzxt/yii2-queue. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)