diff --git a/docs/source/settings.rst b/docs/source/settings.rst index 4be760c28..91123c225 100644 --- a/docs/source/settings.rst +++ b/docs/source/settings.rst @@ -725,6 +725,20 @@ Called to recycle workers during a reload via SIGHUP. The callable needs to accept a single instance variable for the Arbiter. +``on_stopping`` +~~~~~~~~~~~~~~~ + +**Default:** + +.. code-block:: python + + def on_stopping(server): + pass + +Called just before the master process is stopped. + +The callable needs to accept a single instance variable for the Arbiter. + .. _when-ready: ``when_ready`` diff --git a/gunicorn/arbiter.py b/gunicorn/arbiter.py index 008a54efe..04d60c559 100644 --- a/gunicorn/arbiter.py +++ b/gunicorn/arbiter.py @@ -377,6 +377,8 @@ def stop(self, graceful=True): :attr graceful: boolean, If True (the default) workers will be killed gracefully (ie. trying to wait for the current connection) """ + self.cfg.on_stopping(self) + unlink = ( self.reexec_pid == self.master_pid == 0 and not self.systemd diff --git a/gunicorn/config.py b/gunicorn/config.py index 3b57fad0f..0b7959094 100644 --- a/gunicorn/config.py +++ b/gunicorn/config.py @@ -1767,6 +1767,22 @@ def on_reload(server): """ +class OnStopping(Setting): + name = "on_stopping" + section = "Server Hooks" + validator = validate_callable(1) + type = callable + + def on_stopping(server): + pass + default = staticmethod(on_stopping) + desc = """\ + Called just before the master process is stopped. + + The callable needs to accept a single instance variable for the Arbiter. + """ + + class WhenReady(Setting): name = "when_ready" section = "Server Hooks"