主管gunicorn进程未在实例重启时启动

时间:2016-01-31 12:41:58

标签: django gunicorn supervisor

我正在尝试为Django配置gunicorn和supervisor。我已配置gunicorn,我可以手动使用gunicorn运行django应用程序。现在我已经尝试配置主管,问题是没有在实例重启时启动gunicorn进程。如果我从supervisorctl手动启动应用程序,那么应用程序将开始运行。 当我在supervisorctl中看到状态时,它是致命的并且stderr说

Traceback (most recent call last):
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/bin/gunicorn", line 11, in <module>
    sys.exit(run())
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 75, in run
    WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]").run()
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/app/base.py", line 189, in run
    super(Application, self).run()
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/app/base.py", line 72, in run
    Arbiter(self).run()
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/arbiter.py", line 58, in __init__
    self.setup(app)
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/arbiter.py", line 114, in setup
    self.app.wsgi()
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 66, in load
    return self.load_wsgiapp()
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/gunicorn/util.py", line 355, in import_app
    __import__(module)
ImportError: No module named 'config'

django app结构

subscription-project \
  .codeintel
  .ebextensions
  .git
  .gitignore
  Makefile
  Procfile
  README.rst
  common
  config \
    __init__.py
    settings
    urls.py
    wsgi.py
  djangoapps
  locale
  manage.py
  requirements
  runtime.txt

subscriptionapp_gunicorn.py

import multiprocessing

preload_app = True
timeout = 300
bind = "127.0.0.1:8000"
pythonpath = "/subscription/app/subscriptionapp/subscription-project"

workers = (multiprocessing.cpu_count()-1)

upstart supervisor config (/etc/init/supervisor.conf)

description     "supervisord"

start on runlevel [2345]
stop on runlevel [!2345]

kill timeout 432000

setuid www-data
exec /subscription/app/supervisor/venvs/supervisor/bin/supervisord -n --configuration /subscription/app/supervisor/supervisord.conf

/subscription/app/supervisor/conf.d/subscriptionapp.conf

[program:subscriptionapp]

command=/subscription/app/subscriptionapp/venvs/subscriptionapp/bin/gunicorn -c /subscription/app/subscriptionapp/subscriptionapp_gunicorn.py config.wsgi

user=www-data
directory=/subscription/app/subscriptionapp/subscription-project

environment=PORT=8000,ADDRESS=127.0.0.1,LANG=en_US.UTF-8,DJANGO_SETTINGS_MODULE=config.settings.base,PATH="/subscription/app/subscriptionapp/venvs/subscriptionapp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

stdout_logfile=/subscription/var/log/supervisor/subscriptionapp-stdout.log
stderr_logfile=/subscription/var/log/supervisor/subscriptionapp-stderr.log
killasgroup=true
stopasgroup=true

supervisord.conf

; supervisor config file

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

[supervisord]
logfile=/subscription/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/subscription/var/supervisor/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/subscription/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:///subscription/var/supervisor/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.

[inet_http_server]
port = 127.0.0.1:9001

[include]
files = /subscription/app/supervisor/conf.d/*.conf

我们非常感谢您解决此问题的任何帮助。

1 个答案:

答案 0 :(得分:0)

似乎config包不适用于gunicorn。尝试将项目目录显式添加到pythonpath,如下所示:

/subscription/app/supervisor/conf.d/subscriptionapp.conf

[program:subscriptionapp]

command=/subscription/app/subscriptionapp/venvs/subscriptionapp/bin/gunicorn -c /subscription/app/subscriptionapp/subscriptionapp_gunicorn.py --pythonpath '/subscription/app/subscriptionapp/subscription-project,/subscription/app/subscriptionapp/venvs/subscriptionapp/lib/python3.4/site-packages/' config.wsgi

user=www-data
directory=/subscription/app/subscriptionapp/subscription-project

environment=PORT=8000,ADDRESS=127.0.0.1,LANG=en_US.UTF-8,DJANGO_SETTINGS_MODULE=config.settings.base,PATH="/subscription/app/subscriptionapp/venvs/subscriptionapp/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

stdout_logfile=/subscription/var/log/supervisor/subscriptionapp-stdout.log
stderr_logfile=/subscription/var/log/supervisor/subscriptionapp-stderr.log
killasgroup=true
stopasgroup=true

如果这有助于gunicorn“找到”config,则可能是您的gunicorn configuration file未正确加载。根据你的gunicorn版本,从python模块加载设置可能需要一个特殊的表单,如文档中所示:

  

在版本19.4中更改:从Python模块加载配置需要python:前缀。