From 2e0d3f36b4d2e18e6ae22c0a75214b4176f77fbd Mon Sep 17 00:00:00 2001 From: Arksine Date: Mon, 8 Mar 2021 17:04:06 -0500 Subject: [PATCH] moonraker: call optional "on_exit()" plugin method before "close()" This allows plugins to interact with other plugins before they are closed. For example, a plugin may wish to save persistent state to the database before it is closed. Signed-off-by: Eric Callahan --- moonraker/moonraker.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/moonraker/moonraker.py b/moonraker/moonraker.py index 01a0a1e..b045865 100755 --- a/moonraker/moonraker.py +++ b/moonraker/moonraker.py @@ -480,14 +480,18 @@ class Server: async def _stop_server(self, exit_reason="restart"): self.server_running = False - for name, plugin in self.plugins.items(): - if hasattr(plugin, "close"): + for method in ["on_exit", "close"]: + for name, plugin in self.plugins.items(): + if not hasattr(plugin, method): + continue + func = getattr(plugin, method) try: - ret = plugin.close() + ret = func() if asyncio.iscoroutine(ret): await ret except Exception: - logging.exception(f"Error closing plugin: {name}") + logging.exception( + f"Error executing '{method}()' for plugin: {name}") try: if self.klippy_connection.is_connected(): self.klippy_disconnect_evt = Event()