gunicorn 19.2 18.0 配置启动失败

JK *_*iho 5 arch-linux gunicorn systemctl

我有一个在 nginx 后面运行 gunicorn/Django 的开发服务器。作为更广泛的服务器环境更新的一部分,我尝试将 gunicorn 从 18.0 升级到 19.2.1,但该服务将不再启动。(服务器正在运行 Arch,因此使用 systemctl。)

gunicorn 配置是由不再受我们支配的人完成的,并且对 gunicorn 不是很了解,我无法修复甚至定位问题,所以我恢复到 18.0 版并且它现在可以工作。但是,我想最终升级它并使配置处于可以工作的状态。我有一种感觉,当前的配置是次优或多余的,但我无法确定:-)。

环境(或运行gunicorn的virtualenv)没有任何变化,只有gunicorn本身升级了。Systemctl 在systemctl start gunicorn以下位置产生此错误:

? gunicorn.service - gunicorn daemon (production)
   Loaded: loaded (/usr/lib/systemd/system/gunicorn.service; enabled)
   Active: failed (Result: resources) since Tue 2015-02-17 20:55:41 UTC; 8s ago
  Process: 2837 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 9608 ExecReload=/bin/kill -s HUP $MAINPID (code=exited, status=0/SUCCESS)
  Process: 5353 ExecStart=/home/django/gunicorn/run.sh (code=exited, status=0/SUCCESS)
 Main PID: 24876 (code=exited, status=0/SUCCESS)

Feb 17 20:55:41 ashima systemd[1]: PID file /home/django/gunicorn/gunicorn.pid not readable (yet?) after start.
Feb 17 20:55:41 ashima systemd[1]: gunicorn.service never wrote its PID file. Failing.
Feb 17 20:55:41 ashima systemd[1]: Failed to start gunicorn daemon (production).
Feb 17 20:55:41 ashima systemd[1]: Unit gunicorn.service entered failed state.
Run Code Online (Sandbox Code Playgroud)

尝试run.sh从 shell 手动运行(粘贴在下面)中包含的 gunicorn 命令,它只是立即退出而没有产生任何错误,退出代码为 0。没有记录任何内容。事实上,看起来我的前任在日志文件增长到惊人的大小后不久禁用了 gunicorn 日志记录,但这是另一天的问题。

以下是相关文件的内容:

/usr/lib/systemd/system/gunicorn.service:

[Unit]
Description=gunicorn daemon

[Service]
Type=forking
PIDFile=/home/django/gunicorn/gunicorn.pid
User=django
WorkingDirectory=/home/django/[name_withheld]/project
ExecStart=/home/django/gunicorn/run.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=false

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

/home/django/gunicorn/run.sh:

#!/bin/bash

set -e

cd /home/django/[name_withheld]/project
source /home/django/.venv/bin/activate
exec gunicorn -p /home/django/gunicorn/gunicorn.pid -c /home/django/gunicorn/config.py -e HTTPS=on [name_withheld]_site.wsgi:application
Run Code Online (Sandbox Code Playgroud)

/home/django/gunicorn/config.py:

bind = 'unix:/tmp/gunicorn.sock'
backlog = 2048
workers = 16
worker_class = 'egg:gunicorn#sync'
worker_connections = 1000
timeout = 30
keepalive = 2
debug = False
spew = False
daemon = True
pidfile = None
umask = 0755
user = None
group = None
tmp_upload_dir = None
raw_env = 'HTTPS=on'
errorlog = '-'
loglevel = 'info'
accesslog = None
proc_name = None

def post_fork(server, worker):
    server.log.info("Worker spawned (pid: %s)", worker.pid)

def pre_fork(server, worker):
    pass

def pre_exec(server):
    server.log.info("Forked child, re-executing.")

def when_ready(server):
    server.log.info("Server is ready. Spawning workers")
Run Code Online (Sandbox Code Playgroud)

JK *_*iho 1

(在针对该问题发布的评论中,我必须特别指出 skarap 的评论因为它帮助我通过使 Gunicorn 正确输出错误来找到自己的解决方案。我希望我可以为此授予部分赏金;转换将该评论转化为答案还不是完整的答案,但它确实有很大帮助。)

事实证明这是配置文件中有问题的行:

worker_class = 'egg:gunicorn#sync'

它导致了这个错误:

Error: class uri 'egg:gunicorn#sync' invalid or not found: 

[Traceback (most recent call last):
  File "/home/django/.venv/lib/python2.7/site-packages/gunicorn/util.py", line 113, in load_class
    return pkg_resources.load_entry_point(dist, section, name)
  File "/home/django/.venv/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 318, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/home/django/.venv/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg/pkg_resources.py", line 2220, in load_entry_point
    raise ImportError("Entry point %r not found" % ((group,name),))
ImportError: Entry point ('gunicorn.workers', 'sync') not found
]
Run Code Online (Sandbox Code Playgroud)

将其替换为worker_class = 'sync'修复了 ImportError 并因此解决了问题。18.0 -> 19.2.1 升级中无需进行其他配置更改。

Gunicorn 的文档似乎存在问题,我打算报告该问题,因为在撰写本文时,v19.2.1 的文档仍然声明egg:gunicorn#[worker]语法有效。(那里的示例使用了 gevent,但看起来它应该适用于其他类型)。谁知道,它在某些情况下可能是有效的,但在我的(virtualenv 中的 Gunicorn,用 pip 安装)中,它不是。