Start talking about the yii2 queue from the new video template message push of "Abei's Knowledge Sharing"

Many members in the group talked about how to use yii2 to implement the queue function. In this series, we will talk about yii2-queue, the official queue extension of yii2. There are many details. The first article will take you through an example.

Let's talk needs first~

I want to do such a function: every time a new video is released, send a template message to the members who have subscribed to the tutorial where this video is located, but if I send the template message at one time when the course is released, it may cause speed problems and Danger of being blocked.

So I want to separate the receivers in time, say 100 people, send one every 5-10 seconds, I use yii2-queue to achieve it

yii2-queue is an extension officially launched by yii2 last year, which supports delayed push and queues can be stored in various media, such as files, databases, etc.

There are several concepts in the queue to talk about first

A task is what we need to do specifically. In this case, it is to send a WeChat template message. yii2-queue says that each task should be defined as a separate class. The task will eventually enter the queue waiting to be sent.

Worker The worker is responsible for task scheduling, such as which task in the queue should be executed in the next second, and of course it is also responsible for monitoring some information, such as how many tasks are waiting in the current queue, how many have been completed, etc.

So my idea came. When I create a new video, I put the template message to be sent into the queue. This process is not responsible for sending, and then there is a command line worker to help me complete the specific sending work.

Pre-arrangement

In yii2, yii2-queue exists in the form of a component, let's install it first.

composer require yiisoft/yii2-queue

After installation, you can find it in vendor's yiisoft, and then you need a simple configuration

// config/web.php
return [
    'bootstrap' => [
        'queue',
    ],
    'components' => [
        'queue' => [
            'class' => \yii\queue\file\Queue::class,
        ],
    ],
];

We add the queue to the preloaded property bootstrap, so that the worker can be called on the command line, and then configure the queue component. What we specify here is \yii\queue\file\Queue::class, which is to use a file to store queue information .

After configuring the component properties of the queue, you can use Yii::$app->queue to execute the related functions of the queue in the code.

Construct the task class

Since yii2 recommends making each task a class, let's do it. The requirements of yii2-queue are not high, you only need to make this class implement the \yii\queue\JobInterface interface.

namespace app\queue;


class TemplateJob extends Component implements \yii\queue\JobInterface {

    ...

    /**
     * 视频订阅ID
     * @var
     */
    public $subscribeId;


    /**
     * 执行队列任务
     * @param \yii\queue\Queue $queue
     * @return boolean
     */
    public function execute($queue){
        $subscribe = Subscribe::findOne($this->subscribeId);

    }
}

The code idea is the same as above, where execute is our implementation of the interface JobInterface, which is responsible for the specific sending work (task logic), and the purpose of letting TemplateJob inherit from Component is to facilitate the next push task to the queue.

Remember the above code and your doubts, let's look down

start push

With the task class, we only need to implement push in the relevant code, and these templates can send tasks to the queue.

// 某一段代码
$total = 0;
foreach($subscribes as $subscribe){
    ...

    Yii::$app->queue->delay(rand(5,10)*$total)->push(new TemplateJob([
        'subscribeId'=>$subscribe->id
    ]));
    
    $total++;

}

The above code pushes specific tasks one by one to the queue. Because our TemplateJob inherits from yii2's Component, the parameters passed when instantiating a TemplateJob object will be assigned to the properties of the corresponding TemplateJob class object, that is, xxx ->subscribeId = $subscribe->id.

Then we can push with the push function provided by yii2-queue. If we do not set the task limit time, when the worker in the queue finds that there is a task, it will be sent directly, but I want to spread the time, so I use the component The delay method, it can set a delay time, I take a number between 5-10 seconds, and then multiply by the row number of this task, here I use $total instead of the $key of foreach alone is not All $subscribes will be pushed.

Remember, delays are useful.

Now the tasks are in the queue one by one, the production line is about to start, and our robot worker is about to complete the scheduling work.

hard worker

Above we talked about a very important concept in the queue called worker (worker), which is responsible for the task execution of the queue.

There are several methods in yii2-queue to realize the automatic execution function of workers. Let's get familiar with a Supervisor first. Supervisor is a process monitor for linux which automatically starts your console process.

We can start the pipeline by typing yii queue/listen on the program command line.

Of course, you can also view the current queue status through yii queue/info, etc.

ok

A simple queue is implemented.

summary

The implementation of the above simple queue is very simple, but the queue has a lot of advanced support, such as the implementation of different workers, support for different media, and how to incorporate third-party support, etc., we will talk about it slowly.

Knowledge sharing from Abei http://nai8.me/article/index.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325061502&siteId=291194637