Configure Supervisor to run Laravel Queue on Linux
- Tram Ho
Perhaps everyone is not familiar with Laravel Queue, so I will not re-introduce it. Everyone can go to the Laravel doc to read it!
As everyone knows for Queue to work, we have to run the command:
1 2 | php artisan queue:work |
Queue runs continuously without stopping, so if we use the terminal (or cmd), when we turn off the terminal / cmd, it will be considered that the queue will also turn off (annoying, isn’t it everyone). And today I will introduce all supervisor users to help us do this. Let’s find out together!
1. What is a Supervisor?
Supervisor is a tool to help manage processes running on Linux. Some advantages that Supervisor like:
- Make sure a certain process is always running non-stop. If for some reason it is disabled, Supervisor will restart the process.
- Automatically run a process on system startup.
- Manage multiple processes as a group of processes, which can be enabled/disabled at the same time.
- Log the process of running Queue Job.
- In the event that the process that the supervisor executes fails, it can be configured to have the Supervisor retry a certain number of times before officially reporting the failure.
2. Install Supervisor
To install Supervisor, people run the following command on Linux:
1 2 | sudo apt-get install supervisor |
After the installation is complete, supervisor can create a /etc directory on the machine.
3. Basic Configuration Supervisor
Each process supervised by Supervisor will be configured in a file of the form .conf located in the directory /etc/supervisor/conf.d
The structure of a basic config file is as follows:
1 2 3 4 5 6 7 8 9 10 11 | <span class="token punctuation">[</span>program<span class="token punctuation">:</span>laravel<span class="token operator">-</span>worker<span class="token punctuation">]</span> process_name<span class="token operator">=</span><span class="token operator">%</span><span class="token punctuation">(</span>program_name<span class="token punctuation">)</span>s_<span class="token operator">%</span><span class="token punctuation">(</span>process_num<span class="token punctuation">)</span><span class="token number">02</span>d directory<span class="token operator">=</span><span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>your<span class="token operator">/</span>project command<span class="token operator">=</span>sendmail<span class="token punctuation">.</span>sh autostart<span class="token operator">=</span><span class="token boolean constant">true</span> autorestart<span class="token operator">=</span><span class="token boolean constant">true</span> user<span class="token operator">=</span>forge numprocs<span class="token operator">=</span><span class="token number">8</span> redirect_stderr<span class="token operator">=</span><span class="token boolean constant">true</span> stdout_logfile<span class="token operator">=</span><span class="token operator">/</span>path<span class="token operator">/</span>to<span class="token operator">/</span>your<span class="token operator">/</span>project<span class="token operator">/</span>logs<span class="token operator">/</span>worker<span class="token punctuation">.</span>log |
- [program: name-any-name]: program name. Anyone can name anything.
- directory:path to the program to run.
- command: program wants to run.
- autostart: Specifies whether the process will start automatically as soon as Supervisor is started. Usually Supervisor is run at boot, so whether or not the process can run automatically depends on boot.
- autorestart:Specifies whether the process will start automatically as soon as it is shut down for some reason (completed, crashed, timeout…) or not.
- user: user used to execute the process.
- numprocs: the number of processes that Supervisor will run. For example, to send mail, we let numprocs=4, which means that 4 processes run the task of sending mail.
- redirect_stderr: if true, stderr will be redirected to output to the file specified in stdout_logfile
- stdout_logfile: path to your log file. This file does not need to be created first because the supervisor will create it for himself
After the configuration is complete, run the following command to restart supervisor
1 2 3 4 | systemctl restart supervisord # or sudo service supervisor restart |
Note: Every time you change the Supervisor configuration information, everyone needs to restart Supervisor.
4. Implement Supervisor to run Laravel Queue
- Create file laravel-worker.conf in directory /etc/supervisor/conf.d and edit the file
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token punctuation">[</span>program<span class="token punctuation">:</span>laravel<span class="token operator">-</span>worker<span class="token punctuation">]</span> <span class="token shell-comment comment"># mình thường để trùng với tên file cho dễ nhớ :)</span> process_name<span class="token operator">=</span><span class="token operator">%</span><span class="token punctuation">(</span>program_name<span class="token punctuation">)</span>s_<span class="token operator">%</span><span class="token punctuation">(</span>process_num<span class="token punctuation">)</span><span class="token number">02</span>d directory<span class="token operator">=</span><span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>www<span class="token operator">/</span>laravel <span class="token shell-comment comment"># mọi người tự thay cho phù hợp nhé</span> command<span class="token operator">=</span>php artisan queue<span class="token punctuation">:</span>work <span class="token operator">--</span>sleep<span class="token operator">=</span><span class="token number">1</span> <span class="token operator">--</span>tries<span class="token operator">=</span><span class="token number">1</span> autostart<span class="token operator">=</span><span class="token boolean constant">true</span> autorestart<span class="token operator">=</span><span class="token boolean constant">true</span> user<span class="token operator">=</span>root <span class="token shell-comment comment"># user thực hiện command</span> numprocs<span class="token operator">=</span><span class="token number">2</span> redirect_stderr<span class="token operator">=</span><span class="token boolean constant">true</span> <span class="token shell-comment comment"># Nếu có lỗi sẽ được log vào file này</span> stdout_logfile<span class="token operator">=</span><span class="token operator">/</span><span class="token keyword">var</span><span class="token operator">/</span>www<span class="token operator">/</span>laravel<span class="token operator">/</span>storage<span class="token operator">/</span>logs<span class="token operator">/</span>worker<span class="token punctuation">.</span>log |
- Start the queue
1 2 3 4 | sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start laravel-worker:* |
5. Some commonly used commands
Command | Detail |
---|---|
sudo service supervisor restart | Restart all supervisors |
sudo supervisorctl start laravel-worker:* | Start worker laravel-worker:* |
sudo supervisorctl stop laravel-worker:* | Stop worker laravel-worker:* |
sudo supervisorctl restart laravel-worker:* | Restart worker laravel-worker:* |
Conclude
So with supervisor, it will be easier to manage and run Laravel Queue, we also have less to worry about how its process is running, unless there is an error, we have to check the log.
Hope this article is helpful to everyone.
Thanks for reading
References http://supervisord.org/index.html
https://phambinh.net/bai-viet/cau-hinh-laravel-queue-tren-moi-truong-production/