Laravel Queue and running it with Supervisor

tudip-logo

Tudip

24 September 2019

Queue:

A queue data structure is based on FIFO (First In First Out). That is the element that enters first in the queue, leaves first while popping the element from the queue.

This data structure is mostly used for scheduling processes. The queue is also used to store data which will be transmitted while sending and receiving the data.

queue-1

Laravel Queue:

Laravel Queue is an effective way of deferring the processing of a time-consuming task in your application. For example, we can use Laravel queue for sending out verification emails whenever a new user registers or shares a post.

laravel-queue

Configuration:

The configuration file for the queue is stored in config/queue.php. In that file, you will find the connection for many queue drivers that are included in the framework. If you want to discard queued job use a null queue driver.

I will be using a database as a queue driver setting up option.

In .env file add / edit following value

 

QUEUE_DRIVER=database

 

As it has sync as by default value in config/queue.php file.

Note: there are many other drivers are available to see the following doc for more reference:

https://laravel.com/docs/5.6/queues

 

Database tables:

In order to use the database queue driver, you will need a database table to hold the jobs. So run following command to create queue table schema migration.

php artisan queue:table”


Then run “php artisan migrate” to run migration for the queue table.

 

Creating Jobs:

Now, create a job, all the queueable jobs are stored in app/jobs

php artisan make:job ProcessEmail

This command creates a job directory in app directory with the ProcessEmail file.

<?php

namespace App\Jobs;

use Illuminate\Support\Facades\Mail;

use Illuminate\Bus\Queueable;

use Illuminate\Queue\SerializesModels;

use Illuminate\Queue\InteractsWithQueue;

use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Foundation\Bus\Dispatchable;


class ProcessEmail implements ShouldQueue

{

use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $emails;

/**

* Create a new job instance.

*

* @return void

*/

public function __construct($emails)

{

$this->emails = $emails;

}

/**

* Execute the job.

*

* @return void

*/

public function handle()

{

$content = $this->emails;

Mail::send($template, array('params' => $content['params']), function ($message) use ($content['to'], $content['cc'], $content['subject']) {

$message->from("info@tudip.com", "Tudip Technologies");

$message->to($content['to']);

if (!empty($content['cc'])) {

$message->cc($content['cc']);

}

$message->subject($content['subject']);

});

}

}

The handle method is called when the job is processed by the queue. The Laravel service container automatically injects these dependencies. If you would like to take total control over how the container injects dependencies into the handle method, you may use the container's bindMethod method.


Dispatching Jobs:

Once you have written your job class, you may dispatch it using the ‘dispatch’ method on the job itself. The arguments passed to the dispatch method will be given to the job’s constructor:

From your controller file you can dispatch job by adding:

ProcessEmail::dispatch($emailContent);

Processing Jobs:

For processing jobs there are two ways:

To run a command manually.

To process created job queue need to run following command

php artisan queue:work --tries=3

One approach to specifying the maximum number of times a job may be attempted is via the –tries switch on the Artisan command line:

You can also specify try to job file

public $tries = 5; like this way.

Use supervisor for processing jobs.

To automatically run the queue install the supervisor and then configure it.

The supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.

Supervisor Installation and configuration:

  1. Installation
sudo apt install supervisor

Check server is running after installation

service supervisor status

 

  1. Configuration

The configuration files for supervisor are typically stored in the /etc/supervisor/conf.d directory. In this directory, you can create any number of configuration files that instruct supervisor how your processes should be monitored. For example, we will create a queue.conf file that starts and monitors a queue:work process:

[program:queue]

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

command=php /var/www/html/laravel_app/artisan queue:work --tries=3 --sleep=3

user=root

autostart=true

autorestart=true

numprocs=10

redirect_stderr=true

stdout_logfile=/var/www/html/laravel_app/storage/logs/queue.log


In this example, the numprocs directive will instruct Supervisor to run 10 queue:work processes and monitor all of them, automatically restarting them if they fail.

 

  1. Updating Supervisor:

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

 

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start queue:*

 

For more information on Supervisor, consult the Supervisor documentation.

 

 

Request a quote