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

      Supervisor啟動并管理Celery相關進程

      來源:博客園  

      Supervisor啟動并管理Celery相關進程

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

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

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


      (資料圖片)

      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開發的一套通用的進程管理程序,能將一個普通的命令行進程變為系統守護進程daemon,并監控進程狀態,異常退出時能自動重啟。

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

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

      對Supervisor初始化配置

      # 在項目根目錄下創建存儲supervisor配置目錄,在luffycityapi創建scripts目錄,已經創建則忽略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

      創建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# 項目虛擬環境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# 進程結束后60秒才被認定結束stopwatisecs=60# 優先級,值小的優先啟動priority=990

      創建luffycity_celery_beat.ini文件,來觸發我們的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# 項目虛擬環境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# 進程結束后60秒才被認定結束stopwatisecs=60# 優先級,值小的優先啟動priority=998

      創建luffycity_celery_flower.ini文件,來啟動我們的celery監控管理工具

      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# 項目虛擬環境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# 進程結束后60秒才被認定結束stopwatisecs=60# 優先級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根據最新的配置文件,啟動新配置或有改動的進程,配置沒有改動的進程不會受影響而重啟
      ps aux | grep supervisord查看supervisor是否啟動

      把supervisor注冊到ubuntu系統服務中并設置開機自啟

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

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

      [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

      設置開機自啟

      # 創建日志文件sudo chmod 766 /tmp/supervisord.logcd /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是舉例的,具體看上一行的查詢結果

      效果圖:

      關鍵詞:

      最新文章
      亚洲人成色4444在线观看| 亚洲av丰满熟妇在线播放| 亚洲色图综合网站| 亚洲性天天干天天摸| 亚洲情a成黄在线观看动漫尤物| 中文字幕一精品亚洲无线一区| 超清首页国产亚洲丝袜| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲国产精品一区二区三区在线观看 | 午夜亚洲乱码伦小说区69堂| 亚洲第一综合天堂另类专| 亚洲熟妇AV一区二区三区浪潮 | 亚洲中文字幕久久精品蜜桃| 中文字幕无码亚洲欧洲日韩| 国产亚洲精品VA片在线播放| 久久久久久亚洲Av无码精品专口| 久久精品亚洲中文字幕无码网站| 久久久久久亚洲Av无码精品专口| 妇女自拍偷自拍亚洲精品| 色欲aⅴ亚洲情无码AV| 在线亚洲v日韩v| 亚洲精品第一国产综合境外资源| 亚洲国产精品一区二区第一页免| 亚洲一级片内射网站在线观看| 久久亚洲精品无码观看不卡| 国产亚洲精品自在线观看| 精品国产香蕉伊思人在线在线亚洲一区二区| 亚洲伊人久久成综合人影院| 亚洲日本va中文字幕久久| 久久青青成人亚洲精品| 337p日本欧洲亚洲大胆精品555588 | 日韩亚洲AV无码一区二区不卡| 亚洲黄色高清视频| 亚洲国产电影在线观看| 亚洲AV男人的天堂在线观看| 国产成人人综合亚洲欧美丁香花| 亚洲精品乱码久久久久久蜜桃| 亚洲人成色77777| 久久精品国产亚洲av水果派| 久久精品国产亚洲AV蜜臀色欲| 亚洲区日韩精品中文字幕|