Friday, August 13, 2021

Integration of Supervisor in Laravel

Integration of Supervisor in Laravel


What is Supervisor in Laravel?


The supervisor is a process manager which Laravel suggests to use as a process monitor for queue workers. It will automatically start the queue worker in the background, even after the system has booted and will automatically restart the worker if the worker exits unexpectedly.
 
Why use it?

To automatically detect the jobs and process it as per the configuration. The supervisor is a client/server system that allows its users to monitor and control a number of processes.

How to use supervisor in Laravel?

Install supervisor by using the following command : 

sudo apt-get install supervisor 

After you have successfully installed supervisor, you will see a folder "supervisor" inside /etc/ with below mentioned file and folder.

supervisord.conf and conf.d folder 

If folder is not created, run this command: 

echo_supervisord_conf > /etc/supervisord.conf 

Now go to supervisor directory and run the command s below to add the configuration. 

touch laravel-worker.conf 

Now open laravel-worker.conf file and enter the configuration as per your requirements 

[program:laravel-worker] 
process_name=%(program_name)s_%(process_num)02d 
command=php <Laravel_Folder_Path>/artisan queue:work --tries=3 
autostart=true 
autorestart=true 
user=root 
numprocs=8 
redirect_stderr=true 
stdout_logfile=/var/log/supervisor/laravel-queue.log 

Save the file and scroll to the end of supervisord.conf in /etc/supervisor/supervisord.conf and change the path as mentioned below (if not available) 

files = /etc/supervisor/laravel-worker.conf 

 Now, Let's Start Supervisor to run the queue worker but before that lets check if supervisor is already running or not with the command below. 

 use ps -ef to check PID(process ID) and kill to finish the task. 

 Run : supervisord -c /etc/supervisor/supervisord.conf 

Run the following commands one by one after starting supervisor. 

sudo supervisorctl reread -- Restart all programs in configuration files 
sudo supervisorctl update -- Update configurations to supervisord 
sudo supervisorctl start laravel-worker:* -- Start a program 

 To check the status run the command: supervisorctl status

3 comments:

Integration of Queue in Laravel

Integration of Queue in Laravel Introduction Laravel Queue is to enhances the laravel application performance and provides a smooth proc...