來源:博客園
關(guān)于celery在運(yùn)行過程中, 默認(rèn)情況下是無法在關(guān)機(jī)以后自動重啟的。所以我們一般開發(fā)中會使用supervisor進(jìn)程監(jiān)控來對celery程序進(jìn)行運(yùn)行監(jiān)控!當(dāng)celery沒有啟動的情況下,supervisor會自動啟動celery,所以我們需要安裝supervisor并且編寫一個supervisor的控制腳本,在腳本中編寫對celery進(jìn)行啟動的命令即可。
1. 安裝和啟動celery任務(wù)監(jiān)控器針對celery中的任務(wù)執(zhí)行過程,我們也可以安裝一個flower的工具來進(jìn)行監(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: 這里啟動了測試之后就可以關(guān)掉了, 因?yàn)楹竺鏁褂胹upervisor啟動flower, 防止占用端口
2. supervisor啟動celery&flowerSupervisor是用Python開發(fā)的一套通用的進(jìn)程管理程序,能將一個普通的命令行進(jìn)程變?yōu)橄到y(tǒng)守護(hù)進(jìn)程daemon,并監(jiān)控進(jìn)程狀態(tài),異常退出時能自動重啟。
pip install supervisor# 注意:如果supervisor是安裝在虛擬環(huán)境的,則每次使用supervisor務(wù)必在虛擬環(huán)境中進(jìn)行后面所有的操作# conda activate luffycity
supervisor配置文檔:http://supervisord.org/configuration.html
對Supervisor初始化配置
# 在項(xiàng)目根目錄下創(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自動加載當(dāng)前supervisord.conf所在目錄下所有ini配置文件
supervisord/conf.py
,主要修改文件中的39, 40,75,76,169,170
行去掉左邊注釋,其中170修改成當(dāng)前目錄
。配置代碼:
; 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
文件,啟動我們項(xiàng)目worker主進(jìn)程
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# 項(xiàng)目根目錄的絕對路徑[manage.py所在目錄路徑],通過pwd查看directory=/home/ifeng/Desktop/luffycity/luffycityapi# 項(xiàng)目虛擬環(huán)境enviroment=PATH="/home/ifeng/anaconda3/envs/luffycity/bin"# 運(yùn)行日志絕對路徑stdout_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.worker.info.log# 錯誤日志絕對路徑stderr_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.worker.error.log# 自動啟動,開機(jī)自啟autostart=true# 啟動當(dāng)前命令的用戶名user=ifeng# 重啟autorestart=true# 進(jìn)程啟動后跑了幾秒鐘,才被認(rèn)定為成功啟動,默認(rèn)1startsecs=10# 進(jìn)程結(jié)束后60秒才被認(rèn)定結(jié)束stopwatisecs=60# 優(yōu)先級,值小的優(yōu)先啟動priority=990
創(chuàng)建luffycity_celery_beat.ini
文件,來觸發(fā)我們的beat定時計劃任務(wù)
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# 項(xiàng)目根目錄的絕對路徑,通過pwd查看directory=/home/ifeng/Desktop/luffycity/luffycityapi# 項(xiàng)目虛擬環(huán)境enviroment=PATH="/home/ifeng/anaconda3/envs/luffycity/bin"# 運(yùn)行日志絕對路徑stdout_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.beat.info.log# 錯誤日志絕對路徑stderr_logfile=/home/ifeng/Desktop/luffycity/luffycityapi/logs/celery.beat.error.log# 自動啟動,開機(jī)自啟autostart=true# 重啟autorestart=true# 進(jìn)程啟動后跑了幾秒鐘,才被認(rèn)定為成功啟動,默認(rèn)1startsecs=10# 進(jìn)程結(jié)束后60秒才被認(rèn)定結(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# 項(xiàng)目根目錄的絕對路徑,通過pwd查看directory=/home/ifeng/Desktop/luffycity/luffycityapi# 項(xiàng)目虛擬環(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# 自動啟動,開機(jī)自啟autostart=true# 重啟autorestart=true# 進(jìn)程啟動后跑了幾秒鐘,才被認(rèn)定為成功啟動,默認(rèn)1startsecs=10# 進(jìn)程結(jié)束后60秒才被認(rèn)定結(jié)束stopwatisecs=60# 優(yōu)先級priority=999
啟動supervisor
,確保此時你在項(xiàng)目路徑下
cd ~/Desktop/luffycity/luffycityapisupervisord -c scripts/supervisord.conf
通過瀏覽器訪問http://127.0.0.1:9001
常用操作
命令 | 描述 |
---|---|
supervisorctl stop program | 停止某一個進(jìn)程,program 就是進(jìn)程名稱,例如在ini文件首行定義的[program:進(jìn)程名稱] |
supervisorctl stop all | 停止全部進(jìn)程 |
supervisorctl start program | 啟動某個進(jìn)程,program同上,也支持啟動所有的進(jìn)程 |
supervisorctl restart program | 重啟某個進(jìn)程,program同上,也支持重啟所有的進(jìn)程 |
supervisorctl reload | 載入最新的配置文件,停止原有進(jìn)程并按新的配置啟動、管理所有進(jìn)程注意:start、restart、stop 等都不會載入最新的配置文件 |
supervisorctl update | 根據(jù)最新的配置文件,啟動新配置或有改動的進(jìn)程,配置沒有改動的進(jìn)程不會受影響而重啟 |
ps aux | grep supervisord | 查看supervisor是否啟動 |
把supervisor注冊到ubuntu系統(tǒng)服務(wù)中并設(shè)置開機(jī)自啟
cd /home/ifeng/Desktop/luffycity/luffycityapi/scriptstouch supervisor.service
supervisor.service
,配置內(nèi)容,并保存。需要通過conda env list 查看當(dāng)前的虛擬環(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
設(shè)置開機(jī)自啟
# 創(chuàng)建日志文件sudo chmod 766 /tmp/supervisord.logcd /home/ifeng/Desktop/luffycity/luffycityapi/scripts# 賦予權(quán)限chmod 766 supervisor.service# 復(fù)制到系統(tǒng)開啟服務(wù)目錄下sudo cp supervisor.service /lib/systemd/system/# 設(shè)置允許開機(jī)自啟systemctl enable supervisor.service# 判斷是否已經(jīng)設(shè)置為開機(jī)自啟了systemctl is-enabled supervisor.service# 通過systemctl查看supervisor運(yùn)行狀態(tài)systemctl status supervisor.service# 如果查看服務(wù)狀態(tài)時無法啟動,則可以通過重啟linux系統(tǒng)來測試是否因?yàn)榍懊娴慕K端已經(jīng)運(yùn)行了supervisor導(dǎo)致的。當(dāng)然,也可以手動關(guān)閉supervisor以及相關(guān)的服務(wù)。# supervisorctl stop all# ps aux | grep supervisord# kill -9 51564 # 注意: 9068是舉例的,具體看上一行的查詢結(jié)果
效果圖:
關(guān)鍵詞:
農(nóng)行平羅縣支行發(fā)放首筆種業(yè)客戶“抵押e貸” 快播
在寧夏平羅縣興隆種業(yè)公司培育基地的大棚里,剛剛冒出地頭的幼苗水嫩新
富國藍(lán)籌精選007455(富國吉林高速601518滬港深基金怎么樣)
花唄借唄白條金條不想還了,怎么辦請求第二張不同銀行信用卡需求什么手
蘋果xr手機(jī)屏幕錄制在哪(蘋果xr屏幕錄制在哪里)
蘋果xr屏幕錄制的方法蘋果xr錄制屏幕首先要打開手機(jī)界面最找到設(shè)置程序
滬寧沿江高鐵啟動聯(lián)調(diào)聯(lián)試|環(huán)球新資訊
以“江尾海頭、巨輪啟航”為設(shè)計理念的滬寧沿江高速鐵路江陰站由中鐵建
美調(diào)查:通貨膨脹壓力下,超9成美國人都在削減開支
美調(diào)查:通貨膨脹壓力下,超9成美國人都在削減開支
關(guān)于我們 加入我們 聯(lián)系我們 商務(wù)合作 粵ICP備2022077823號
創(chuàng)氪網(wǎng) www.m.cn-everich.com 版權(quán)所有 技術(shù)支持:廣州中創(chuàng)互聯(lián)網(wǎng)信息服務(wù)有限公司
投稿投訴聯(lián)系郵箱:317 493 128 @qq.com