How To Configure Supervisor For Laravel 5/6/7 Queue Jobs?

A Laravel Queue and running it with Supervisor data structure is based on FIFO (First In First Out). That is the element that enters first in the laravel queue monitor, leaves first while popping the element from the queue.

How Laravel Queues work?

This data structure is mostly help for scheduling processes. The laravel check if queue is running is also help to save horizon information which will be transmitted while sending with receiving the information laravel clear queue.

Laravel Queue:

Laravel Queue is an effective method of deferring the step by step processing of a time-consuming task based system in your web projects. For example, I can use Laravel queue jobs for sending out some imp confirm verification emails whenever a fresh user signups or shares a articles.

laravel queue tutorial

Laravel Supervisor Configuration:

The configuration settings file for the queue is saved in config/queue.php. In that main file, you will get the settings connection for many queue drivers that are added in the laravel framework. If you want to discard queued job use a null queue driver & laravel run queue in background.

I will be using a database as a laravel multiple queues driver setting up option.

In laravel main .env file add / edit and update below information

QUEUE_DRIVER=database

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

Imp Note: there are many other some imp drivers are available to see the below doc for more reference:

https://laravel.com/docs/7.x/queues

Database tables:

In order to use the database queue driver, you will required a database table to hold the jobs. therefor you can run below command to make queue table schema db migration.

php artisan queue:table

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

Creating Jobs:

And then, You can a make a job, all the queueable jobs are stored in app/jobs

php artisan make:job SendAllProcessMail

Laravel Supervisor Queues Step By Step Tutorial

This command creates a job folder in app folder with the SendAllProcessMail file.

emails = $emails;
	}

	/**
	* Execute the job.
	*
	* @return void
	*/

	public function handle()
	{

		$mailinfo = $this->emails;

		Mail::send($template, array('params' => $mailinfo['params']), function ($message) use ($mailinfo['to'], $mailinfo['cc'], $mailinfo['subject']) {
			$message->from("[email protected]", "Tudip Technologies");
			$message->to($mailinfo['to']);
			if (!empty($mailinfo['cc'])) {
				$message->cc($mailinfo['cc']);

			}

			$message->subject($mailinfo['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 including:

SendAllProcessMail::dispatch($emailContent);

Processing Jobs:

For processing jobs there are two methods:

  • To run a command manually.
  • To process created job queue need to run below command

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 method.

Configure Supervisor for Laravel Queues

Use supervisor for processing jobs.

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

The supervisor is a client/server based system that allows its users to monitor as well as some control a number of processes on UNIX-like operating systems same as a laravel queue:listen.

Supervisor Installation and some update configuration:

Step 1: INSTALL SUPERVISOR

sudo apt install supervisor

here you can simply Check server is running after installation process

service supervisor status

Step 2: Configuration : CREATE SUPERVISOR CONFIGURATION FILE FOR QUEUE

The configuration files for supervisor are typically saved in the /etc/supervisor/conf.d folder. In this folder, you can make any total number of configuration files that instruct supervisor how your processes should be monitored. For Supervisor example, I will make a queue.conf file that starts as well as monitors a queue:work process:

[program:queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/shooping_add_web_application/artisan queue:work --tries=3 --sleep=3
user=root
autostart=true
autorestart=true
numprocs=10
redirect_stderr=true
stdout_logfile=/var/www/html/shooping_add_web_application/storage/logs/queue.log

In this Supervisor example, the total numprocs directive will instruct Supervisor to run 10 queue:work processes as well as check with monitor all of them, with repetaly automatically restarting them if they jobs fail.

Step 3: Updating Supervisor: START THE PROCESSES IN SUPERVISOR

and last the every settings to configuration file has been done, you may all the settings update the Supervisord main file some data configuration as well as start the all the processes using the below some commands:
Additional Supervisor Command

sudo supervisorctl reread

sudo supervisorctl update

sudo supervisorctl start queue:*

I hope you get an idea about Laravel Supervisor.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment