Laravel Job Queue task queue mechanism

Job queue task queue application scenario is to delay (asynchronously) processing of time-consuming tasks, such as sending mail, so as to greatly shorten the time of Web request and response.

1, queue

1.1 connection and queue

There is a connections configuration entry in the configuration file config/queue.php. This configuration item defines the specific connector of the background queue service, such as Amazon SQS, Beanstalk, or Redis.

Each kind of queue connector can have many queues. You can imagine the window queues of different businesses in the bank.

<?php

return [
    //Default queue connection name
    'default' => env('QUEUE_CONNECTION', 'sync'),

    //Queue connection name
    'connections' => [
        'sync' => [
            'driver' => 'sync',
        ],
    .
    .
    .
        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 90,
            'block_for' => null,
        ],

    ],
    //Failed queue connector
    'failed' => [
        'database' => env('DB_CONNECTION', 'mysql'),
        'table' => 'failed_jobs',
    ],
];

Note that each connector configuration example in connections in the queue configuration file has a queue attribute. For example, in redis above, the default value of its queue is default. When a new queue task is added to the specified connection, the value of this configuration item is the default listening queue. In other words, if you do not assign a specific queue name, the value of queue is also the queue to which the task is added by default.

1.2 using Redis queue connector

If we want to use Redis as the queue connector, we need to configure the following files:

1.2.1 add Redis package

Add "predis/predis": "~1.0" in the require section of composer.json, and then update composer update.

1.2.2 queue.php

First, you need to configure queue connection in. env. Because you plan to use redis now, you need to configure it as redis.

QUEUE_CONNECTION = redis

Then configure the connections section of queue.php to find the redis connection,

'redis' => [
      'driver' => 'redis',
      'connection' => 'default',
      'queue' => env('REDIS_QUEUE', 'default'),
      'retry_after' => 90,
      'block_for' => null,
],

The driver here is redis, so we need to find the redis configuration file. Note that the connection in redis is a sub connection of redis itself, and it should not be confused with the connection of queue.

Here we see that the sub connection of redis is default. In database.php, we see that redis has three sub connections, including default. The value default corresponding to connection is the default connection of redis in database.php. That is to say, the default of ['connections']['redis']['connection '] in queue.php points to the value under ['redis']['default'] in database.php. You can also modify it according to your own needs.

1.2.3 database.php

Edit the. env configuration file according to the required configuration items in the default connection, and fill the Redis? Host, Redis? Password, and Redis? Port in the. env configuration file into the corresponding value of Redis in your server.

<?php

return [

    'default' => env('DB_CONNECTION', 'mysql'),
.
.
.
    'redis' => [

        'client' => 'predis',

        'default' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],

        'cache' => [
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

    ],

];

2. Task

The next step is to write the actual operation class. Laravel provides the artisan command to simplify the creation of task classes:

php artisan make:job Test

In the app/Jobs directory, generate a Test.php task class.

<?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;

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

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //Business logic processing
        logger()->channel('abc')->info('test 456 at:'.date('Y-m-d H:i:s', time()));
    }
}

We do the corresponding business logic processing directly in the handle method.

3. Distribution task

Now write an entry function to push the task to the queue. Use the auxiliary function dispatch():

<?php

use App\Jobs\Test as TestJob;

class AbcController extends Controller
{
    public function test()
    {   
        dispatch(new Test('I am dispatching test job with 123'));
        var_dump('123 at'.date('Y-m-d H:i:s', time()));
        exit();
    }
}

Open queue

If all goes well, this is the last step. On the command line:

php artisan queue:work {connectionName} {--queue=queueName}

The connectionName here is the string key in connections in the queue.php file, such as sync, database, sqs, redis, etc. if this parameter is not included, the connection name pointed to by the default key in queue.php will be used.

Of course, you can also add connections in connections, such as

'abc' => [
            'driver' => 'redis',
            'connection' => 'default',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 90,
            'block_for' => null,
        ],

--queue=queueName this is to indicate a certain queue and start a specific queue. We see that in addition to sync, other connection settings have queue configuration. If this parameter is not added here, the configured queue will be started. The default value is default.

 

Note: before setting up the queue, make sure that other previous mechanisms have thrown tasks to the queue and whether there are unfinished tasks. If there is, and there is no need to execute any more, we can clear those tasks from the queue through the command php article cache: clear. But make sure that these tasks are useless and do not need to be performed again.

Tags: Programming Redis PHP Database MySQL

Posted on Wed, 18 Mar 2020 00:17:05 -0400 by pillarofautumn