uWSGI 闲置后不重启

uWSGI not restarting after being idle

我在 ubuntu 16.

上使用 uwsgi 和 systemd

我从这里按照每个应用一项服务下的说明进行操作:http://uwsgi-docs.readthedocs.io/en/latest/Systemd.html

如果我启动 systemd 套接字并访问该站点,一切正常。 但是在空闲时间(在 ini 文件中指定)之后,uwsgi 进程被终止并且进一步的请求似乎不会重新启动/重新激活进程。

workers have been inactive for more than 30 seconds (1501589319-1501589288)
SIGINT/SIGQUIT received...killing workers...
worker 1 buried after 1 seconds
worker 2 buried after 1 seconds
goodbye to uWSGI.

这是我的 ini 文件:

[uwsgi]
module = wsgi

logto = /tmp/uwsgi.log

master = true
processes = 2
cheap = true
idle = 30
die-on-idle = true

manage-script-name = true

这是我的 systemd 套接字配置:

[Unit]
Description=Socket for uWSGI app

[Socket]
ListenStream=/var/run/uwsgi/myapp.socket
SocketUser=www-data
SocketGroup=www-data
SocketMode=0666

[Install]
WantedBy=sockets.target

这是我的系统服务配置:

[Unit]
Description=uWSGI app
After=syslog.target

[Service]
ExecStart=/home/myapp/venv/bin/uwsgi \
        --ini /home/myapp/uwsgi/uwsgi.ini \
        --socket /var/run/uwsgi/myapp.socket
User=www-data
Group=www-data
Restart=on-failure
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all
RuntimeDirectory=uwsgi
WorkingDirectory=/home/myapp

在摆弄了将近一天之后,我在相关设置的文档中找到了答案。虽然因为我不使用 emperor 而不是同一种设置,但它确实给了我一个线索。

套接字激活仅在第一次有效的原因是因为 uwsgi 覆盖了由 systemd 套接字创建的套接字文件。

看到绿色的“!”此页面上的重要框: http://uwsgi-docs.readthedocs.io/en/latest/OnDemandVassals.html

它说:

If you will define in your vassal config same socket as used by emperor for on demand action, vassal will override that socket file. That could lead to unexpected behaviour, for example on demand activation of that vassal will work only once.

所以在我的例子中我没有皇帝,但 systemd 就像皇帝一样。

那么解决方法是什么?

从 systemd 服务文件中的 ExecStart 中删除 --socket 参数。

而不是

ExecStart=/home/myapp/venv/bin/uwsgi \
        --ini /home/myapp/uwsgi/uwsgi.ini \
        --socket /var/run/uwsgi/myapp.socket

我改成了

ExecStart=/home/myapp/venv/bin/uwsgi \
        --ini /home/myapp/uwsgi/uwsgi.ini

之后我停止了 systemd 服务和套接字,执行了 systemctl daemon-reload 并再次启动了 systemd 套接字。 Evrything 开始按预期工作 - systemd 套接字激活与 uwsgi 一起工作! :)