In this article, we will see how to set up a supervisor in laravel 10. Here, we will learn about supervisors. The supervisor is a client/server system that allows its users to monitor and control a number of processes on UNIX-like operating systems.
The supervisor is configured through a simple INI-style config file that’s easy to learn. It provides many per-process options, such as restarting failed processes and automatic log rotation.
In production, you need a way to keep your queue:work
processes running. A queue:work
process may stop running for various reasons, such as an exceeded worker timeout or the execution of the queue:restart
command.
For this reason, you need to configure a process monitor that can detect when your queue:work
processes exit and automatically restart them.
So, let's see laravel 10 setup supervisor, how to install supervisor in ubuntu, install supervisor ubuntu laravel, how to configure supervisor in ubuntu.
In this step, we will install the laravel 10 application using the composer command.
composer create-project --prefer-dist laravel/laravel laravel_10_supervisor_example
Now, we will create jobs and failed_jobs table using the following command.
// to generate jobs table in migrations
php artisan queue:table
// then run migrate after the jobs migrations generated
php artisan migrate
The supervisor is a process monitor for the Linux operating system, and will automatically restart your queue:work
processes if they fail. To install Supervisor on Ubuntu, you may use the following command.
//install supervisor
sudo apt-get install supervisor
//check supervisor status after the installation
sudo systemctl status supervisor
After that, we will configure the laravel project file for our supervisor worker.
# then navigate supervisor config folder
cd /etc/supervisor/conf.d
# then create Laravel project configuration file
sudo nano laravel-worker.conf
For example, let's create a laravel-worker.conf
file that starts and monitors queue:work
processes.
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/forge/app.com/artisan queue:work sqs --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=forge
numprocs=8
redirect_stderr=true
stdout_logfile=/home/forge/app.com/worker.log
stopwaitsecs=3600
NOTE:
1. Be sure that you input your exact project directory for replacing /var/www/project-name.com/public_html
2. user value must be your logged user name.
3. Also the stdout_logfile change to your project directory.
So, run the following command.
//to read the new supervisor configurations
sudo supervisorctl reread
//then activate the new configuration
sudo supervisorctl update
//to start the queue command
//take note that this is the name of our config file above.
//You can change it depending on your project name.
sudo supervisorctl start laravel-worker:*
//then check status if configured correctly
sudo supervisorctl status
Also, you can use another method like the below code example.
// it will stop all queue
sudo supervisorctl stop all
// then read the new configuration
sudo supervisorctl reread
// then update so that it will activate the newly added
sudo supervisorctl update
// then start all queues
sudo supervisorctl start all
// then check the status
sudo supervisorctl status
You might also like: