How to Implement Queue in Laravel

How to Implement Queue in Laravel

Hi guys,

Today, I faced a slowness issue while emailing multiple users. So, I have searched on the internet and found one solution. i.e. Laravel queue. So we implement queue in Laravel.

Sometimes tasks take too much time to complete, like importing a CSV file and shooting bulk emails. Etc. In this case, we do not want users to wait to complete this process. In that case, we should use a queue to separate these processes.

Step by Step guide to implement queue in laravel

Step 1: We are using a database driver for the Laravel queue. First, we can generate a migration file. Execute the below command.

php artisan queue:table

Now, you need to migrate to the table. Execute the following command.

php artisan migrate

Step 2: We need to notify Laravel that we will use the database driver by updating the .env file:

QUEUE_CONNECTION=database

Step 3: For executing the jobs. Execute the following command.

php artisan queue:listen

Step 4:  We must create the job to send the actual email to the user.

php artisan make:job SendEmailJob

Step 5: You change the handle method accordingly. For example, I am emailing a user.

/**
   * Execute the job.
   *
   * @return void
   */
 public function handle()
{
       Mail::send([], [], function ($message) use ($user,$html) {
                  $message->to($user['email'])
                   ->subject($this->subject)->setBody($html, 'text/html');
}

Step 6: We need to dispatch the newly created job from the EmailController. So that code will look like this. If you want to about tricks of Controller Check article.

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Jobs\SendEmailJob;

class EmailController extends Controller
{
      public function sendEmail()
      {
              dispatch(new SendEmailJob());
              echo "email sent";
       }
}

It will store a new job in the jobs table. Run the below command. It will execute all jobs stored in the database.

php artisan queue:listen

php artisan queue:work

For Production Server

Step 1: We need to install the supervisor on the server. This package helps you to process in the background. The supervisor is a process monitor for the Linux operating system. It will automatically restart your queue:listen to the process if it fails.

sudo apt-get install supervisor

Step 2: You may create many configuration files that instruct the supervisor on how your process should monitor. They often store supervisor configuration files in the /etc/supervisor/conf.d directory.

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/{USERNAME}/path to project/artisan queue:work --sleep=3 --tries=3 --queue=email,default
autostart=true
autorestart=true
user={USERNAME}
numprocs=8
redirect_stderr=true
stdout_logfile=/home/{USERNAME}/path to project/worker.log

For Eg. 

USERNAME => rohit ( we can find the user’s name. Open the terminal & run the ‘pwd’ command ‘/home/rohit’ )

Pathtoproject => /home/rohit/Videos/test/ ( project root folder path )

Step 3:  You may update the Supervisor configuration and start the processes using the following commands:

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start laravel-worker:*

We have successfully implement a queue in laravel application. Laravel queue is a very powerful tool to improve your application’s performance, especially when the application has many heavy, frequently executed tasks.

Thank you for reading this article. Please share this article with your friend circle. That’s it for the day. Stay Connected!
Cheers

Loading

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top