Skip to content

Commit

Permalink
Add on_stopping hook to set anything before master is killed
Browse files Browse the repository at this point in the history
  • Loading branch information
ilpianista committed Apr 3, 2024
1 parent 88fc4a4 commit 6c5dac3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/source/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down
2 changes: 2 additions & 0 deletions gunicorn/arbiter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions gunicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 6c5dac3

Please sign in to comment.