Laravel Basic Tutorial--Queue

Source: Internet
Author: User

Queue

Brief introduction

The Laravel Queue Service provides a unified API for a variety of different background queue services. Queues allow you to defer tasks that consume time, such as sending an email. This can effectively reduce the time of the request response.

Configuration

The configuration file for the queue is stored in config/queue.php. In this file you will find examples of queue-driven configuration connections supported by the framework. These drivers include: Database, Beanstalkd,amazon Sqs,redis, and a synchronous (locally used) driver.

There is also a driver named null indicating that the queue task is not used.

Queue Prerequisites

Database

If you are using the database queue driver, you need to add a data table to handle the queue tasks. You can use the queue:table Artisan command to generate a migration table. Once the migration table is built, you can use the Migrate command to migrate to the database:

PHP Artisan queue:tablephp Artisan Migrate

Other queue dependencies

The other queue drivers and their corresponding dependencies are listed below:

    • Amazon sqs:aws/aws-sdk-php ~3.0
    • Beanstalkd:pda/pheanstalk ~3.0
    • Redis:predis/predis ~1.0

Writing task Classes

Build Task Class

By default, all the tasks that can be queued are stored in the App/jobs directory, and you can generate a new queue task with the Artisan command:

PHP Artisan make:job Sendreminderemail

The command generates a new class in the App/jobs directory. The class implements the Illuminate\contracts\queue\shouldqueue interface, which indicates that Laravel should add the task to the task queue in the background instead of executing synchronously.

Task class Structure

The task class is very simple, usually it contains only a handle method to be called when the queue task executes. Let's look at a simple task example:

!--? phpnamespace app\jobs;use app\user;use app\jobs\job;use illuminate\contracts\ Mail\mailer;use Illuminate\queue\serializesmodels;use Illuminate\queue\interactswithqueue;use Illuminate\ Contracts\queus\shouldqueue;class Sendreminderemail extends Job implements shouldqueue{use Interactswithqueue,  Serializesmodels;  protected $user;   /** * Create A new job instance.   * * @param user $user * @return void */Public function __construct (user $user) {$this--->user = $user;    }/** * Execute the job. * * @param Mailer $mailer * @return void */Public function handle (Mailer $mailer) {$mailer->send (      ' Emails.reminder ', [' user ' = + $this->user], function () {//});    $this->user->reminders ()->create (...); }}

In this case, you need to be aware that we can directly transfer a eloquent model in the constructor of the queue task. Because we introduced the serializesmodels trait, the eloquent model is gracefully serialized and deserialized when the queue task executes. If the queue task receives the eloquent model in the constructor, the queue task will only serialize the model's ID. When a task needs to be processed, the queue system automatically retrieves the model instance from the database based on the ID. This is completely transparent in the application, which avoids the problem of serializing the complete model that might occur in the queue.

The handle method is called when the queue task executes. What you need to know is that we can use type hints in the handle method of the task to make dependent injections. The Laravel service container automatically injects these dependencies into it.

Something unusual happened.

If an exception is thrown while the task is in progress. It automatically frees the task and appends it to the end of the queue so that the task can try again. The task will be released continuously for an attempt unless the number of attempts exceeds the maximum number of times set. You can set the maximum number of attempts by adding the--tries option to the queue listener Queue:listen or queue:work Artisan task command. We will cover the queue listener in detail in the following space.

Manual release task to end of queue

You can use the release method to perform manual release tasks. The Interactswithqueue trait has been introduced in the task class generated by the Laravel command, which provides the release method for accessing the task queue. The method receives a parameter: the interval (in seconds) that you want the task to be available again:

Public function handle (Mailer $mailer) {  if (condition) {    $this->release (Ten)}  }

Check the number of times a task has been attempted

As we mentioned above, if an exception occurs while the task is in progress, the queue automatically frees the task and pushes the task to the end of the queue so that it can try again. You can use the attempts method to check the number of releases of a task:

Public function handle (Mailer $mailer) {  if ($this->attempts () > 3) {    //  }}

Push Task to queue

The Laravel base controller in app/http/controllers/controller.php uses the dispatchesjobs trait. This trait provides some ways to allow you to easily push tasks to the queue. For example Dispatch method:

 
  Dispatch (New Sendreminderemail ($user));}   }

Dispatchesjobs traits

Of course, sometimes you might want to distribute tasks anywhere in your app, not just in a routing or controller. For this reason, you can use the Dispatchesjobs trait to any class that you need to invoke the distribution task in your app, so you can use the dispatch method in this class, and here's a simple example of using trait:

    

Dispatch method

Alternatively, you can use the dispatch global Help method:

Route::get ('/job ', function () {  dispatch (new App\jobs\performtask);   Return ' done! ';});

To assign a queue to a task

You can also specify the queue to which the task needs to be assigned.

You can categorize your queue tasks to push tasks to different queues, and you can even assign priority to various queue work. This is not a push task on a different queue connection as defined in the queue configuration file, but rather an operation that is performed only on the queue specified in a single connection. You can use the Onqueue method of the task instance to specify the queue. Onqueue from illuminate\bus\queueable trait, the trait has been introduced by the App\jobs\job base class:

 
   Onqueue (' emails ');    $this->dispatch ($job);  }}

Delay task

Sometimes, you might want to delay the execution of a queue task. For example, you might want a queue task to send a reminder message 5 minutes after the user registers. You can do this in the task class using the delay method, which is provided by the illuminate\bus\queueable trait:

 
   Delay (5);     $this->dispatch ($job);   }}

In this example, we specify that the task should be deferred for 5 minutes in the queue.

Note: The Amazon SQS Service has a maximum deferred execution limit, which can be delayed by up to 15 minutes.

Task events

Task Life cycle Events

You can use the Queue::before and Queue::after methods to register a callback that is called before the queue task starts or after the queue executes successfully. Callbacks provide a great opportunity to add additional logs, continuously perform subtasks, or increase statistics. For example, we can define an event listener after a successful execution of a task in Laravel's Appserviceprovider, and attach a callback to the event:

 
   ConnectionName       //$event->job       //$event->data     });   }   /**    * Register the service provider.    *    * @return void    *    /Public Function register ()    {      //    }}

Run Queue Listener

Start the queue monitoring

Laravel contains a Artisan command to run the execution of a task pushed to the queue. You can use the Queue:listen command to run the listener:

PHP Artisan Queue:listen

You can also specify which queue to listen to:

PHP Artisan Queue:listen Connection-name

What you need to note is that this command executes and it will continue to run unless you manually stop it. You can use Supervisor process monitoring to ensure that the queue listener is running.

Queue priority

You can use to split the connected queue to determine the priority of the queue's operation:

PHP Artisan Queue:listen--queue=high,low

In this example, the task takes precedence over the high queue before it runs the task in the low queue.

Specify the time-out period for a task

You can set the time-out period for a task:

PHP Artisan Queue:listen--timeout=60

Specify the sleep time for a queue

In addition, you can specify the time (in seconds) to wait for the queue to poll for new tasks:

PHP Artisan Queue:listen--sleep=5

You need to be aware that the queue will only sleep if there are no tasks that need to be performed in the queue. If there are more than one executable task in the queue, the queue continues to perform the task without a sleep operation.

The first task of the execution queue

You can use the Queue:work command to execute only the first task of the queue:

PHP Artisan Queue:work

Supervisor Configuration

Supervisor is a process monitor for the Linux operating system, and it can be restarted automatically if the Queue:listen or Queue:work command fails. You can use the following command to install Supervisor in Ubuntu:

sudo apt-get install Supervisor

Supervisor configuration files are typically stored in the/ETC/SUPERVISOR/CONF.D directory. In this directory, you can create any number of configuration files to guide supervisor to manage the monitoring process. For example, let's create a laravel-worker.conf file to start and monitor the queue:work process:

[program:laravel-worker]process_name=% (Program_name) s_% (process_num) 02dcommand= Php/home/forge/app.com/artisan Queue:work SQS--sleep=3--tries=3--daemonautostart=trueautorestart=trueuser=forgenumprocs=8redirect_stderr= Truestdout_logfile=/home/forge/app.com/worker.log

In the example above, Numprocs instructs Supervisor to run 8 queue:work processes and monitor them, automatically restarting them when they fail. Of course, you can modify the Queue:work sqs portion of the command to use the queue driver you expect.

Once the profile creation is complete, you can use the following command to update the Supervisor configuration file and start the process:

sudo supervisorctl rereadsudo supervisorctl updatesudo supervisorctl start laravel-worker:*

For more Supervisor configuration information, please refer to Supervisor documentation. Alternatively, you can use Laravel Forge to automatically configure and manage your Supervisor configuration from a convenient Web interface.

Queue Monitor Daemon

The Queue:work Artisan command contains a--daemon option to force the queue to perform tasks continuously without rebooting the framework. This significantly reduces CPU consumption by comparing the Queue:lsten command:

PHP artisan queue:work connection-name--daemonphp artisan queue:work connection-name--daemon--sleep=3php artisan Queue : Work Connection-name--daemon--sleep=3--tries=3

As you can see, the Queue:work task supports options that are similar to Queue:listen. You can display the available options by using the PHP artisan help queue:work command.

Coding Considerations for Daemons

The daemon for the queue task does not reboot the framework before each task executes, so you should be careful to clean up some of the heavier resource consumption when your task finishes. For example, if you use the GD class library to process a picture, you should use Imagedestroy to release it from memory after the task has finished executing.

To deploy a queue listener with daemons

Because the daemon of the queue work is a resident process. It will not restart when your code changes. Therefore, you should use the deployment script to redeploy the queue work that uses the daemon when code changes:

PHP Artisan Queue:restart

This command will gracefully instruct the queue to complete the current task after death, so there will be no missing tasks. You should be aware that when you execute the queue:restart command, the queue will die, so you should use a process manager, such as Supervisor, which you can configure to automatically restart the queue.

Note: This command relies on the caching system to make a restart schedule. The default APCU does not support running tasks in the CLI. If you use APCU, you should add Apc.enable_cli=1 to your APCU configuration.

Interacting with a failed task

Since a lot of things can't be done as planned, sometimes the execution of a queue task may fail, don't worry, it happens to us the best! Laravel contains a convenient way to specify the number of times a task should be tried repeatedly. If a task is repeatedly executed to a specified number of times, it is recorded in the Failed_jobs table. The name of the table you can customize with the config/queue.php configuration file.

You can use the queue:failed-table command to generate a failed_jobs migration table:

PHP Artisan queue:failed-table

When you run the queue listener, you can use the--tries option to specify the maximum number of attempts for a task:

PHP Artisan Queue:listen connection-name--tries=3

Failed task events

If you want to perform certain actions when the queue task fails, you can use the Queue::failing method to register a listener event. This event provides a great opportunity to inform your team via email or hipchat. For example, you can attach a callback to an event in Appserviceprovider:

 
   Job       //$event-data     });   }   /**    * Register the service provider.    *    * @return void    *    /Public Function register ()    {      //    }}

Failed methods in the task class

You can define a failed method in the task class for finer control, which allows you to perform some of the specified actions when a task fails:

     

Re-executing failed tasks

You can use the queue:failed Artisan command to display failed tasks in the Database Failed_jobs table:

PHP Artisan queue:failed

The queue:failed command lists the task ID, connection, queue, and failure time. The task ID can be used to attempt a failed task. For example, you can try to re-execute a task with ID 5:

PHP Artisan queue:retry 5

You can use the Queue:retry all command to restart All tasks:

PHP Artisan Queue:retry All

If you want to delete a failed task, you can use the Queue:forget command:

PHP Artisan queue:forget 5

You can use the Queue:flush command to clear all failed tasks:

PHP Artisan Queue:flush
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.