Don’t Wash dishes studio – Hanxiao
The copyright belongs to the author, please indicate the source
Why use queues?
- 2) it would be too low to write only regular business, so we have to try something more advanced (danjun • ㅂ•́)و✧!
- An article on message queues on CSDN will give you a lot of inspiration
Introduction to queues in laravel framework
The best way to explain is by example
-
First, we need to understand a few concepts, from small to large: queue tasks, queues, connections. They are sequentially contained relationships, with many queue tasks in one queue and many queues in one connection
-
Queue tasks: Actions performed on each user, understood as queue tasks. For example, when a user registers, we send an authentication email. Each user registers, we do such a task
-
Queue: Many queues are crowded together waiting to be processed, forming a queue
-
Connection: Storage form of queue
-
Queue processor Processes queue tasks
-
-
Example: Queue task: an action, such as sending an email, will be replaced with a simpler action example
Queues: Queues exist so that subsequent queue handlers can process the corresponding queue tasks, which we will not cover
Connection: database (also refer to mysql), Redis, etc., we mainly use database
In actual combat
-
Generate the task class with this command:
php artisan make:job SendEmail
A Jobs folder will be generated here along with our task class
Create a Controller to distribute tasks
php artisan make:controller QueueTest
-
Specific code implementation:
How do I create a job in a controller and pass the parameters? Please look at:
SendEmail.php:
QueueController.php:
Routing – web. PHP:
Create job data table
php artisan queue:table
php artisan migrate
A super simple queue adding process is completed, and once the service is served, we send the request using tools such as Postman or a browser
Then you can see that even if we don’t add a task handler, our task is still whizzed to completion, there is nothing in the job table, why??
Open the. Env file
Change it to database, repeat the request, and look at the job table
-
Start queue task handler:
One line: PHP artisan queue:work
This sentence can add a lot of parameters, not too much statement here, you can see the documentation, it is easy to understand
We see that the task is executed directly, because when we added the task, it was executed one minute later, and when we turned on the processor, it was already out of time, so the queue was consumed
-
Use the Supervisor to keep the task manager running permanently in the background, otherwise we would have to keep the terminal window open to keep the task processor running
-
The installation
pip install supervisor
-
configuration
The Supervisor is very personal to boot, and typing echo_supervisord_conf on the command line gives us a copy of the default configuration file that we’re going to copy over, Where any one of the current user has permission to create a new. Ini or. Conf suffixes at the end of the file (I create path: / usr/local/etc/supervisord. Ini), copy content, can be saved, but we just need to do to modify some of the content
To get a more intuitive view of how our processes are running, make the following changes to remove httpServer and port comments:
This allows us to see the process in action in the browser
Then modify the last line as follows:
This is where we put our Laravel-worker configuration file. Here we create and edit our laravel-worker.ini file
Some important parameters:
-
command: As I understand it, the Supervisor will manage the process generated by this command. However, since we are not in the project folder, we cannot directly PHP artisan Queue :work, so we add our project path in front of artisan. The part obscured by the Mosaic is the file directory for our Laravel project
-
User: indicates the current system user name. Root indicates the current user name
-
Logfile in the last line: we need to select the directory that we have permission to, and we need to manually create folders and files for them
In this way, the supervisor configuration is complete
Then starts surpervisord – c configuration file directory, and my order is: supervisord – c/usr/local/etc/supervisord. Ini
Type http://localhost:9001 into your browser and you’ll see our task processor running
Our queue tasks also performed successfully
-
Reference: A Laravel community article on the Supervisor run queue task processor
-