Configuring Supervisor for a Laravel Queue Worker on Ubuntu
Background:
In the quest to ensure seamless and efficient background job processing in Laravel, many of us have turned to the trusty php artisan queue:work --daemon
command scheduled via a cronjob to run every 30 minutes. It’s a simple and straightforward approach, but it comes with its own set of challenges and potential pitfalls.
While the php artisan queue:work --daemon
command for Laravel queue processing is a choice, it can lead to challenges when employed as the sole method. Issues such as inconsistent job execution, potential memory leaks, and unreliable process management may arise. In this blog, we’ll touch on these concerns and introduce a more dependable solution using Supervisor.
Step 1: Create Supervisor Configuration File
Create a Supervisor configuration file for your Laravel queue worker:
sudo apt-get install supervisor sudo nano /etc/supervisor/conf.d/santoshm-laravel-worker.conf
Inside the file, add the following content:
[program:santoshm-laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /var/www/santoshm/artisan queue:work --sleep=3 --tries=3 autostart=true autorestart=true user=santosh // update with name username. redirect_stderr=true stdout_logfile=/var/www/santoshm/storage/logs/queue-worker.log
- Replace
/var/www/santoshm/
with the actual path to your Laravel project. - Customize the
--sleep
and--tries
options as needed for your application.
Step 2: Update Supervisor Configuration
After creating the configuration file, update Supervisor to recognize the new configuration:
sudo supervisorctl reread sudo supervisorctl update
Step 3: Start Supervisor
Start the Supervisor service:
sudo service supervisor start
Step 4: Check Supervisor Logs
Review the Supervisor logs to check for any errors or issues:
sudo cat /var/log/supervisor/supervisord.log
Step 5: Restart Supervisor (If Needed)
If Supervisor is running but not recognizing the new configuration, try restarting it:
sudo service supervisor restart
Step 6: Start the Laravel Queue Worker
Start the Laravel queue worker managed by Supervisor:
sudo supervisorctl start santoshm-laravel-worker
Step 7: Check Supervisor Status
You can use the following command to check the status of Supervisor and verify that the Laravel queue worker is running:
sudo service supervisor status
That’s it! You’ve successfully configured Supervisor to manage your Laravel queue worker with the name “santoshm.” The worker should now run reliably and automatically restart in case of any issues.