Summary of Laravel's skills in using Queue queues

Laravel queues provide a unified API for different background queue services, such as Beanstalk, Amazon SQS, Redis, and even other queues based on relational databases. The purpose of the queue is to delay the processing of time-consuming tasks, such as sending emails, thereby greatly reducing web requests and corresponding times.

The queue configuration file is stored in config/queue.php. Configuration for every queue driver can be found in this file, including database, Beanstalkd, Amazon SQS, Redis, and sync (native use) drivers. It also contains a null queue driver for tasks that abandon the queue

1. Why use a queue?

In general, queues are used for:

1, asynchronous,

2. Retry

Maybe you have other reasons to use queues, but these should be the most basic two reasons.

It takes a long time, such as uploading a file and performing some format conversions.

If you need to ensure the delivery rate, such as sending a text message, there is always a chance of failure because you want to call other people's APIs, so in order to ensure delivery, retrying is essential.

When using a queue, you must think about a problem, whether this task can be asynchronous, if it will cause problems because of asynchrony, then you must give up using the queue.

2. Necessary settings for the driver

database.php

Configure the redis database part in the database.php configuration file. There is a default connection by default, just use this:)
According to the configuration items required in this default connection, edit the .env configuration file, and set REDIS_HOST, REDIS_PASSWORD, Fill in REDIS_PORT with the corresponding value of Redis in your own server.

queue.php

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

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

database

To use the database queue driver, you need to create a data table to store tasks.

You can use queue:table Artisan

php artisan queue:table

Handle failed tasks

Sometimes tasks in your queue will fail. Don't worry, things won't be smooth sailing in the first place.

Laravel has a built-in convenience way to specify the maximum number of times a task will be retried. When the task exceeds this number of retries, it will be inserted into the failed_jobs data table. To create a migration file for the failed_jobs table, you can use the queue:failed-table command, followed by the migrate Artisan command to generate the failed_jobs table:

php artisan queue:failed-table

When the migration is created, you can use the migrate command to create the data table:

php artisan migrate

3. Execution command explanation

php artisan queue:work --daemon --quiet --queue=default --delay=3 --sleep=3 --tries=3
 
 

--daemon 
queue:work The Artisan command includes a --daemon option to force queue workers to continue processing jobs without having to restart the framework. Compared with the queue:listen command, this will significantly reduce the CPU usage.
Generally speaking, this option is generally added to the supervisor to save CPU usage.

--quiet
output nothing


--delay=3
After a task fails, how long to delay before retrying, in seconds. I personally recommend not to set this value too short, because a task fails (such as network reasons), and the retry time is too short may cause continuous failures.


--sleep=3
When I went to get the task in Redis, I found that there was no task, how long to rest, the unit is second. The setting of this value depends on whether your task is urgent. If it is a very urgent task, you cannot wait for too long.


--tries=3
defines the maximum number of retries for failed tasks. The setting of this value is determined according to the importance of the task, generally 3 times is more suitable.

4. Create a task

Generate task class

In your application, the job classes of the queue are placed in the app/Jobs directory by default. If this directory does not exist, it will be created automatically when you run the make:job Artisan command. You can create a new queue job with the following Artisan command:

php artisan make:job Demo

The generated class implements the Illuminate\Contracts\Queue\ShouldQueue interface, which means that the task will be pushed to the queue instead of executed synchronously.

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Log;

class Demo implements ShouldQueue
{
 use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

 public $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);
 }
}

controller code

public function queue_demo()
{
 $num = rand(1,999999999);
 // 这个任务将被分发到默认队列...
 DemoJob::dispatch($num);
}

5. Open the queue

php artisan queue:work --queue=default

Since it is local, the monitoring needs to be turned on, and when the interface is accessed, the tasks in the queue will be triggered

If you are online, you need to use the supervisor configuration

6. Supervisor configuration

Install Supervisor

Supervisor is a process monitoring software on the Linux operating system that automatically restarts queue:listen or queue:work commands after they fail. To install Supervisor on Ubuntu, you can use the following command:

sudo apt-get install supervisor

{tip} If manually configuring Supervisor yourself sounds a bit overwhelming, consider using Laravel Forge, which can automatically install and configure Supervisor for your Laravel project.

Configure Supervisors

Supervisor configuration files are generally placed in the /etc/supervisor/conf.d directory. In this directory you can create any number of configuration files to tell Supervisor how to monitor your process. For example, we create a laravel-worker.conf to start and monitor a queue:work process:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3
autostart=true
autorestart=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log

The numprocs command in this example will ask Supervisor to run and monitor 8 queue:work processes and restart them if they fail. Of course, you have to change the queue:work sqs command to show the queue driver of your choice.

Start Supervisor After the configuration file is created, you need to update the Supervisor configuration and start the process with the following command:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*

Guess you like

Origin blog.csdn.net/u012322399/article/details/127652615