devops [已解决] supervisor 部署出错

不二家的小球迷 · December 29, 2016 · Last by 不二家的小球迷 replied at December 30, 2016 · 3196 hits

原因

我在V2EX发了求助帖,好像没有人回复,我在testerhome碰碰运气吧,简述一下问题。
我需要部署flask写的小应用,之前一直用gunicorn部署的,但是每次需要重启感觉麻烦,然后知道supervisor可以守护进程加上gunicorn执行,但是我遇到了问题。

我的 supervisor.conf 如下:

; 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".
; - Comments must have a leading space: "a=b ;comment" not "a=b;comment".

[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)

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

[supervisord]
logfile=/tmp/supervisord.log ; (main log file;default $CWD/supervisord.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[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=user ; should be same as http_username if set
password=123 ; should be same as http_password if set


[include]
files =ci_ios/webhook/wsgi.py
[program:wsgi]
command=~/Documents/supervisor/env/bin/gunicorn --chdir ~/Documents/supervisor
/ci_ios/webhook -b 0.0.0.0:8000 wsgi:application
startsecs=0
stopwaitsecs=0
autostart=false
autorestart=true
stdout_logfile=/tmp/gunicorn.log
stderr_logfile=/tmp/gunicorn.err

然后我直接启动命令,
~/Documents/supervisor/env/bin/gunicorn --chdir ~/Documents/supervisor/ci_ios/webhook -b 0.0.0.0:8000 wsgi:application, 没有报错,可以正常启动:

 Starting gunicorn 19.6.0
[2016-12-29 15:23:45 +0800] [35578] [INFO] Listening at: http://0.0.0.0:8000 (35578)
[2016-12-29 15:23:45 +0800] [35578] [INFO] Using worker: sync
[2016-12-29 15:23:45 +0800] [35581] [INFO] Booting worker with pid: 35581
^C[2016-12-29 15:23:54 +0800] [35578] [INFO] Handling signal: int
[2016-12-29 15:23:54 +0800] [35581] [INFO] Worker exiting (pid: 35581)

但是我通过supervisord -c supervisor.conf就会报错:

Error: File contains no section headers.
file: /Users/djx/Documents/supervisor/ci_ios/webhook/wsgi.py, line: 2
'from __future__ import with_statement\n'
For help, use /Users/djx/Documents/supervisor/env/bin/supervisord -h

可是谷歌了半天也不知到底该怎么改,我保存的 py 格式是 UTF-8 无 BOM 。

还有我的项目目录的结构是

~/Documents/supervisor/

ci_ios/webhook/wsgi.py

supervisor.conf

希望大神赶快出现,我实在无力解决。

我对这方面了解可能不足,或许我的supervisor.conf 写的有问题,希望可以告诉我指导一下。

谢谢。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞

[include]
files =ci_ios/webhook/wsgi.py

ci_ios/webhook/wsgi.py 这个贴出来看看?

第一种

新建 python2.7/site-packages/sitecustomize.py ,输入:

import sys
sys.setdefaultencoding('utf-8)

第二种

cat -e file可以看到换行符,CRLF 显示为^M$,LF 显示为$ ,Linux 和 Mac 上都要使用 LF 换行

或者 Pycharm 打开文件,右下角也有显示,Mac 上选中根目录,在 File-Line-Separators 菜单里可以修改项目里的所有文件

讨论

并不会 Python ,公司有个产品设置这两个后就没碰到过no section headers UnicodeDecodeError: 'ascii' codec 之类的错误了

不过也看到了why-should-we-not-use-sys-setdefaultencodingutf-8-in-a-py-script这个问题,如果按第一种方法设置之后不报错,可能还是某个文件(不一定是.py文件)编码不对

#1楼 @Lihuazhang

#coding=utf-8
from __future__ import with_statement
from app import create_app


application = create_app()
if __name__ == "__main__":
application.run()

我自己搞定,我判断原因是directory没有写绝对路径。贴出我当前的supersior.conf



[unix_http_server]
file=/tmp/supervisor.sock ; (the path to the socket file)

[inet_http_server] ; inet (TCP) server disabled by default
port=192.168.1.106: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.log)
logfile_maxbytes=50MB ; (max main logfile bytes b4 rotation;default 50MB)
logfile_backups=10 ; (num of main logfile rotation backups;default 10)
loglevel=info ; (log level;default info; others: debug,warn,trace)
pidfile=/tmp/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
nodaemon=false ; (start in foreground if true;default false)
minfds=1024 ; (min. avail startup file descriptors;default 1024)
minprocs=200 ; (min. avail process descriptors;default 200)

[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket
serverurl=http://192.168.1.106:9001 ; use an http:// url to specify an inet socket
username=chris ; should be same as http_username if set
password=123 ; should be same as http_password if set
;prompt=mysupervisor ; cmd line prompt (default "supervisor")
;history_file=~/.sc_history ; use readline history if available

[program:webhook]
directory = /Users/ddd/Documents/web_project/ci_ios/webhook ; 程序的启动目录
command = /Users/ddd/Documents/web_project/env/bin/gunicorn -b 0.0.0.0:8000 wsgi:application ; 启动命令,可以看出与手动在命令行启动的命令是一样的
autostart = true ; supervisord 启动的时候也自动启动
startsecs = 5 ; 启动 5 秒后没有异常退出,就当作已经正常启动了
autorestart = true ; 程序异常退出后自动重启
startretries = 3 ; 启动失败自动重试次数,默认是 3
redirect_stderr = true ; stderr 重定向到 stdout,默认 false
stdout_logfile_maxbytes = 20MB ; stdout 日志文件大小,默认 50MB
stdout_logfile_backups = 20 ; stdout 日志文件备份数
stdout_logfile=/tmp/gunicorn.log
stderr_logfile=/tmp/gunicorn.err
不二家的小球迷 关闭了讨论 30 Dec 21:10
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up