From d1cd6dd2afc04e043c06131a3373f68a3c8add3f Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 23 Mar 2023 01:11:54 +0100 Subject: [PATCH] get_plugin with reload=True should stop the existing plugin if it's running --- platypush/context/__init__.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/platypush/context/__init__.py b/platypush/context/__init__.py index 9b22d9c0f..c63738b8d 100644 --- a/platypush/context/__init__.py +++ b/platypush/context/__init__.py @@ -71,7 +71,7 @@ def register_backends(bus=None, global_scope=False, **kwargs): else: backends = {} - for (name, cfg) in Config.get_backends().items(): + for name, cfg in Config.get_backends().items(): module = importlib.import_module('platypush.backend.' + name) # e.g. backend.pushbullet main class: PushbulletBackend @@ -119,7 +119,7 @@ def get_plugin(plugin, plugin_name=None, reload=False): :param reload: If ``True``, the plugin will be reloaded if it's already been registered. """ - from ..plugins import Plugin + from ..plugins import Plugin, RunnablePlugin if isinstance(plugin, str): name = plugin @@ -170,8 +170,13 @@ def get_plugin(plugin, plugin_name=None, reload=False): raise RuntimeError(e) from e with plugins_init_locks[name]: - if _ctx.plugins.get(name) and not reload: - return _ctx.plugins[name] + plugin = _ctx.plugins.get(name) + if plugin: + if not reload: + return _ctx.plugins[name] + if isinstance(plugin, RunnablePlugin): + plugin.stop() + _ctx.plugins[name] = plugin_class(**plugin_conf) return _ctx.plugins[name]