The most basic operation tutorial of using Queue in Laravel

  • 2021-08-31 07:29:27
  • OfStack

Preface

Queue services in laravel are no different from other queue services, and they are the simplest and most common processes that most conform to human thinking: there is one place to store queue information, one PHP process writes tasks at runtime, and another PHP daemon polls queue information, and executes and deletes tasks that meet the execution requirements. Since PHP is an url-driven synchronization language and is itself blocking, it is not surprising that laravel provides a daemon tool to query and execute queue information.

The queue that first came into contact with Laravel in the past two days also had a good time. . . After a toss, I still feel that I can't do it for Laravel. The document is relatively simple and generalized, and I can't do it when I look at the source code (but it is the most reliable to look at the source code for debugging and verifying slowly).

The following is my simple Demo, which only uses the most basic operation of queue, and the more advanced operation needs to take another good time:)

Students who have some difficulties in getting started with Laravel can communicate with me for 1 time.

Configure

Add an Redis package

Add "predis/predis": "~ 1.0" to require in composer. json, and then update composer up 1.

database.php

Configure the redis database part in the database. php configuration file. There is 1 default connection by default, so use this one:)

According to the configuration items required in this default connection, edit the. env configuration file, and fill in REDIS_HOST, REDIS_PASSWORD and REDIS_PORT as the corresponding values of Redis in your own server.

queue.php

First, you need to configure QUEUE_DRIVER in. env, because you are going to use Redis now, so configure it as redis.

Then configure the redis connection of connections part in queue. php, where the corresponding value of connection is the default connection of redis in database. php.

Task class

The next step is to write the actual operation class. Laravel provides artisan command to simplify the creation of task classes:


php artisan make:job Demo

The Jobs directory will be generated under the app directory, which already has the Demo. php task class.

Lumen does not have this artisan command, but it is also very convenient. By default, there will be an ExampleJob. php has been written, and a copy can be changed to a name.
First, write a simple log output test 1. In handle method:


Log::info('Hello, queue');

Issue tasks

Now write an entry function to push the task to the queue. Using the helper function dispatch ():

The following methods are used in Laravel:


Demo::dispatch();

The following methods are used in Lumen:


dispatch(new Demo);

Open queue

If it goes well, this is the last step. Execute from the command line:


php artisan queue:listen --queue=default

It listens to the queue and outputs simple execution, such as:


[2017-11-07 02:12:47] Processing: App\Jobs\Demo
[2017-11-07 02:12:47] Processed: App\Jobs\Demo

After no problem, you can let this queue script execute in the background:


php artisan queue:work --daemon --quiet --queue=default 1>> /dev/null 2>&1

Under Advanced 1:)

When pushed to the queue, there will be a need to pass parameters, so how to pass them here?

Parameter passing

Incoming

Pass in parameters in the entry function as follows:

The following methods are used in Laravel:


$param = 'Stephen';
Demo::dispatch($param);

The following methods are used in Lumen:


$param = 'Stephen';
dispatch(new Demo($param));

Receive

The parameters are received in the task class as follows:


protected $param;
/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($param)
{
  $this->param = $param;
}
/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
  Log::info('Hello, ' . $this->param);
}

This is the end of the first attempt, and there are many advanced uses, such as delayed distribution, error handling, priority, failure handling, etc., which will continue to be written later:)

Multiple queues

This is a problem that must be considered. It is impossible for me to put all tasks in a queue called default, so it is not easy to manage the queue.

To specify different queues, it is very simple to follow the onQueue () method immediately after dispatch ():


Log::info('Hello, queue');
0

No, I don't seem to have defined this queue called emails. Well, naturally one change needs to be made. In the queue. php configuration file, redis configuration queue is changed from default to {default}. The effect of this is that the queue name can be dynamically obtained from runtime, instead of being written to death.

If you use the Lumen framework, writing this directly will report an error: Call to a member function onQueue () on string.

The reason is that the trait of Illuminate\ Foundation\ Bus\ Dispatchable is not used in the Job base class of Lumen, but the onQueue () method in Illuminate\ Bus\ Queueable is directly used.

It is now clear that our Illuminate\ Bus\ Queueable class uses the trait, so we need to call the onQueue () method on the Job class.


Log::info('Hello, queue');
1

When we open the queue:


Log::info('Hello, queue');
2

When the queue names emails and dispatch are specified here, it is sufficient to keep the specified queue name 1.

Summarize

Reference link

Laravel Official Document


Related articles: