Laravel Queue Tutorial with Supervisor Process Control
Posted on June 7th, 2019
Laravel Queues are very powerful to execute multiple processes in the background. In this Laravel Queue tutorial, I will show you how to set up workers in Supervisor to run your Laravel Queues 24*7.
If you are new to using queues, you might face some issues grasping the concept and reason behind queues. If you are using Laravel, There is one good news for you. In Laravel, It is very easy to perform tasks in background using Queues.
Here, we are not talking about messaging queues. Sure, We can use queues to send automated E-mail notifications to customers or subscribers. But we can also perform many other tasks in background using queues.
In Laravel, you can queue almost any task. You can queue and specific job, a notification, a mail, a command and much more.
It means, You can execute many kinds of processes in background to improve the user experience on the application. Now, let’s understand how queues work so that we can get started with the laravel queue tutorial as fast as possible.
How Laravel Queues work?
Let’s say we want to send an E-mail notification called “Registration successful” the user. There are two ways to send a notification. First one is to send an E-mail straight away when a user registers. And the second one is to put an E-mail notification in a queue called Notifications.
In the first case, a user has to wait 2-3 seconds because the E-mail notification is being sent directly after clicking on register button. But in the second case, you can queue an E-mail notification in few milliseconds and tell the user that an E-mail has been sent.
Sending notification via queue will also take 2-3 seconds to deliver based on the traffic on queues, but it feels faster because the processes are executed in the background.
Also, queues are not limited to any specific number of tasks you can perform. You can run multiple workers using supervisor to process tasks even faster.
NOTE: Workers are the processes that run continuously on the server. Constantly waiting for a job to perform.
For example, if you want to run Notifications queue, you have to keep the following process running on the server.
$ php artisan queue:work –queue=Notifications
Everything is perfect, But how to keep these processes running even when we are not connected with the server via SSH? The solution is to use supervisor.
Configure Supervisor for Laravel Queues
I have further divided this guide into steps. Let’s first install Supervisor.
Supervisor is very easy to install, configure and manage processes. There are some good features that you can use to make Queues even more efficient, Like auto restart.
In this tutorial, I will create a Notifications queue and run it using supervisor with multiple workers. Here, Multiple workers basically means that we can execute same tasks in parallel. Which means, multiple workers will pick task from the queue at the same time and process all of them together.
Connect to your server via SSH and execute the following command to install Supervisor.
$ sudo apt-get install supervisor -y
It will only take few seconds to install and configure. The next thing we have to do is to create a configuration file for running our Notifications queue workers.
Create Supervisor configuration file for Queue
Now, we have to create a supervisor configuration file with information and instructions to run the processes. To create a configuration file, execute the following command.
$ sudo nano /etc/supervisor/conf.d/Notifications.conf
Now, paste the following code with your preferences and modifications in the file.
command=php /path/to/project/artisan queue:work --queue=Notifications --tries=3
Do not forget to replace Notifications with your queue name and the path to the project. Also, note the numprocs line in the configuration. Here, you can decide how many workers you want to run for this queue. If you keep it to 3, it will send 3 E-mail notifications concurrently.
Start the Processes in Supervisor
We have our configuration ready. But supervisor does not know about it yet. We have to reload the configuration and start the processes using Supervisor command line interface. To start supervisor command line interface, execute the following command.
$ sudo supervisorctl
Once you are in, execute the following commands to reread the files, update the configuration and start the processes.
> start Notifications:*
Now, execute the status command in supervisor command line interface and check if your processes are running or not. If the status on all your processes is RUNNING, you have finally configured Laravel queues using supervisor.
Start queuing the tasks you want to run in the background and improve the user experience. Let us know if you need help following the Laravel queue tutorial.