久久精品亚洲成a人|亚洲精品中文字幕乱码|男人添女人全身免费视频|久久免费精品国自产拍网站|桃色午夜日韩在线三区精品|91精品国产高清自在线看|亚洲午夜精品无码专区在线观|97超级碰碰碰久久久久app

您的位置:首頁>三文娛 >

Supervisor啟動并管理Celery相關進程

來源:博客園  

Supervisor啟動并管理Celery相關進程

關于celery在運行過程中, 默認情況下是無法在關機以后自動重啟的。所以我們一般開發(fā)中會使用supervisor進程監(jiān)控來對celery程序進行運行監(jiān)控!當celery沒有啟動的情況下,supervisor會自動啟動celery,所以我們需要安裝supervisor并且編寫一個supervisor的控制腳本,在腳本中編寫對celery進行啟動的命令即可。

1. 安裝和啟動celery任務監(jiān)控器

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


(資料圖片)

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

http://localhost:5555

attention: 這里啟動了測試之后就可以關掉了, 因為后面會使用supervisor啟動flower, 防止占用端口

2. supervisor啟動celery&flower

Supervisor是用Python開發(fā)的一套通用的進程管理程序,能將一個普通的命令行進程變?yōu)橄到y(tǒng)守護進程daemon,并監(jiān)控進程狀態(tài),異常退出時能自動重啟。

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

supervisor配置文檔:http://supervisord.org/configuration.html

對Supervisor初始化配置

# 在項目根目錄下創(chuàng)建存儲supervisor配置目錄,在luffycityapi創(chuàng)建scripts目錄,已經(jīng)創(chuàng)建則忽略conda activate luffycitycd /home/ifeng/Desktop/luffycity/luffycityapimkdir -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 defaultport=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.loglogfile_maxbytes=50MB        ; max main logfile bytes b4 rotation; default 50MBlogfile_backups=10           ; # of main logfile backups; 0 means none, default 10loglevel=info                ; log level; default info; others: debug,warn,tracepidfile=/tmp/supervisord.pid ; supervisord pidfile; default supervisord.pidnodaemon=false               ; start in foreground if true; default falsesilent=false                 ; no logs to stdout if true; default falseminfds=1024                  ; min. avail startup file descriptors; default 1024minprocs=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 socketserverurl=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

創(chuàng)建luffycity_celery_worker.ini文件,啟動我們項目worker主進程

cd /home/ifeng/Desktop/luffycity/luffycityapi/scriptstouch luffycity_celery_worker.ini
[program:luffycity_celery_worker]# 啟動命令 conda env listcommand=/home/ifeng/anaconda3/envs/luffycity/bin/celery -A luffycityapi worker -l info -n worker1# 項目根目錄的絕對路徑[manage.py所在目錄路徑],通過pwd查看directory=/home/ifeng/Desktop/luffycity/luffycityapi# 項目虛擬環(huán)境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# 進程啟動后跑了幾秒鐘,才被認定為成功啟動,默認1startsecs=10# 進程結(jié)束后60秒才被認定結(jié)束stopwatisecs=60# 優(yōu)先級,值小的優(yōu)先啟動priority=990

創(chuàng)建luffycity_celery_beat.ini文件,來觸發(fā)我們的beat定時計劃任務

cd /home/ifeng/Desktop/luffycity/luffycityapi/scriptstouch luffycity_celery_beat.ini
[program:luffycity_celery_beat]# 啟動命令 conda env listcommand=/home/ifeng/anaconda3/envs/luffycity/bin/celery -A luffycityapi  beat -l info# 項目根目錄的絕對路徑,通過pwd查看directory=/home/ifeng/Desktop/luffycity/luffycityapi# 項目虛擬環(huán)境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# 進程啟動后跑了幾秒鐘,才被認定為成功啟動,默認1startsecs=10# 進程結(jié)束后60秒才被認定結(jié)束stopwatisecs=60# 優(yōu)先級,值小的優(yōu)先啟動priority=998

創(chuàng)建luffycity_celery_flower.ini文件,來啟動我們的celery監(jiān)控管理工具

cd /home/ifeng/Desktop/luffycity/luffycityapi/scriptstouch luffycity_celery_flower.ini
[program:luffycity_celery_flower]# 啟動命令 conda env listcommand=/home/ifeng/anaconda3/envs/luffycity/bin/celery -A luffycityapi flower --port=5555# 項目根目錄的絕對路徑,通過pwd查看directory=/home/ifeng/Desktop/luffycity/luffycityapi# 項目虛擬環(huán)境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# 進程啟動后跑了幾秒鐘,才被認定為成功啟動,默認1startsecs=10# 進程結(jié)束后60秒才被認定結(jié)束stopwatisecs=60# 優(yōu)先級priority=999

啟動supervisor,確保此時你在項目路徑下

cd ~/Desktop/luffycity/luffycityapisupervisord -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根據(jù)最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟
ps aux | grep supervisord查看supervisor是否啟動

把supervisor注冊到ubuntu系統(tǒng)服務中并設置開機自啟

cd /home/ifeng/Desktop/luffycity/luffycityapi/scriptstouch supervisor.service

supervisor.service,配置內(nèi)容,并保存。需要通過conda env list 查看當前的虛擬環(huán)境路徑

[Unit]Description=supervisorAfter=network.target[Service]Type=forkingExecStart=/home/ifeng/anaconda3/envs/luffycity/bin/supervisord -n -c /home/ifeng/Desktop/luffycity/luffycityapi/scripts/supervisord.confExecStop=/home/ifeng/anaconda3/envs/luffycity/bin/supervisorctl $OPTIONS shutdownExecReload=/home/ifeng/anaconda3/envs/luffycity/bin/supervisorctl $OPTIONS reloadKillMode=processRestart=on-failureRestartSec=42s[Install]WantedBy=multi-user.target

設置開機自啟

# 創(chuàng)建日志文件sudo chmod 766 /tmp/supervisord.logcd /home/ifeng/Desktop/luffycity/luffycityapi/scripts# 賦予權(quán)限chmod 766 supervisor.service# 復制到系統(tǒng)開啟服務目錄下sudo cp supervisor.service /lib/systemd/system/# 設置允許開機自啟systemctl enable supervisor.service# 判斷是否已經(jīng)設置為開機自啟了systemctl is-enabled  supervisor.service# 通過systemctl查看supervisor運行狀態(tài)systemctl status supervisor.service# 如果查看服務狀態(tài)時無法啟動,則可以通過重啟linux系統(tǒng)來測試是否因為前面的終端已經(jīng)運行了supervisor導致的。當然,也可以手動關閉supervisor以及相關的服務。# supervisorctl stop all# ps aux | grep supervisord# kill -9 51564  # 注意: 9068是舉例的,具體看上一行的查詢結(jié)果

效果圖:

關鍵詞:

最新文章
枣庄市| 荥经县| 晋江市| 丰都县| 盐山县| 长岭县| 兰州市| 勐海县| 余庆县| 即墨市| 浮梁县| 天峨县| 海伦市| 广德县| 界首市| 荔浦县| 都匀市| 扶余县| 横山县| 博白县| 昔阳县| 霸州市| 清远市| 商丘市| 开阳县| 民权县| 崇义县| 菏泽市| 盐津县| 北流市| 永济市| 通辽市| 井陉县| 鲁甸县| 柘城县| 镇远县| 民勤县| 宁晋县| 扎赉特旗| 邵东县| 宁强县|