diff --git a/docs/source/deploy.rst b/docs/source/deploy.rst index 5f8689793..eb83baf35 100644 --- a/docs/source/deploy.rst +++ b/docs/source/deploy.rst @@ -247,7 +247,8 @@ to the newly created unix socket: [Service] # gunicorn can let systemd know when it is ready - Type=notify + # if systemd versions >= v253, otherwise use 'Type=notify' + Type=notify-reload NotifyAccess=main # the specific user that our service will run as User=someuser @@ -257,7 +258,8 @@ to the newly created unix socket: RuntimeDirectory=gunicorn WorkingDirectory=/home/someuser/applicationroot ExecStart=/usr/bin/gunicorn applicationname.wsgi - ExecReload=/bin/kill -s HUP $MAINPID + # if 'Type=notify' instead of 'Type=notify-reload' (depending on systemd version) + # ExecReload=/bin/kill -s HUP $MAINPID KillMode=mixed TimeoutStopSec=5 PrivateTmp=true diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 646d684ef..4d3337dce 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -251,7 +251,9 @@ def handle_hup(self): - Gracefully shutdown the old worker processes """ self.log.info("Hang up: %s", self.master_name) + systemd.sd_notify("RELOADING=1\nSTATUS=Gunicorn arbiter reloading", self.log) self.reload() + systemd.sd_notify("READY=1\nSTATUS=Gunicorn arbiter reloaded", self.log) def handle_term(self): "SIGTERM handling" @@ -340,6 +342,7 @@ def wakeup(self): def halt(self, reason=None, exit_status=0): """ halt arbiter """ + systemd.sd_notify("STOPPING=1\nSTATUS=Gunicorn arbiter stopping", self.log) self.stop() log_func = self.log.info if exit_status == 0 else self.log.error diff --git a/gunicorn/systemd.py b/gunicorn/systemd.py index 9b1855060..50351c0bb 100644 --- a/gunicorn/systemd.py +++ b/gunicorn/systemd.py @@ -4,6 +4,7 @@ import os import socket +import time SD_LISTEN_FDS_START = 3 @@ -66,6 +67,8 @@ def sd_notify(state, logger, unset_environment=False): if addr[0] == '@': addr = '\0' + addr[1:] sock.connect(addr) + monotonic_usecs = time.clock_gettime_ns(time.CLOCK_MONOTONIC) // 1000 + state = "%s\nMONOTONIC_USEC=%d" % (state, monotonic_usecs) sock.sendall(state.encode('utf-8')) except Exception: logger.debug("Exception while invoking sd_notify()", exc_info=True)