supervisor在python3中的使用

本篇文章请结合以下项目学习:

点击查看

项目目录树:

django_celery
├── conf  # 存放supervisor相关配置文件
│   ├── supervisord.conf
│   ├── supervisor_celery_worker.ini  # python3 manage.py celery worker -l info
│   ├── supervisor_celery_beat.ini  # python3 manage.py celery beat -l info
│   ├── supervisor_celery_flower.ini  # python3 manage.py celery flower
│   ├── supervisor_manage.ini  # python3 manage.py runserver
├── course  # 新建应用
│   ├── admin.py
│   ├── __init__.py
│   ├── migrations
│   ├── models.py
│   ├── tasks.py  # 任务模块
│   ├── urls.py  # 路由文件
│   └── views.py
├── db.sqlite3
├── django_celery
│   ├── celeryconfig.py  # celery配置文件
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── manage.py

第一步:安装支持python3的supervisor进程管理工具

pip3 install git+https://github.com/Supervisor/supervisor

第二步:在项目根目录下创建conf文件夹用来保存supervisor相关配置信息

mkdir conf

第三步:将supervisor配置文件重定向到我们创建的文件夹下,方便管理

echo_supervisord_conf > /home/fatpuffer/myproject/django_celery/conf/supervisord.conf

第四步:修改supervisord.conf配置文件信息

[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)


[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



[include]
files = *.ini  # 包含当前目录(conf)下的所有以.ini结尾的文件作为supervisor配置信息

第五步:创建supervisor_celery_worker.ini文件,启动我们项目worker任务队列

[program:test-celery-worker]
# 启动命令
command=python3 manage.py celery worker -l info
# 项目绝对路径
directory=/home/fatpuffer/myproject/django_celery
# 项目虚拟环境
enviroment=PATH="/home/fatpuffer/.virtualenvs/dj_celery/bin"
# 输出日志
stdout_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.worker.log
# 错误日志
stderr_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.worker.log
# 自动启动
autostart=true
# 重启
autorestart=true
# 进程启动后跑了几秒钟,才被认定为成功启动,默认1
startsecs=10
# 进程结束后60秒才被认定结束
stopwatisecs=60
# 优先级
priority=997

第六步:创建supervisor_celery_beat.ini文件,来触发我们的beat定时任务

;[program:test-celery-beat]
;command=python3 manage.py celery_beat -l info
;directory=/home/fatpuffer/myproject/django_celery
;enviroment=PATH="/home/fatpuffer/.virtualenvs/dj_celery/bin"
;stdout_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.beat.log
;stderr_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.beat.log
;autostart=true
;autorestart=true
;startsecs=10
;stopwaitsecs=60
;priority=998

第七步:创建supervisor_celery_flower.ini文件,来启动我们的celery监控管理工具

[program:test-celery-flowering]
command=python3 manage.py celery flower
directory=/home/fatpuffer/myproject/django_celery
enviroment=PATH="/home/fatpuffer/.virtualenvs/dj_celery/bin"
stdout_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.flower.log
stderr_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.flower.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=60
priority=990  # 在启动celery监控工具flower前必须先启动worker队列,因此优先级必须低于worker启动配置文件

第八步:创建supervisor_manage.ini文件,启动我们django项目

[program:test-manage]
command=python3 manage.py runserver
directory=/home/fatpuffer/myproject/django_celery
enviroment=PATH="/home/fatpuffer/.virtualenvs/dj_celery/bin"
stdout_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.flower.log
stderr_logfile=/home/fatpuffer/myproject/django_celery/logs/celery.flower.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs=60
priority=999

第九步:启动supervisor,确保此时你在项目路径下

supervisord -c conf/supervisord.conf

第十步:启动supervisorctl命令行,进行管理

supervisorctl

supervisor> help

default commands (type help <topic>):
=====================================
add    exit      open  reload  restart   start   tail   
avail  fg        pid   remove  shutdown  status  update 
clear  maintail  quit  reread  signal    stop    version

我们可以通过以上命令来进行管理,具体命令含义自行百度

你可能感兴趣的:(Django)