Understanding the need of laravel queues and setup

Saibal Roy • December 3, 2021

laravel-queues

Caveats for long running http requests

Queues in Laravel

For this reason to process http requests asynchronously we use jobs and queues.

Create a job

php artisan make:job SendWelcomeEmail

Simulate a long running task in the job handle method of app/Jobs/SendWelcomeEmail.php

public function handle()
  {
    sleep(3); // To simulate a log running task
    \info('This job is executed'); // This will be log in the storage/logs/laravel.log file
  }

Configure queue

config/queue.php
In the .env : QUEUE_CONNECTION=database (Choose any driver offered in queue.php)
php artisan queue:table
php artisan migrate

Trigger the job in routes/web.php


Route::get('/', function () {
\App\Jobs\SendWelcomeEmail::dispatch();
});

Running laravel queue worker

php artisan queue:work

Things to remember:

Happy learning folks!