Supervisor啟動並管理Celery相關程序

2023-06-15 18:00:41

Supervisor啟動並管理Celery相關程序

關於celery在執行過程中, 預設情況下是無法在關機以後自動重啟的。所以我們一般開發中會使用supervisor程序監控來對celery程式進行執行監控!當celery沒有啟動的情況下,supervisor會自動啟動celery,所以我們需要安裝supervisor並且編寫一個supervisor的控制指令碼,在指令碼中編寫對celery進行啟動的命令即可。

1. 安裝和啟動celery任務監控器

針對celery中的任務執行過程,我們也可以安裝一個flower的工具來進行監控。

pip install flower
cd /home/moluo/Desktop/luffycity/luffycityapi
# 保證celery在啟動中
celery -A luffycityapi worker -l INFO
# 再啟動celery-flower
celery -A luffycityapi flower --port=5555

http://localhost:5555

attention: 這裡啟動了測試之後就可以關掉了, 因為後面會使用supervisor啟動flower, 防止佔用埠

2. supervisor啟動celery&flower

Supervisor是用Python開發的一套通用的程序管理程式,能將一個普通的命令列程序變為系統守護行程daemon,並監控程序狀態,異常退出時能自動重啟。

pip install supervisor
# 注意:如果supervisor是安裝在虛擬環境的,則每次使用supervisor務必在虛擬環境中進行後面所有的操作
# conda activate luffycity

supervisor設定檔案:http://supervisord.org/configuration.html

對Supervisor初始化設定

# 在專案根目錄下建立儲存supervisor設定目錄,在luffycityapi建立scripts目錄,已經建立則忽略
conda activate luffycity
cd /home/ifeng/Desktop/luffycity/luffycityapi
mkdir -p scripts && cd scripts
# 生成初始化supervisor核心組態檔,echo_supervisord_conf是supervisor安裝成功以後,自動附帶的。
echo_supervisord_conf > supervisord.conf
# 可以通過 ls 檢視scripts下是否多了supervisord.conf這個檔案,表示初始化設定生成了。
# 在編輯器中開啟supervisord.conf,並去掉最後一行的註釋分號。
# 修改如下,表示讓supervisor自動載入當前supervisord.conf所在目錄下所有ini組態檔

supervisord/conf.py,主要修改檔案中的39, 40,75,76,169,170行去掉左邊註釋,其中170修改成當前目錄。設定程式碼:

; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html
;
; Notes:
;  - Shell expansion ("~" or "$HOME") is not supported.  Environment
;    variables can be expanded using this syntax: "%(ENV_HOME)s".
;  - Quotes around values are not supported, except in the case of
;    the environment= options as shown below.
;  - Comments must have a leading space: "a=b ;comment" not "a=b;comment".
;  - Command will be truncated if it looks like a config file comment, e.g.
;    "command=bash -c 'foo ; bar'" will truncate to "command=bash -c 'foo ".
;
; Warning:
;  Paths throughout this example file use /tmp because it is available on most
;  systems.  You will likely need to change these to locations more appropriate
;  for your system.  Some systems periodically delete older files in /tmp.
;  Notably, if the socket file defined in the [unix_http_server] section below
;  is deleted, supervisorctl will be unable to connect to supervisord.

[unix_http_server]
file=/tmp/supervisor.sock   ; the path to the socket file
;chmod=0700                 ; socket file mode (default 0700)
;chown=nobody:nogroup       ; socket file uid:gid owner
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

; Security Warning:
;  The inet HTTP server is not enabled by default.  The inet HTTP server is
;  enabled by uncommenting the [inet_http_server] section below.  The inet
;  HTTP server is intended for use within a trusted environment only.  It
;  should only be bound to localhost or only accessible from within an
;  isolated, trusted network.  The inet HTTP server does not support any
;  form of encryption.  The inet HTTP server does not use authentication
;  by default (see the username= and password= options to add authentication).
;  Never expose the inet HTTP server to the public internet.

[inet_http_server]         ; inet (TCP) server disabled by default
port=127.0.0.1:9001        ; ip_address:port specifier, *:port for all iface
;username=user              ; default is no username (open server)
;password=123               ; default is no password (open server)

[supervisord]
logfile=/tmp/supervisord.log ; main log file; default $CWD/supervisord.log
logfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MB
logfile_backups=10           ; # of main logfile backups; 0 means none, default 10
loglevel=info                ; log level; default info; others: debug,warn,trace
pidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pid
nodaemon=false               ; start in foreground if true; default false
silent=false                 ; no logs to stdout if true; default false
minfds=1024                  ; min. avail startup file descriptors; default 1024
minprocs=200                 ; min. avail process descriptors;default 200
;umask=022                   ; process file creation umask; default 022
;user=supervisord            ; setuid to this UNIX account at startup; recommended if root
;identifier=supervisor       ; supervisord identifier, default is 'supervisor'
;directory=/tmp              ; default is not to cd during start
;nocleanup=true              ; don't clean up tempfiles at start; default false
;childlogdir=/tmp            ; 'AUTO' child log dir, default $TEMP
;environment=KEY="value"     ; key value pairs to add to environment
;strip_ansi=false            ; strip ansi escape codes in logs; def. false

; The rpcinterface:supervisor section must remain in the config file for
; RPC (supervisorctl/web interface) to work.  Additional interfaces may be
; added by defining them in separate [rpcinterface:x] sections.

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

; The supervisorctl section configures how supervisorctl will connect to
; supervisord.  configure it match the settings in either the unix_http_server
; or inet_http_server section.

[supervisorctl]
; serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL  for a unix socket
serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket
;username=chris              ; should be same as in [*_http_server] if set
;password=123                ; should be same as in [*_http_server] if set
;prompt=mysupervisor         ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history  ; use readline history if available

; The sample program section below shows all possible program subsection values.
; Create one or more 'real' program: sections to be able to control them under
; supervisor.

;[program:theprogramname]
;command=/bin/cat              ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=999                  ; the relative start priority (default 999)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; when to restart if exited after running (def: unexpected)
;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=true          ; redirect proc stderr to stdout (default false)
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_capture_maxbytes=1MB   ; number of bytes in 'capturemode' (default 0)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions (def no adds)
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample eventlistener section below shows all possible eventlistener
; subsection values.  Create one or more 'real' eventlistener: sections to be
; able to handle event notifications sent by supervisord.

;[eventlistener:theeventlistenername]
;command=/bin/eventlistener    ; the program (relative uses PATH, can take args)
;process_name=%(program_name)s ; process_name expr (default %(program_name)s)
;numprocs=1                    ; number of processes copies to start (def 1)
;events=EVENT                  ; event notif. types to subscribe to (req'd)
;buffer_size=10                ; event buffer queue size (default 10)
;directory=/tmp                ; directory to cwd to before exec (def no cwd)
;umask=022                     ; umask for process (default None)
;priority=-1                   ; the relative start priority (default -1)
;autostart=true                ; start at supervisord start (default: true)
;startsecs=1                   ; # of secs prog must stay up to be running (def. 1)
;startretries=3                ; max # of serial start failures when starting (default 3)
;autorestart=unexpected        ; autorestart if exited after running (def: unexpected)
;exitcodes=0                   ; 'expected' exit codes used with autorestart (default 0)
;stopsignal=QUIT               ; signal used to kill process (default TERM)
;stopwaitsecs=10               ; max num secs to wait b4 SIGKILL (default 10)
;stopasgroup=false             ; send stop signal to the UNIX process group (default false)
;killasgroup=false             ; SIGKILL the UNIX process group (def false)
;user=chrism                   ; setuid to this UNIX account to run the program
;redirect_stderr=false         ; redirect_stderr=true is not allowed for eventlisteners
;stdout_logfile=/a/path        ; stdout log path, NONE for none; default AUTO
;stdout_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stdout_logfile_backups=10     ; # of stdout logfile backups (0 means none, default 10)
;stdout_events_enabled=false   ; emit events on stdout writes (default false)
;stdout_syslog=false           ; send stdout to syslog with process name (default false)
;stderr_logfile=/a/path        ; stderr log path, NONE for none; default AUTO
;stderr_logfile_maxbytes=1MB   ; max # logfile bytes b4 rotation (default 50MB)
;stderr_logfile_backups=10     ; # of stderr logfile backups (0 means none, default 10)
;stderr_events_enabled=false   ; emit events on stderr writes (default false)
;stderr_syslog=false           ; send stderr to syslog with process name (default false)
;environment=A="1",B="2"       ; process environment additions
;serverurl=AUTO                ; override serverurl computation (childutils)

; The sample group section below shows all possible group values.  Create one
; or more 'real' group: sections to create "heterogeneous" process groups.

;[group:thegroupname]
;programs=progname1,progname2  ; each refers to 'x' in [program:x] definitions
;priority=999                  ; the relative start priority (default 999)

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = *.ini

建立luffycity_celery_worker.ini檔案,啟動我們專案worker主程序

cd /home/ifeng/Desktop/luffycity/luffycityapi/scripts
touch luffycity_celery_worker.ini
[program:luffycity_celery_worker]
# 啟動命令 conda env list
command=/home/ifeng/anaconda3/envs/luffycity/bin/celery -A luffycityapi worker -l info -n worker1
# 專案根目錄的絕對路徑[manage.py所在目錄路徑],通過pwd檢視
directory=/home/ifeng/Desktop/luffycity/luffycityapi
# 專案虛擬環境
enviroment=PATH="/home/ifeng/anaconda3/envs/luffycity/bin"
# 執行紀錄檔絕對路徑
stdout_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.worker.info.log
# 錯誤紀錄檔絕對路徑
stderr_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.worker.error.log
# 自動啟動,開機自啟
autostart=true
# 啟動當前命令的使用者名稱
user=ifeng
# 重啟
autorestart=true
# 程序啟動後跑了幾秒鐘,才被認定為成功啟動,預設1
startsecs=10
# 程序結束後60秒才被認定結束
stopwatisecs=60
# 優先順序,值小的優先啟動
priority=990

建立luffycity_celery_beat.ini檔案,來觸發我們的beat定時計劃任務

cd /home/ifeng/Desktop/luffycity/luffycityapi/scripts
touch luffycity_celery_beat.ini
[program:luffycity_celery_beat]
# 啟動命令 conda env list
command=/home/ifeng/anaconda3/envs/luffycity/bin/celery -A luffycityapi  beat -l info
# 專案根目錄的絕對路徑,通過pwd檢視
directory=/home/ifeng/Desktop/luffycity/luffycityapi
# 專案虛擬環境
enviroment=PATH="/home/ifeng/anaconda3/envs/luffycity/bin"
# 執行紀錄檔絕對路徑
stdout_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.beat.info.log
# 錯誤紀錄檔絕對路徑
stderr_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.beat.error.log
# 自動啟動,開機自啟
autostart=true
# 重啟
autorestart=true

# 程序啟動後跑了幾秒鐘,才被認定為成功啟動,預設1
startsecs=10

# 程序結束後60秒才被認定結束
stopwatisecs=60

# 優先順序,值小的優先啟動
priority=998

建立luffycity_celery_flower.ini檔案,來啟動我們的celery監控管理工具

cd /home/ifeng/Desktop/luffycity/luffycityapi/scripts
touch luffycity_celery_flower.ini
[program:luffycity_celery_flower]
# 啟動命令 conda env list
command=/home/ifeng/anaconda3/envs/luffycity/bin/celery -A luffycityapi flower --port=5555
# 專案根目錄的絕對路徑,通過pwd檢視
directory=/home/ifeng/Desktop/luffycity/luffycityapi
# 專案虛擬環境
enviroment=PATH="/home/ifeng/anaconda3/envs/luffycity/bin"
# 輸出紀錄檔絕對路徑
stdout_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.flower.info.log
# 錯誤紀錄檔絕對路徑
stderr_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.flower.error.log
# 自動啟動,開機自啟
autostart=true
# 重啟
autorestart=true

# 程序啟動後跑了幾秒鐘,才被認定為成功啟動,預設1
startsecs=10

# 程序結束後60秒才被認定結束
stopwatisecs=60

# 優先順序
priority=999

啟動supervisor,確保此時你在專案路徑下

cd ~/Desktop/luffycity/luffycityapi
supervisord -c scripts/supervisord.conf

通過瀏覽器存取http://127.0.0.1:9001

常用操作

命令 描述
supervisorctl stop program 停止某一個程序,program 就是程序名稱,例如在ini檔案首行定義的[program:程序名稱]
supervisorctl stop all 停止全部程序
supervisorctl start program 啟動某個程序,program同上,也支援啟動所有的程序
supervisorctl restart program 重啟某個程序,program同上,也支援重啟所有的程序
supervisorctl reload 載入最新的組態檔,停止原有程序並按新的設定啟動、管理所有程序
注意:start、restart、stop 等都不會載入最新的組態檔
supervisorctl update 根據最新的組態檔,啟動新設定或有改動的程序,設定沒有改動的程序不會受影響而重啟
ps aux | grep supervisord 檢視supervisor是否啟動

把supervisor註冊到ubuntu系統服務中並設定開機自啟

cd /home/ifeng/Desktop/luffycity/luffycityapi/scripts
touch supervisor.service

supervisor.service,設定內容,並儲存。需要通過conda env list 檢視當前的虛擬環境路徑

[Unit]
Description=supervisor
After=network.target

[Service]
Type=forking
ExecStart=/home/ifeng/anaconda3/envs/luffycity/bin/supervisord -n -c /home/ifeng/Desktop/luffycity/luffycityapi/scripts/supervisord.conf
ExecStop=/home/ifeng/anaconda3/envs/luffycity/bin/supervisorctl $OPTIONS shutdown
ExecReload=/home/ifeng/anaconda3/envs/luffycity/bin/supervisorctl $OPTIONS reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

設定開機自啟

# 建立紀錄檔檔案
sudo chmod 766 /tmp/supervisord.log
cd /home/ifeng/Desktop/luffycity/luffycityapi/scripts
# 賦予許可權
chmod 766 supervisor.service
# 複製到系統開啟服務目錄下
sudo cp supervisor.service /lib/systemd/system/
# 設定允許開機自啟
systemctl enable supervisor.service
# 判斷是否已經設定為開機自啟了
systemctl is-enabled  supervisor.service
# 通過systemctl檢視supervisor執行狀態
systemctl status supervisor.service
# 如果檢視服務狀態時無法啟動,則可以通過重啟linux系統來測試是否因為前面的終端已經執行了supervisor導致的。當然,也可以手動關閉supervisor以及相關的服務。
# supervisorctl stop all
# ps aux | grep supervisord
# kill -9 51564  # 注意: 9068是舉例的,具體看上一行的查詢結果

效果圖: