centos8(linux): 安裝使用supervisor管理laravel佇列

刘宏缔的架构森林發表於2024-07-08

一,什麼是supervisor

Supervisor是一個程序控制系統,它使使用者能夠監視和控制類unix作業系統程序。
它透過提供基於配置或事件啟動、停止和重新啟動程序的機制,幫助管理應該在系統中連續執行的程序。
對於需要控制和監視Linux或其他類unix作業系統上多個程序的狀態的開發人員和系統管理員來說,Supervisor非常有效。

二,安裝/管理supervisor

1,用yum從命令列安裝:

[root@blog ~]# yum install supervisor 

2,檢視版本:

[lhdop@blog ~]$ supervisord --version
4.2.2

3,檢視幫助:

[lhdop@blog ~]$ supervisord --help
[lhdop@blog ~]$ supervisorctl -h

4,檢視服務狀態:

[root@blog ~]# systemctl status supervisord.service
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: inactive (dead)

5,配置自啟動

# 開機自啟動
# systemctl enable supervisord 

6,啟動服務

# 啟動supervisord服務
# systemctl start supervisord 

7, 輸出預設配置:

[lhdop@blog ~]$ echo_supervisord_conf 

8, 檢視配置檔案:

[lhdop@blog etc]$ more /etc/supervisord.conf

三,用supervisor管理php的laravel佇列消費者

1,建立指定程序的配置檔案

[root@blog ~]# cd /etc/supervisord.d/
[root@blog supervisord.d]# vi laravel-work.ini 

程式碼:

[root@blog supervisorlogs]# more /etc/supervisord.d/laravel-work.ini
[program:laravel-work]
process_name=%(program_name)s_%(process_num)02d
command=/usr/local/soft/php7/bin/php /data/work/laraveltest/dignews/artisan queue:work --daemon --quiet --delay=3 --sleep=3 --tries=1
autostart=true
autorestart=true
user=root
#numprocs=8
redirect_stderr=true
stdout_logfile=/data/logs/supervisorlogs/queue.log

2, 啟動服務

[root@blog supervisord.d]# systemctl status supervisord.service
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@blog supervisord.d]# systemctl start supervisord.service
[root@blog supervisord.d]# systemctl status supervisord.service
● supervisord.service - Process Monitoring and Control Daemon
   Loaded: loaded (/usr/lib/systemd/system/supervisord.service; disabled; vendor preset: disabled)
   Active: active (running) since Mon 2024-07-08 09:16:05 CST; 3s ago
  Process: 23525 ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf (code=exited, status=0/SUCCESS)
 Main PID: 23528 (supervisord)
    Tasks: 2 (limit: 26213)
   Memory: 39.0M
   CGroup: /system.slice/supervisord.service
           ├─23528 /usr/bin/python3.6 /usr/bin/supervisord -c /etc/supervisord.conf
           └─23529 /usr/local/soft/php7/bin/php /data/work/laraveltest/dignews/artisan queue:work --daemon --quiet --delay=3 --sleep=3 --tries=1

Jul 08 09:16:05 blog systemd[1]: Starting Process Monitoring and Control Daemon...
Jul 08 09:16:05 blog systemd[1]: Started Process Monitoring and Control Daemon.

3,測試效果:

訪問生產訊息的controller後可以看到日誌中的輸出內容

[root@blog supervisord.d]# cd /data/logs/supervisorlogs/
[root@blog supervisorlogs]# more queue.log
接收到訊息:hello,隨機數:509;開始處理:
接收到訊息:hello,隨機數:898;開始處理:
接收到訊息:hello,隨機數:401;開始處理:            

相關文章