Supervisor For Laravel Queue Scheduling


09th May 2021 2 mins read
Share On        


Hi in this article let's see how we can quickly set up Supervisor for Laravel Queue scheduling in Ubuntu server.


Step 1 - .env Configuration


Laravel 5.7 & above

QUEUE_CONNECTION=database


Laravel below 5.7

QUEUE_DRIVER=database

Step 2 - Database Table Migrations


The following command will create the jobs table to keep track of your queues.

php artisan queue:table

php artisan migrate

Step 3 - Your Queue Configuration In Code


You might have some code to send mail like below. Make sure to implement ShouldQueue Interface

use Illuminate\Contracts\Queue\ShouldQueue;

class UserChangedPasswordMail extends Mailable implements ShouldQueue
{
  ...
}

Step 4 - Testing In Development / Localhost


Run the following command to test if the queues are working or not in your localhost or development environment


php artisan queue:listen

Step 5 - In Production Server Using Supervisor


First, update your Ubuntu server for new package updates & then install Supervisor.

sudo apt-get update

sudo apt install supervisor


Thinking that your project is deployed in the following path in the Ubuntu server

/var/www/html/YOUR_PROJECT


Your Supervisor configurations can be created in the following path. You can create as many as configurations for single or multiple projects.


NOTE: queue-worker.conf is the name of the configuration file. You can rename it to anything. But make sure to keep the conf


cd /etc/supervisor/conf.d/

nano queue-worker.conf


Add the following code in queue-worker.conf


[program:queue-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/YOUR_PROJECT/artisan queue:work --sleep=3 --tries=5
autostart=true
autorestart=true
user=root
numprocs=4
redirect_stderr=true
stdout_logfile=/var/log/supervisor/worker.log
stopwaitsecs=3600


Few observations to keep in mind


Make sure to add the configuration file name without .conf in the following line

[program:queue-worker]


In the following (replace YOUR_PROJECT with the name of your project)

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

Step 6 - Restart Supervisor Configuration To Take Effect


Run the following commands to make the new configurations take to affect

#Read the new configuration file
sudo supervisorctl reread

#Update the process of supervisor to use the new configuration
sudo supervisorctl update

#Start the process queue-worker in this case make sure to replace this
sudo supervisorctl start queue-worker

#Reload the configuration to take effect
sudo supervisorctl reload

#Restart the supervisor service
sudo service supervisor restart

Few Handy Supervisor Commands


#Check the status of supervisorctl
sudo supervisorctl status

#Stop all supervisor processes
sudo supervisorctl stop all

#Start all supervisor processes
sudo supervisorctl start all

#Read the new configuration file
sudo supervisorctl reread

#Update the process of supervisor to use the new configuration
sudo supervisorctl update

#Start the process queue-worker in this case make sure to replace this
sudo supervisorctl start queue-worker

#Reload the configuration to take effect
sudo supervisorctl reload

#Restart the supervisor service
sudo service supervisor restart




AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing