重新加载与重新启动 uWSGI 以激活代码更改

Reloading vs. restarting uWSGI to activate code changes

在阅读了 uWSGI 的 documentation on reloading 之后,我的理解是,对于使用 lazy-apps 的应用程序,将 w 写入 uWSGI 的主 FIFO 应该会触发所有 worker 的重启(并且因此激活 Python 代码中的更改。

但是,这似乎对我不起作用。我需要重新启动 systemd 服务 (systemctl restart myservice) 以使代码更改生效。是我误解了文档,还是我的设置有问题?

我的 myservice.service 文件如下所示:

...
ExecStart=/usr/lib/myservice/virtualenv/bin/uwsgi --ini /etc/myservice/uwsgi.ini
ExecReload=/bin/echo 'w' > /run/myservice/masterfifo
ExecStop=/bin/kill -INT $MAINPID
...

特别是,systemctl reload myservice 应该将 w 写入主 FIFO。我可以从 systemctl status myservice 中的日志中看到执行了重新加载,但是对 HTTP 请求的响应告诉我旧代码仍然有效。

我的/etc/myservice/uwsgi.ini是这样的:

[uwsgi]
processes = 16
procname-master = myservice
master-fifo = /run/myservice/masterfifo
touch-chain-reload
listen = 128

thunder-lock
reload-on-as = 4096
limit-as = 8192
max-requests = 2000

; termination options
vacuum
die-on-term

; application
chdir = /usr/lib/myservice
virtualenv = /usr/lib/myservice/virtualenv
module = myservice.uwsgi
callable = app  
master
need-app
enable-threads
lazy = True
lazy-apps = True

; logging
logto = /var/log/myservice/uwsgi.log
log-maxsize = 5242880
logdate = [%%Y/%%m/%%d %%H:%%M:%%S]
disable-logging

; stats server
stats-server = :8201
memory-report

; unix socket config (nginx->uwsgi)
socket = /run/myservice/myservice.sock
chown-socket = api
chmod-socket = 660

我是 uWSGI 的 运行 版本 2.0.19.1

我对 uWSGI 的了解就是它的存在,但我注意到这里有一个错误:

ExecReload=/bin/echo 'w' > /run/myservice/masterfifo

手册页 explains the problem:

This syntax is inspired by shell syntax, but only the meta-characters and expansions described in the following paragraphs are understood, and the expansion of variables is different. Specifically, redirection using "<", "<<", ">", and ">>", pipes using "|", running programs in the background using "&", and other elements of shell syntax are not supported.

换句话说,没有发生重定向,echo 将只接收 3 个要打印的参数:char w、char > 和字符串 /run/myservice/masterfifo

试试这个:

ExecReload=/bin/sh -c '/bin/echo w > /run/myservice/masterfifo'