Sinatra + Thin 的 Systemd 服务不断重新启动

Sinatra + Thin 的 Systemd 服务不断重新启动

我为 Sinatra 应用程序提供了 systemd 服务,该应用程序在 Nginx 反向代理后面使用瘦服务器运行。它工作正常,但因为它收到大量流量,我看到很多关于无法连接到上游的 nginx 错误。在检查该服务后,我注意到它永远不会运行很长时间,最多只有几分钟,这可以解释为什么 Nginx 无法多次连接(当服务重新启动时)。

查看该服务的输出,journalctl我看到很多这样的内容:

Dec 20 22:09:48 cs2092 systemd[1]: Started My app web site.
Dec 20 22:10:59 cs2092 bundle[11576]: pure virtual method called
Dec 20 22:10:59 cs2092 bundle[11576]: terminate called without an active exception
Dec 20 22:10:59 cs2092 systemd[1]: my-service.service: Main process exited, code=killed, status=6/ABRT
Dec 20 22:10:59 cs2092 systemd[1]: my-service.service: Failed with result 'signal'.
Dec 20 22:10:59 cs2092 systemd[1]: my-service.service: Service hold-off time over, scheduling restart.
Dec 20 22:10:59 cs2092 systemd[1]: my-service.service: Scheduled restart job, restart counter is at 7.
Dec 20 22:10:59 cs2092 systemd[1]: Stopped My app web site.
Dec 20 22:10:59 cs2092 systemd[1]: Started My app web site.
Dec 20 22:11:19 cs2092 bundle[11828]: pure virtual method called
Dec 20 22:11:19 cs2092 bundle[11828]: terminate called without an active exception
Dec 20 22:11:19 cs2092 systemd[1]: my-service.service: Main process exited, code=killed, status=6/ABRT
Dec 20 22:11:19 cs2092 systemd[1]: my-service.service: Failed with result 'signal'.
Dec 20 22:11:19 cs2092 systemd[1]: my-service.service: Service hold-off time over, scheduling restart.
Dec 20 22:11:19 cs2092 systemd[1]: my-service.service: Scheduled restart job, restart counter is at 8.
Dec 20 22:11:19 cs2092 systemd[1]: Stopped My app web site.
Dec 20 22:11:19 cs2092 systemd[1]: Started My app web site.
Dec 20 22:14:28 cs2092 bundle[11968]: pure virtual method called
Dec 20 22:14:28 cs2092 bundle[11968]: terminate called without an active exception
Dec 20 22:14:28 cs2092 systemd[1]: my-service.service: Main process exited, code=killed, status=6/ABRT
Dec 20 22:14:28 cs2092 systemd[1]: my-service.service: Failed with result 'signal'.
Dec 20 22:14:28 cs2092 systemd[1]: my-service.service: Service hold-off time over, scheduling restart.
Dec 20 22:14:28 cs2092 systemd[1]: my-service.service: Scheduled restart job, restart counter is at 9.
Dec 20 22:14:28 cs2092 systemd[1]: Stopped My app web site.
Dec 20 22:14:28 cs2092 systemd[1]: Started My app web site.

看起来该应用程序经常被杀死?为什么会发生这种情况?

这是服务:

[Unit]
Description=My app web site
Documentation=https://myapp.com
After=network.target

[Service]
Type=simple
WorkingDirectory=/var/www/my-app
Environment="RACK_ENV=production"
ExecStart=/usr/local/bin/bundle exec /usr/local/bin/thin -R /var/www/my-app/config.ru -p 6903 --max-conns 15360 --max-persistent-conns 2048 --threaded --debug start
ExecStop=/usr/local/bin/bundle exec /usr/local/bin/thin -R /var/www/my-app/config.ru -p 6903 stop
ExecReload=/usr/local/bin/bundle exec /usr/local/bin/thin -R /var/www/my-app/config.ru -p 6903 --max-conns 15360 --max-persistent-conns 2048 --threaded --debug restart
Restart=on-failure
User=julien

[Install]
WantedBy=multi-user.target

我不明白的另一件事是,正如您从我启动 Sinatra 的服务中看到的那样--max-conns 15360,但在journalctl输出中我可以看到最大连接数设置为 1024:

Dec 21 10:24:24 cs2092 bundle[21058]: Starting my-app in production...
Dec 21 10:24:24 cs2092 bundle[21058]: 2021-12-21 10:22:30 +0000 Thin web server (v1.8.1 codename Infinite Smoothie)
Dec 21 10:24:24 cs2092 bundle[21058]: 2021-12-21 10:22:30 +0000 Debugging ON
Dec 21 10:24:24 cs2092 bundle[21058]: 2021-12-21 10:22:30 +0000 Maximum connections set to 1024
Dec 21 10:24:24 cs2092 bundle[21058]: 2021-12-21 10:22:30 +0000 Listening on 0.0.0.0:6903, CTRL+C to stop

知道发生了什么事吗?

注:Ubuntu 18.04.4

答案1

您的应用程序有一个错误并终止并打印以下 ABRT/6:

pure virtual method called
terminate called without an active exception

该错误很可能与某些外部 C++ 库有关。

要增加连接限制,请尝试添加LimitNOFILE=15360到你的单位文件。

答案2

所以看来问题出在 Thin 服务器内部,一旦我用 Puma 替换它,所有问题都消失了。

相关内容