Implementation of Laravel Queue Use

  • 2021-11-13 06:46:06
  • OfStack

1 Environment

Laravel is an php framework similar to ThinkPHP, which encapsulates many functions and can be used conveniently. Queue Queue is one of them.

Under the environment of Windows, PHPstorm can be used as the integrated development environment of Laravel, IDE.

2 queues

Laravel can be configured with a variety of queue drivers, including "sync", "database", "beanstalkd", "sqs", "redis", "null" (see app/config/queue. php for details)
Among them, sync is synchronous, database is using database, the last three are third-party queue services, and the last one is not using queue.

The choice of driver is determined by the QUEUE_CONNECTION option in. env.

If QUEUE_CONNECTION=database, the database-driven queue is selected.

3 Principle

The so-called queue will be divided into producers and consumers of data. Producers deliver data to the queue, and consumers get data from the queue.

For example, in the scenario of sending emails to users, there are now 10w emails to be sent. In the simplest case, we need to have a method to split the recipients and contents of emails into 10w tasks and put them in the queue. At the same time, we need to set up a callback method to handle each task. When there is a mail sending task in the queue, the queue will actively call the callback method and pass the task details in. After the callback method is processed, a single message is sent. Other mails will be treated as samples.

4 Using database-driven queues

4.1 Build Task Table

Enter under the terminal


php artisan queue:table
php artisan migrate

When the database connection is normal, the jobs table appears in the database:


 [id] bigint 
 [queue] nvarchar(255) 
 [payload] nvarchar(max) 
 [attempts] tinyint 
 [reserved_at] int 
 [available_at] int 
 [created_at] int 

4.2 Creating Task Classes


php artisan make:job SendEmail

Executing the above commands in the terminal automatically generates app/Jobs/SendMail. php files


class SendMail implements ShouldQueue

In the handle method of this file, task processing logic can be placed.

4.3 Send Tasks

You can call the dispatch sending task at any location like the following 1


SendMail::dispatch($email);

4.4 Drive Queue

After completing the above steps, one record (exported as an insert SQL statement) can be found in the database:

INSERT INTO [jobs]([id], [queue], [payload], [attempts], [reserved_at], [available_at], [created_at]) VALUES (6, N'default', N'{"displayName":"App\\Jobs\\ProcessPodcast","job":"Illuminate\\Queue\\CallQueuedHandler@call","maxTries":null,"timeout":null,"timeoutAt":null,"data":{"commandName":"App\\Jobs\\ProcessPodcast","command":"O:23:\"App\\Jobs\\ProcessPodcast\":8:{s:29:\"\u0000App\\Jobs\\ProcessPodcast\u0000data\";s:6:\"111222\";s:6:\"\u0000*\u0000job\";N;s:10:\"connection\";N;s:5:\"queue\";N;s:15:\"chainConnection\";N;s:10:\"chainQueue\";N;s:5:\"delay\";N;s:7:\"chained\";a:0:{}}"}}', 0, NULL, 1545980176, 1545980176);

At this point the task has been placed in the database, only after the queue is running, the queue can actively call the callback method.


php artisan queue:work

Run the above command in the terminal. The command also has many parameters, such as deamon, tries, etc., which can be specified as needed.

4.5 Daemons

In order to ensure the stability of application services, it is necessary to open daemons.

Under Linux, Supervisor is generally used, and Forever is used under Windows

4.6 Execute failed processing

Laravel also provides solutions for handling failed tasks. You can create a table to record failed tasks by running the following command.


php artisan queue:failed-table
php artisan migrate

failed_jobs is generated in the database:


 [id] bigint
 [connection] nvarchar(max)
 [queue] nvarchar(max) 
 [payload] nvarchar(max) 
 [exception] nvarchar(max) 
 [failed_at] datetime

The Exception that causes the task to fail will be passed to the failed method of SendMail, so you need to implement this method in SendMail and do the next step.

There are many reasons for task execution failure, such as parameter passing error, number of attempts exceeding the limit, timeout, and even throwing an exception in handle method, which will be treated as a failed task.

4.7 Processing before and after task execution

Laravel provides processing entry before and after task execution, that is, add the following code to boot () in App/Providers/AppServiceProvider:


public function boot()
{
   Queue::before( function (JobProcessing $event) {
     Log::info(" Before processing the task ");
   });
   Queue::after( function (JobProcessed $event) {
     Log::info(" After processing the task ");
   });
}

In the $event passed, with task details, a few simple examples:


$event->connectionName
$event->job
$event->job->payload()

5 Drive queues using Redis

5.1 Laravel Installing Predis Package

Before using Redis in Laravel, you need to install the predis/predis package through Composer:


 [id] bigint 
 [queue] nvarchar(255) 
 [payload] nvarchar(max) 
 [attempts] tinyint 
 [reserved_at] int 
 [available_at] int 
 [created_at] int 
0

The above extensions are intended to help Laravel deal with Redis, which we currently lack.

If you change QUEUE_CONNECTION from. env to redis at this time, an error will be reported when accessing:

Predis \ Connection \ ConnectionException (10061)
����Ŀ����������ܾ����޷����ӡ� [tcp://127.0.0.1:6379]

5.2 Configuring the Redis service

Download the source code in Redis official website and compile it by yourself.

The official version of Windows is not available. The Windows layout of Redis is maintained by Microsoft Working Group, which can be found on its GitHub page. However, it seems that it is no longer maintained. The latest version is 3.2. 100 released in 16 years.

Through simple operation under Linux


 [id] bigint 
 [queue] nvarchar(255) 
 [payload] nvarchar(max) 
 [attempts] tinyint 
 [reserved_at] int 
 [available_at] int 
 [created_at] int 
1

You can start the service and then pass


 [id] bigint 
 [queue] nvarchar(255) 
 [payload] nvarchar(max) 
 [attempts] tinyint 
 [reserved_at] int 
 [available_at] int 
 [created_at] int 
2

To try using Redis. It is also very simple to use, namely set key value and get key.
After installation under Windows, go to the installation directory on the command line cd


C:\Program Files\Redis>redis-server redis.windows.conf
C:\Program Files\Redis>netstat -an|find "6379"
 TCP  127.0.0.1:6379     0.0.0.0:0       LISTENING

You can start the Redis service.

The Redis service is stopped by the following command:


 [id] bigint 
 [queue] nvarchar(255) 
 [payload] nvarchar(max) 
 [attempts] tinyint 
 [reserved_at] int 
 [available_at] int 
 [created_at] int 
4

Related articles: