前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python测试开发django-193.使用supervisord 后台启动celery 服务(worker/beat)

python测试开发django-193.使用supervisord 后台启动celery 服务(worker/beat)

作者头像
上海-悠悠
发布2022-07-12 15:19:00
3050
发布2022-07-12 15:19:00
举报

前言

Supervisor(’http://supervisord.org/')是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。 它可以很方便监听、启动、停止、重启一个或多个进程。 用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

环境准备

centos 安装 supervisord

代码语言:javascript
复制
yum install -y supervisord

debian 安装  supervisord

代码语言:javascript
复制
apt-get install -y supervisor

supervisord.conf

安装完成后在/etc/supervisor 目录下会有个配置文件 supervisord.conf

代码语言:javascript
复制
# cd /etc/supervisor
/etc/supervisor# ls
conf.d    supervisord.conf

cat 查看内容

代码语言:javascript
复制
# cat supervisord.conf 
; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below 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: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; 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 = /etc/supervisor/conf.d/*.conf

在项目中我们需要用到此配置,可以在项目根目录导出一份

代码语言:javascript
复制
echo_supervisord_conf > ./supervisord.conf

文件内容编写

supervisord.conf文件内容编写, 前面内容不用改,直接接着在后面写 比如我需要后台启动celery的worker和beat服务

代码语言:javascript
复制
; Sample supervisor config file.
;
; For more information on the config file, please see:
; http://supervisord.org/configuration.html

; 前面文档省略,不用删,接着后面写

[program:celery-worker]
command=celery -A hrun2_web worker -l info ; 管理命令,supervisor不支持后台进程
process_name=%(program_name)s
#user=root                                 ;进程启动用户
autostart=true                           ;是否随supervisor启动
autorestart=true                         ;是否在挂了之后重启,意外关闭后会重启,比如kill掉!
startretries=3                           ;启动尝试次数
stderr_logfile=./celery_worker.log           ;标准输出文件
stdout_logfile=./celery_worker.log        ;标准输出文件
loglevel=info                            ;日志的级别

[program:celery-beat]
command=celery -A hrun2_web beat ; 管理命令,supervisor不支持后台进程
process_name=%(program_name)s
#user=root                                 ;进程启动用户
autostart=true                           ;是否随supervisor启动
autorestart=true                         ;是否在挂了之后重启,意外关闭后会重启,比如kill掉!
startretries=3                           ;启动尝试次数
stderr_logfile=./celery_beat.log          ;标准输出文件
stdout_logfile=./celery_beat.log        ;标准输出文件
loglevel=info                            ;日志的级别

启动服务

启动服务

代码语言:javascript
复制
supervisord -c /path/supervisord.conf

关闭服务

代码语言:javascript
复制
supervisorctl shutdown

运行过程,启动没问题,不显示任何内容,如果需要关闭用shutdown

代码语言:javascript
复制
root@13107c465557:/code# supervisord -c ./supervisord.conf
root@13107c465557:/code# supervisorctl shutdown
Shut down

查看启动的服务

代码语言:javascript
复制
supervisorctl status

执行结果如下

代码语言:javascript
复制
root@13107c465557:/code# supervisorctl status
celery-worker                    RUNNING   pid 234, uptime 0:00:14
celery-beat                      RUNNING   pid 235, uptime 0:00:14

说明启动了celery和celery-beat两个服务

查看日志

我们在配置里面指定了./celery_worker.log文件保存运行日志,所以可以直接查看这个文件

代码语言:javascript
复制
tail -f celery_worker.log -n 30

运行就可以看到worker运行的日志了

参考教程https://blog.csdn.net/qq_43084874/article/details/124932475

2022年第 11 期《python接口web自动化+测试开发》课程,6月5号开学

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-07-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 环境准备
  • supervisord.conf
  • 文件内容编写
  • 启动服务
  • 查看日志
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档