[Laravel 5.2 Document] service--queue

Source: Internet
Author: User

1. Introduction

The Laravel Queue Service provides a unified API for a variety of different background queues. Queues allow you to defer execution of time-consuming tasks, such as sending messages, to dramatically increase the speed of Web requests.

1.1 Configuration

The queue configuration file is stored in the config/queue.php. In this file you will find each queue-driven connection configuration that the framework comes with, including databases, BEANSTALKD, Ironmq, Amazon SQS, Redis, and synchronous (local-use) drivers. It also contains a null queue driver to reject the queue task.

1.2 Queue-driven readiness knowledge

Database

In order to use the database queue driver, you need a table to hold the task, to generate a migration to create the table, run the artisan command queue:table, after the migration is created, run the migration with the Migrate command:

PHP Artisan queue:tablephp Artisan Migrate

Other queue dependencies

The following is a list of dependencies that the queue driver needs to install:

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

2. Writing Task Classes

2.1 Generating a task class

By default, all queue tasks that are applied are stored in the App/jobs directory. You can use the artisan CLI to generate a new queue task:

PHP Artisan make:job Sendreminderemail

The command will generate a new class in the App/jobs directory, and the class implements the Illuminate\contracts\queue\shouldqueue interface, telling Laravel that the task should be pushed to the queue instead of running synchronously.

2.2 Task class Structure

The task class is very simple and normally contains only one handle method that is executed when the queue processes the task, so let's look at an example of a task class:

!--? 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\queue\shouldqueue;class Sendreminderemail extends Job implements shouldqueue{use Interactswithqueue, Serial    Izesmodels;    protected $user;    /** * Create a new Task instance * * @param user $user * @return void */Public function __construct (user $user)    {$this--->user = $user; }/** * Perform task * * @param Mailer $mailer * @return void */Public function handle (Mailer $mailer        {$mailer->send (' Emails.reminder ', [' user ' = ' + ' $this->user], function ($m) {//});    $this->user->reminders ()->create (...); }}

In this case, note that we are able to pass the eloquent model directly into the constructor of the column task. Because the task uses the Serializesmodelstrait,eloquent model, it is gracefully serialized and deserialized when the task is executed. If your queue task receives the eloquent model in the constructor, only the primary key of the model is serialized to the queue, and the queue system automatically fetches the entire model instance from the database when the task is actually executed. This is completely transparent to the application, avoiding the problem caused by serializing the entire eloquent model instance.

The handle method is called when the task is processed by the queue, and note that we can do dependency injection in the handle method of the task. These dependencies are automatically injected by the Laravel service container.

Error

If an exception is thrown when the task is processed, the task will be automatically freed back to the queue to attempt execution again. The task continues to be released knowing the maximum number of attempts to allow the application to be reached. The maximum number of attempts is defined by the--tries switch on the artisan task Queue:listen or queue:work. More information about running queue listeners can be seen below.

Manually Releasing tasks

If you want to manually release the task, the interactswithqueuetrait that comes with the generated task class provides the release method that releases the queue task, which receives a parameter-the wait time between two runs of the same task:

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

Check the number of attempts run

As mentioned above, if an exception occurs during task processing, the task is automatically released back into the queue, and you can check the number of times that the task has been tried by using the attempts method:

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

3. Push Task to queue

The default Laravel controller is located in app/http/controllers/controller.php and uses the dispatchesjobstrait. The trait provides some methods that allow you to easily push tasks to the queue, such as the Dispatch method:

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

dispatchesjobs Trait

Of course, there are times when you want to distribute tasks from somewhere in the app's middle or outside the controller, because for this reason, you can include dispatchesjobstrait in any class you apply to get access to the distribution method, for example, here's an example class that uses the trait:

     

Dispatch method

Alternatively, you can also use the global dispatch method:

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

To specify a queue for a task

You can also specify the queue to which the task is sent.

Depending on the queue to which the task is pushed, you can "categorize" the queue tasks, or even prioritize the number of workers assigned to multiple queues. This does not push the task to a different queue "connection" as defined in the queue configuration file, but only to a specific queue in a single connection. To specify this queue, use the Onqueue method on the task instance, which is provided by illuminate\bus\queueabletrait in the base class App\jobs\job that Laravel comes with:

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

3.1 Deferred tasks

Sometimes you might want to delay the execution of a queue task. For example, you might want to put a task that registers 15 minutes after the consumer sends a reminder message to the queue, which can be implemented by using the delay method on the task class, which is provided by illuminate\bus\queueabletrait:

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

In this example, we specify that the task is delayed by 60 seconds before it starts executing in the queue.

Note: The maximum delay time for Amazon SQS Services is 15 minutes.

3.2 Task Events

Task Completion Events

The Queue::after method allows you to register a callback function to execute after the queue task executes successfully. In this callback we can add the log, statistics data. For example, we can add an event callback in the Laravel built-in Appserviceprovider:

      

4. Running Queue Listener

Start Task Listener

Laravel contains a Artisan command to run a new task that is pushed to the queue. You can run the listener using the Queue:listen command:

PHP Artisan Queue:listen

You can also specify which queue connection the listener uses:

PHP Artisan Queue:listen Connection

Note Once the task starts, it will continue to run until it is manually stopped. You can use a process monitor such as supervisor to ensure that the queue listener does not stop running.

Queue priority

You can pass a comma-delimited list of queue connections to the Listen task to set the queue priority:

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

In this example, the tasks on the high queue are always processed before the task is moved from the low queue.

Specify task Timeout parameters

You can also set the maximum time (in seconds) that each task is allowed to run:

PHP Artisan Queue:listen--timeout=60

Specify the queue sleep time

In addition, you can specify the wait time (in seconds) before the new task is polled:

PHP Artisan Queue:listen--sleep=5

Note that the queue will only "sleep" when there are no tasks on the queue, and if there are multiple valid tasks, the queue will run continuously and never sleep.

4.1 Supervisor Configuration

Supervisor the process Monitor provided for the Linux operating system will automatically restart the Queue:listen or queue:work command on failure, and to install Supervisor on Ubuntu, use the following command:

sudo apt-get install Supervisor

Supervisor profiles are typically stored in the/ETC/SUPERVISOR/CONF.D directory, where you can create multiple profiles that indicate how supervisor monitors the process, for example, let's create an instance that opens and monitors the queue:work process. laravel-worker.conf file:

[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 this example, the Numprocs Directive lets supervisor run 8 queue:work processes and monitor them, and automatically restarts if they fail. After the configuration file has been created, you can update the supervisor configuration and turn on the process using the following command:

sudo supervisord-c/etc/supervisord.confsudo supervisorctl-c/etc/supervisor/supervisord.confsudo supervisorctl Rereadsudo supervisorctl updatesudo supervisorctl start laravel-worker:*

To learn more about the use and configuration of supervisor, view the supervisor documentation. In addition, you can use Laravel Forge to easily configure and manage Supervisor configurations from the Web interface.

4.2 Background Queue Listener

The artisan command queue:work contains a--daemon option to force the queue worker to continue processing tasks without having to restart the framework. The command has a significant decrease in CPU usage compared to the Queue:listen command:

PHP artisan queue:work Connection--daemonphp artisan queue:work connection--daemon--sleep=3php artisan queue:work Conne ction--daemon--sleep=3--tries=3

As you can see, the Queue:work task supports the most effective options in most queue:listen. You can use the PHP artisan help queue:work task to see all the available options.

Background Queue Listener Coding considerations

The background queue worker does not restart the framework when processing each task, so you need to release the resources before the task is complete, for example, if you are using the GD library to manipulate the picture, then use Imagedestroy to free up memory when you are done.

Similarly, the database connection should be disconnected after a long run in the background, and you can use the Db::reconnect method to ensure that a new connection is obtained.

4.3 Deployment of a background queue listener

Because the background queue worker is a resident process and does not restart, the changes in the code are not applied, so the simplest way to deploy a background queue worker is to restart all workers with the deployment script, and you can restart all workers by including the following command in the deployment script:

PHP Artisan Queue:restart

This command tells all queue workers to restart after completing the current task processing so that no tasks are missed.

Note: This command relies on the cache system restart progress table, which, by default, does not work in CLI tasks, and if you are using APC, you need to add Apc.enable_cli=1 to the APC configuration.

5. Handling Failed Tasks

Because things don't always go according to plan, sometimes your queue tasks fail. Don't worry, it happens to most of us! Laravel contains a convenient way to specify the maximum number of execution times for a task, which is inserted into the Failed_jobs table when the maximum number of task executions are reached, and the name of the failed task can be configured through the configuration file config/queue.php.

To create a migration of a failed_jobs table, you can use the queue:failed-table command:

PHP Artisan queue:failed-table

When running queue listeners, you can use the--tries switch on the queue:listen command to specify the maximum number of times a task can be attempted:

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

5.1 Failed Task events

If you want to register an event that is called when a queue task fails, you can use the Queue::failing method, which notifies the team by mail or Hipchat. For example, I can add a callback to the event in Laravel's Appserviceprovider:

       

Failure methods for task classes

For finer-grained control, you can define the failed method directly on the queue task class, allowing you to perform the specified action when the failure occurs:

        

5.2 Retry failed Task

To view all the failed tasks that have been inserted into the Failed_jobs data table, you can use the Artisan command queue:failed:

PHP Artisan queue:failed

The command lists the task ID, connection, column and time of failure, and the task ID can be used to retry the failed task, for example, to retry a failed task with ID 5, using the following command:

PHP Artisan queue:retry 5

To retry all failed tasks, use the following command:

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

To remove all failed tasks, you can use the Queue:flush command:

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.