Laravel Queue: A Comprehensive Guide

Laravel Queue: A Comprehensive Guide

Configuration

To configure the database and Redis for Laravel queue, you need to edit the config/database.php and config/queue.php files.

Database Configuration

In the config/database.php file, update the ‘mysql’ configuration as follows:

'Mysql' => [
    'Driver' => 'mysql',
    'Host' => 'localhost',
    'Port' => 3306,
    'Database' => 'demo_laravel',
    'Username' => 'demo',
    'Password' => '123456',
    'Charset' => 'utf8',
    'Collation' => 'utf8_unicode_ci',
    'Prefix' => '',
    'Strict' => false,
    'Engine' => null,
],

Redis Queue Configuration

In the config/queue.php file, update the ‘Queue’ configuration as follows:

'Queue' => [
    'Host' => '192.168.56.101',
    'Password' => null,
    'Port' => 6379,
    'Database' => 0,
],

Failed Jobs Table

To create the failed jobs table, execute the following commands:

php artisan queue:failed-table
php artisan migrate:install
php artisan migrate

Producer

The producer is responsible for throwing data into the queue. To create a producer, execute the following command:

php artisan make:console Demo/Test

Producer Code

Create a new file app/Console/Commands/Demo.php with the following code:

namespace App\Console\Commands\Demo;

use App\Jobs\Demo\HandleTest;
use Illuminate\Console\Command;
use Illuminate\Foundation\Bus\DispatchesJobs;

class Test extends Command
{
    use DispatchesJobs;

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'demo:test {--num=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'thrown into the data queue';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $num = $this->option('num');
        for ($i = 0; $i <= $num; $i++) {
            $queueName = 'demo_1'; // here can be started after the plurality of queues according to service modulo
            $job = (new HandleTest($i))->onConnection('redis')->onQueue($queueName);
            $this->dispatch($job);
        }
    }
}

Consumer

The consumer is responsible for consuming the queue contents. To create a consumer, execute the following command:

php artisan make:job Demo/HandleTest

Consumer Code

Create a new file app/Jobs/Demo/HandleTest.php with the following code:

namespace App\Jobs\Demo;

use App\Jobs\Job;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class HandleTest extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;

    /**
     * Create a new job instance.
     *
     * @param  mixed  $data
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        echo $this->data . PHP_EOL;
    }
}

Starting the Producer

To start the producer, execute the following command:

php artisan demo:test --num=2

Consumer Queue Management

To start the consumer queue, execute the following command:

php artisan queue:work redis --queue=demo_1 --daemon --tries=10

Failure Queue

If the queue fails, the task will be listed in the failed_jobs table. To retry the failed job, execute the following command:

php artisan queue:retry 1

This guide provides a comprehensive overview of the Laravel queue system, including configuration, producer, consumer, and failure queue management.