get_plugin with reload=True should stop the existing plugin if it's running

This commit is contained in:
Fabio Manganiello 2023-03-23 01:11:54 +01:00
parent a2a5fce6cb
commit d1cd6dd2af
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -71,7 +71,7 @@ def register_backends(bus=None, global_scope=False, **kwargs):
else: else:
backends = {} backends = {}
for (name, cfg) in Config.get_backends().items(): for name, cfg in Config.get_backends().items():
module = importlib.import_module('platypush.backend.' + name) module = importlib.import_module('platypush.backend.' + name)
# e.g. backend.pushbullet main class: PushbulletBackend # 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 :param reload: If ``True``, the plugin will be reloaded if it's already
been registered. been registered.
""" """
from ..plugins import Plugin from ..plugins import Plugin, RunnablePlugin
if isinstance(plugin, str): if isinstance(plugin, str):
name = plugin name = plugin
@ -170,8 +170,13 @@ def get_plugin(plugin, plugin_name=None, reload=False):
raise RuntimeError(e) from e raise RuntimeError(e) from e
with plugins_init_locks[name]: with plugins_init_locks[name]:
if _ctx.plugins.get(name) and not reload: plugin = _ctx.plugins.get(name)
return _ctx.plugins[name] if plugin:
if not reload:
return _ctx.plugins[name]
if isinstance(plugin, RunnablePlugin):
plugin.stop()
_ctx.plugins[name] = plugin_class(**plugin_conf) _ctx.plugins[name] = plugin_class(**plugin_conf)
return _ctx.plugins[name] return _ctx.plugins[name]