[Laravel] Using Supervisor with Laravel Queue Jobs

本文最後更新於:2024年7月7日 晚上

Using Supervisor with Laravel Queue Jobs

When working with queue jobs in Laravel, it is important to ensure that the queue:work command can continuously run on the server. If the process stops, it should be able to restart automatically. This is where the Supervisor tool comes in handy.

1. Installing Supervisor

apt update
apt install supervisor

2. Starting Supervisor

# supervisord -c <configuration file path> (default is /etc/supervisor/supervisord.conf)
sudo supervisord -c /etc/supervisor/supervisord.conf

3. Creating the Queue Worker Configuration File

cd /etc/supervisor/conf.d
vim laravel-worker.conf

Configuration file content (remove comments):

[program:laravel-worker-my-project]
process_name=%(program_name)s_%(process_num)02d
# command=php <project directory path>/artisan queue:work --sleep=<interval> --tries=<maximum attempts>
command=php /var/www/my-project/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
# Execution user, recommended to use the same user as the web server. Start Supervisor with root privileges to avoid permission issues.
user=root
# Number of processes
numprocs=2
startsecs=0
redirect_stderr=true
# stdout_logfile=<log file path>
stdout_logfile=/var/log/supervisor/laravel-worker-my-project.log
# logfile_maxbytes=<log file size limit in bytes> (example is 10MB)
logfile_maxbytes=10485760
stopwaitsecs=3600

4. Loading and Enabling the Configuration File

supervisorctl reread
supervisorctl update
supervisorctl restart laravel-worker-my-project:*
# You can also use "all" to specify all configuration files
supervisorctl restart all

5. Checking If the Service is Running

supervisorctl status

6. Additional Operations

# Stopping
# supervisorctl stop <program_name>
supervisorctl stop laravel-worker-my-project:*
supervisorctl stop all

# Starting
# supervisorctl start <program_name>
supervisorctl start laravel-worker-my-project:*
supervisorctl start all

Environment

  • OS: Ubuntu 18.04

References


[Laravel] Using Supervisor with Laravel Queue Jobs
https://hankz1108.github.io/posts/20240707-en-20231105-laravel-queue-with-supervisor/
作者
Hankz
發布於
2024年7月7日
更新於
2024年7月7日
許可協議