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 <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-03-08 17:04:06 -05:00
parent 5144508410
commit 2e0d3f36b4
1 changed files with 8 additions and 4 deletions

View File

@ -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()