systemdからuWSGIの起動後にタイムアウトエラーが何度も出てしまったのでメモ

  • 17 Nov 2019

Flaskでアプリ組んだ時にuWSGIの起動後のエラーがあったので対応メモ

環境

  • Ubuntu 18.08
  • Python 3.6.8
  • uWSGI 2.0.18

現象

systemd から uWSGI で Flaskアプリを起動したときに、90秒ごとにアプリの再起動が走っていました。syslog を見ると下記のような感じで何やらタイムアウトが走ってプロセスが kill されては再起動していたようです。

Nov 17 21:21:44 ubuntu-bionic systemd[1]: Started uWSGI instance to serve scopes future analyze api.
Nov 17 21:21:44 ubuntu-bionic uwsgi[2480]: [uWSGI] getting INI configuration from /path/to/your/app/uwsgi.ini
Nov 17 21:23:15 ubuntu-bionic systemd[1]: yourapps.service: State 'stop-sigterm' timed out. Killing.
Nov 17 21:23:15 ubuntu-bionic systemd[1]: yourapps.service: Killing process 2494 (uwsgi) with signal SIGKILL.
Nov 17 21:23:15 ubuntu-bionic systemd[1]: yourapps.service: Killing process 2502 (uwsgi) with signal SIGKILL.
Nov 17 21:23:15 ubuntu-bionic systemd[1]: yourapps.service: Failed with result 'timeout'.
Nov 17 21:23:25 ubuntu-bionic systemd[1]: yourapps.service: Service hold-off time over, scheduling restart.
Nov 17 21:23:25 ubuntu-bionic systemd[1]: yourapps.service: Scheduled restart job, restart counter is at 2.
Nov 17 21:23:25 ubuntu-bionic systemd[1]: Stopped uWSGI instance to serve scopes future analyze api.
Nov 17 21:23:25 ubuntu-bionic systemd[1]: Started uWSGI instance to serve scopes future analyze api.
Nov 17 21:23:25 ubuntu-bionic uwsgi[2528]: [uWSGI] getting INI configuration from /path/to/your/app/uwsgi.ini
Nov 17 21:24:55 ubuntu-bionic systemd[1]: yourapps.service: State 'stop-sigterm' timed out. Killing.
Nov 17 21:24:55 ubuntu-bionic systemd[1]: yourapps.service: Killing process 2542 (uwsgi) with signal SIGKILL.
Nov 17 21:24:55 ubuntu-bionic systemd[1]: yourapps.service: Killing process 2550 (uwsgi) with signal SIGKILL.
Nov 17 21:24:55 ubuntu-bionic systemd[1]: yourapps.service: Failed with result 'timeout'.
Nov 17 21:25:05 ubuntu-bionic systemd[1]: yourapps.service: Service hold-off time over, scheduling restart.
Nov 17 21:25:05 ubuntu-bionic systemd[1]: yourapps.service: Scheduled restart job, restart counter is at 3.
Nov 17 21:25:05 ubuntu-bionic systemd[1]: Stopped uWSGI instance to serve scopes future analyze api.
Nov 17 21:25:05 ubuntu-bionic systemd[1]: Started uWSGI instance to serve scopes future analyze api.
Nov 17 21:25:05 ubuntu-bionic uwsgi[2575]: [uWSGI] getting INI configuration from /path/to/your/app/uwsgi.ini
Nov 17 21:26:36 ubuntu-bionic systemd[1]: yourapps.service: State 'stop-sigterm' timed out. Killing.
Nov 17 21:26:36 ubuntu-bionic systemd[1]: yourapps.service: Killing process 2586 (uwsgi) with signal SIGKILL.
Nov 17 21:26:36 ubuntu-bionic systemd[1]: yourapps.service: Killing process 2595 (uwsgi) with signal SIGKILL.
Nov 17 21:26:36 ubuntu-bionic systemd[1]: yourapps.service: Failed with result 'timeout'.
Nov 17 21:26:46 ubuntu-bionic systemd[1]: yourapps.service: Service hold-off time over, scheduling restart.
Nov 17 21:26:46 ubuntu-bionic systemd[1]: yourapps.service: Scheduled restart job, restart counter is at 4.
Nov 17 21:26:46 ubuntu-bionic systemd[1]: Stopped uWSGI instance to serve scopes future analyze api.
Nov 17 21:26:46 ubuntu-bionic systemd[1]: Started uWSGI instance to serve scopes future analyze api.
Nov 17 21:26:46 ubuntu-bionic uwsgi[2618]: [uWSGI] getting INI configuration from /path/to/your/app/uwsgi.ini

uwsgi.ini の中身

uwsgi.ini の中身はだいたいこんな感じ。

[uwsgi]
socket        = /var/run/uwsgi/yourapps.sock
pidfile       = /var/run/uwsgi/yourapps.pid

enable-threads = true
threads        = 8

wsgi-file     = /path/to/your/apps/index.py
touch-reload  = reload.trigger
master        = true
py-autoreload = 1
die-on-term   = true
callable      = app
chmod-socket  = 666
uid           = www-data
gid           = www-data

daemonize     = /var/log/uwsgi/yourapps.log
req-logger    = file:/var/log/uwsgi/yourapps.request.log
log-reopen    = true
log-maxsize   = 8000000
logfile-chown = on
logfile-chmod = 644

問題点

結露を言うと、daemonize が良くなかったです。なので、

- daemonize     = /var/log/uwsgi/yourapps.log
+ logger        = file:/var/log/uwsgi/yourapps.log

としたら解消しました。確かにuWSGI のhttps://uwsgi-docs.readthedocs.io/en/latest/Systemd.htmlNOTE: DO NOT daemonize the Emperor (or the master) unless you know what you are doing!!!って書いてありましたが、何がダメなのか良くわからず。