How to Use Laravel Queue

In this blog, I will guide you through the concept of using Laravel queues and the benefits they bring, step by step, with practical examples. Before delving into how to use queues, let's first understand why we need to use Laravel Queues.

Why Use Laravel Queues?

When building a web application, certain tasks can be time-consuming, such as sending emails whenever a new user registers or processing entries from a CSV file into the database. Laravel queues are employed to handle such heavy tasks efficiently. They allow these tasks to run in the background, enhancing website speed and providing users with a smoother experience.

Key Benefits of Laravel Queues:

Improved Website Performance:

Queued jobs run asynchronously, preventing them from affecting the immediate user experience. This results in faster website performance.

Efficient Task Handling:

Tasks that take a considerable amount of time, like sending emails or processing files, are seamlessly handled in the background using queues.

Step 1: Identify Tasks for Queues

Think about tasks in your web app that take time, like sending emails or processing files. These are perfect candidates for Laravel queues.

Step 2: Configure Laravel for Queues

Open your Laravel project and head to the config/queue.php file. Set up your preferred queue connection, such as database or redis. This tells Laravel where to manage your queued jobs.

// config/queue.php

'connections' => [

'database' => [

'driver' => 'database',

'table' => 'jobs',

'queue' => 'default',

'retry_after' => 90,

],

],

Step 3: Set the Queue Connection

Find the QUEUE_CONNECTION variable in your .env file. If it doesn't exist, you can add it. Set the value to your preferred queue driver. For example, to use the database driver, add the following line:

QUEUE_CONNECTION=database

Step 4: Set Queue Worker Timeout (Optional)

If you want to specify a timeout for your queue worker processes, you can add the following line:

QUEUE_TIMEOUT=60

This example sets a timeout of 60 seconds for each job.

Step 5: Install Database for Queues

For this example, we'll use the database driver for simplicity. Install the required database tables using:

php artisan queue:table

php artisan migrate

Step 6: Define Your Job

Create a new job class that defines the task you want to perform. For instance, let's make a job to send a welcome email.

php artisan make:job SendWelcomeEmail

This command generates a new class named NewUserWelcomeMail in the App/Jobs directory. This class automatically queues up tasks to run in the background, making use of Laravel's ShouldQueue interface.

<?php



namespace App\Jobs;



use Illuminate\Bus\Queueable;

use Illuminate\Contracts\Queue\ShouldBeUnique;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Queue\SerializesModels;

use Illuminate\Support\Facades\Mail;

use App\Mail\UserWelcomeMail;



class SendWelcomeEmail implements ShouldQueue

{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $user;

/**

* Create a new job instance.

*

* @return void

*/

public function __construct($user)

{

$this->user = $user;

}



/**

* Execute the job.

*

* @return void

*/

public function handle()

{

Mail::to($this->user->email)->send(new UserWelcomeMail($this->user));

return 1;

}

}

Step 7: Dispatch the Job

In your code, dispatch the job when needed. This pushes the task to the background for efficient execution.

<?php



namespace App\Http\Controllers;



use App\Jobs\SendWelcomeEmail;

use App\Models\User;

use Illuminate\Http\Request;



class UserController extends Controller

{

public function create(Request $request)

{

// validate your request



// create a user

$user = User::create([

"name"=> $request->name,

"email" => $request->email

// other fields

]);



// dispatch your queue job

dispatch(new SendWelcomeEmail($user));



// return your response

}

}

Step 8: Run the Queue Worker

Open your terminal, navigate to your project, and run the Laravel queue worker. This will continuously process jobs from the queue.

php artisan queue:work

You can also use the queue:listen command. With this command, there's no need to manually restart the worker when you make changes to your code or want to reset the application state.

php artisan queue:listen

how to check queue failed jobs in laravel

To check failed jobs in Laravel, you can use the queue:failed Artisan command. Simply run:

php artisan queue:failed

This will display a list of all failed jobs, including the job ID, connection, queue, and the time of failure. You can also view the details of a specific failed job by using:

php artisan queue:failed show JOB_ID

Replace JOB_ID with the actual ID of the failed job you want to inspect.

How to retry failed jobs

To retry failed jobs in Laravel, you can use the queue:retry Artisan command. Follow these steps:

First, list the failed jobs to get the ID of the job you want to retry:

php artisan queue:failed

Take note of the job ID from the list.

Retry the failed job using:

php artisan queue:retry JOB_ID

Replace JOB_ID with the actual ID of the failed job you want to retry.

This command will requeue the specified failed job, giving it another chance to be processed.

To retry all failed jobs in Laravel, you can use the queue:retry command in combination with a loop to iterate over all failed job IDs.

php artisan queue:failed | awk '{print $1}' | xargs -I % php artisan queue:retry %

This command uses awk to extract the job IDs and then utilizes xargs to pass each ID to the queue:retry command. It will retry all the failed jobs.

Make sure to test this in a safe environment or during low traffic to avoid any unexpected behavior.

how to install supervisor on server

to run laravel queue on the server we need to install supervisor on the server.

To install Supervisor, a process control system for managing and monitoring processes on Unix-based systems, follow these steps:

For Ubuntu/Debian Systems:

Update Package Lists:

sudo apt-get update

Install Supervisor:

sudo apt-get install supervisor

Start Supervisor:

sudo service supervisor start

Enable Supervisor on Boot:

sudo systemctl enable supervisor

For CentOS/RHEL Systems:

Install EPEL Repository:

sudo yum install epel-release

Install Supervisor:

sudo yum install supervisor

Start Supervisor:

sudo service supervisord start

Enable Supervisor on Boot:

sudo systemctl enable supervisord

Configuration:

Verify Installation:

supervisorctl version

Edit Supervisor Configuration:

sudo nano /etc/supervisord.conf

Customize the configuration if needed.

Reload Supervisor:

sudo supervisorctl reread

sudo supervisorctl update

How to start a supervisor program for laravel queue

To start a Supervisor program for Laravel queues, follow these steps:

Create a Supervisor Configuration File:

Create a configuration file for Supervisor to manage your Laravel queue worker. You can create a new file, for example, laravel-queue-worker.conf:

sudo nano /etc/supervisor/conf.d/laravel-queue-worker.conf

Add Supervisor Configuration:

Inside the file, add the following configuration. Adjust the paths and options based on your Laravel project setup:

[program:laravel-queue-worker]

process_name=%(program_name)s_%(process_num)02d

command=php /path-to-your-laravel-project/artisan queue:work --sleep=3 --tries=3

autostart=true

autorestart=true

user=your-username

numprocs=8

redirect_stderr=true

stdout_logfile=/path-to-your-laravel-project/storage/logs/worker.log
  • command: Specify the full path to your Laravel project's artisan file and adjust any additional options like --sleep and --tries as needed.
  • user: Replace your-username with the username associated with your Laravel project.
  • numprocs: Set the number of worker processes based on your server's capabilities.
  • Save and Exit:
  • Save the file and exit the text editor.

Update Supervisor:

Reload the Supervisor configuration to apply the changes:

sudo supervisorctl reread

sudo supervisorctl update

Start the Laravel Queue Worker:

Start the Laravel queue worker using Supervisor:

sudo supervisorctl start laravel-queue-worker:*

This command starts all the configured Laravel queue worker processes.

Check Status:

Verify the status of the Supervisor program:

sudo supervisorctl status

Ensure that the laravel-queue-worker program is in the RUNNING state.

Your Laravel queue worker is now managed by Supervisor, ensuring it runs continuously and restarts if it encounters any issues. Adjust the configuration parameters according to your project's requirements.

Conclusion:

Laravel queues are a powerful tool for handling resource-intensive tasks in the background, enhancing overall website performance and providing users with a seamless experience. By understanding when and how to use queues, you can optimize your Laravel application for efficiency.

Remember, the key to a responsive and user-friendly web application often lies in leveraging the benefits of Laravel queues.

Feel free to explore the examples and steps provided in this blog to integrate Laravel queues seamlessly into your projects.

FAQs

Q: Why use Laravel Queues?

A: Laravel Queues are employed to handle time-consuming tasks, such as sending emails or processing files, in the background. This enhances website speed and provides users with a smoother experience.

Q: What are the benefits of Laravel Queues?

A: Laravel Queues improve website performance by running tasks asynchronously. They efficiently handle tasks that take a considerable amount of time, preventing them from affecting the immediate user experience.

Q: How do I configure Laravel for Queues?

A: Open the config/queue.php file and set up your preferred queue connection, such as database or redis. Also, configure the .env file with the desired queue driver and optional settings like timeout.

Q: How do I identify tasks for Laravel Queues?

A: Identify tasks in your web app that take time, like sending emails or processing files. These are perfect candidates for Laravel Queues.

Q: How do I create a new job in Laravel for Queues?

A: Use the php artisan make:job YourJob command to generate a new job class. This class defines the task you want to perform and automatically queues up tasks to run in the background.

Q: How do I dispatch a job in Laravel?

A: Dispatch the job in your code using dispatch(new YourJob($parameters)). This pushes the task to the background for efficient execution.

Q: How do I run the Laravel Queue Worker?

A: Open your terminal, navigate to your project, and run php artisan queue:work to continuously process jobs from the queue. Alternatively, use php artisan queue:listen for automatic restarting on code changes.

Q: How do I check failed jobs in Laravel?

A: Use the php artisan queue:failed command to display a list of all failed jobs. You can view details of a specific failed job using php artisan queue:failed show JOB_ID.

Q: How do I retry failed jobs in Laravel?

A: Retry a specific failed job using php artisan queue:retry JOB_ID. To retry all failed jobs, you can use a loop in combination with the queue:retry command.

Q: How do I install Supervisor on the server for Laravel Queue?

A: Follow the provided steps based on your server type (Ubuntu/Debian or CentOS/RHEL) to install Supervisor. Ensure Supervisor is started, enabled on boot, and customize the configuration if needed.

Q: How do I start a Supervisor program for Laravel Queue?

A: Create a Supervisor configuration file, add the Laravel queue worker configuration, and start the Supervisor program. Adjust parameters like the number of worker processes based on your project's requirements.

Q: What is the role of Supervisor in Laravel Queue?

A: Supervisor manages and monitors processes, ensuring continuous execution of Laravel queue worker processes. It restarts processes if they encounter issues, providing reliability.

Q: Can I customize the Laravel Queue Worker configuration?

A: Yes, you can customize the Laravel Queue Worker configuration by adjusting parameters such as the number of processes, sleep duration, and the maximum number of retry attempts.

Q: How do I integrate Laravel Queues into my projects?

A: Follow the step-by-step guide provided in the blog to identify tasks, configure Laravel for queues, create and dispatch jobs, run the queue worker, and utilize Supervisor for process management.

Q: Why are Laravel Queues important for web applications?

A: Laravel Queues are crucial for handling resource-intensive tasks in the background, enhancing overall website performance, and providing users with a seamless and responsive experience.

1 Comments

  • shaheryar bhatti

    shaheryar bhatti

    13 Dec 2023 09:38
    Thanks guys! let us know if you have any question.

Post Comment