Skip to content

Commit 88b5a10

Browse files
committed
systemd: auto-shutdown old master post USR2 re-exec
1 parent a852827 commit 88b5a10

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

docs/source/signals.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,9 @@ running::
117117
20859 benoitc 20 0 55748 11m 1500 S 0.0 0.1 0:00.02 gunicorn: worker [test:app]
118118
20860 benoitc 20 0 55748 11m 1500 S 0.0 0.1 0:00.02 gunicorn: worker [test:app]
119119
20861 benoitc 20 0 55748 11m 1500 S 0.0 0.1 0:00.01 gunicorn: worker [test:app]
120+
121+
If no pidfile is available (``kill -TERM $(cat /var/run/gunicorn.pid)``) then killing
122+
the *oldest* process (``pkill --oldest -TERM -f "gunicorn: master "``) should suffice.
123+
124+
When running via systemd socket activation, Gunicorn will *automatically* issue the graceful
125+
shutdown of the old master, as part of starting up the new one.

gunicorn/arbiter.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ def start(self):
173173

174174
self.cfg.when_ready(self)
175175

176+
# systemd: not yet shutting down old master here (wait for workers)
177+
176178
def init_signals(self):
177179
"""\
178180
Initialize master signal handling. Most of the signals
@@ -339,6 +341,12 @@ def maybe_promote_master(self):
339341
# MAINPID does not change here, it was already set on fork
340342
systemd.sd_notify("READY=1\nMAINPID=%d\nSTATUS=Gunicorn arbiter promoted" % (os.getpid(), ), self.log)
341343

344+
elif self.systemd and len(self.WORKERS) >= 1:
345+
# still attached to old master, but we are ready to take over
346+
# this automates `kill -TERM $(cat /var/run/gunicorn.pid)`
347+
self.log.debug("systemd managed: shutting down old master %d after re-exec", self.master_pid)
348+
os.kill(self.master_pid, signal.SIGTERM)
349+
342350
def wakeup(self):
343351
"""\
344352
Wake up the arbiter by writing to the PIPE

0 commit comments

Comments
 (0)