@@ -173,9 +173,7 @@ def start(self):
173173
174174 self .cfg .when_ready (self )
175175
176- # # call `pkill --oldest -TERM -f "gunicorn: master "` instead
177- # if self.master_pid and self.systemd:
178- # os.kill(self.master_pid, signal.SIGTERM)
176+ # systemd: not yet shutting down old master here (wait for workers)
179177
180178 def init_signals (self ):
181179 """\
@@ -343,6 +341,13 @@ def maybe_promote_master(self):
343341 # MAINPID does not change here, it was already set on fork
344342 systemd .sd_notify ("READY=1\n MAINPID=%d\n STATUS=Gunicorn arbiter promoted\n " % (os .getpid (), ), self .log )
345343
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+
350+
346351 def wakeup (self ):
347352 """\
348353 Wake up the arbiter by writing to the PIPE
@@ -464,7 +469,7 @@ def reexec(self):
464469 self .log .debug ("exe=%r argv=%r" % (self .START_CTX [0 ], self .START_CTX ['args' ]))
465470 # let systemd know we will be in control after exec()
466471 systemd .sd_notify (
467- "RELOADING=1\n MAINPID=%d\n STATUS=Gunicorn arbiter re-exec in progress..\n " % (self . reexec_pid , ), self .log
472+ "RELOADING=1\n MAINPID=%d\n STATUS=Gunicorn arbiter re-exec in progress..\n " % (os . getpid () , ), self .log
468473 )
469474 os .execve (self .START_CTX [0 ], self .START_CTX ['args' ], environ )
470475
0 commit comments