Added wait_stop() method to RunnablePlugin

This commit is contained in:
Fabio Manganiello 2022-07-23 17:33:23 +02:00
parent 32be4df11c
commit c32142c8b5
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774
2 changed files with 17 additions and 12 deletions

View file

@ -19,12 +19,12 @@ def action(f):
result = f(*args, **kwargs) result = f(*args, **kwargs)
if result and isinstance(result, Response): if result and isinstance(result, Response):
result.errors = result.errors \ result.errors = (
if isinstance(result.errors, list) else [result.errors] result.errors if isinstance(result.errors, list) else [result.errors]
)
response = result response = result
elif isinstance(result, tuple) and len(result) == 2: elif isinstance(result, tuple) and len(result) == 2:
response.errors = result[1] \ response.errors = result[1] if isinstance(result[1], list) else [result[1]]
if isinstance(result[1], list) else [result[1]]
if len(response.errors) == 1 and response.errors[0] is None: if len(response.errors) == 1 and response.errors[0] is None:
response.errors = [] response.errors = []
@ -39,12 +39,14 @@ def action(f):
return _execute_action return _execute_action
class Plugin(EventGenerator, ExtensionWithManifest): # lgtm [py/missing-call-to-init] class Plugin(EventGenerator, ExtensionWithManifest): # lgtm [py/missing-call-to-init]
""" Base plugin class """ """Base plugin class"""
def __init__(self, **kwargs): def __init__(self, **kwargs):
super().__init__() super().__init__()
self.logger = logging.getLogger('platypush:plugin:' + get_plugin_name_by_class(self.__class__)) self.logger = logging.getLogger(
'platypush:plugin:' + get_plugin_name_by_class(self.__class__)
)
if 'logging' in kwargs: if 'logging' in kwargs:
self.logger.setLevel(getattr(logging, kwargs['logging'].upper())) self.logger.setLevel(getattr(logging, kwargs['logging'].upper()))
@ -53,8 +55,9 @@ class Plugin(EventGenerator, ExtensionWithManifest): # lgtm [py/missing-call-t
) )
def run(self, method, *args, **kwargs): def run(self, method, *args, **kwargs):
assert method in self.registered_actions, '{} is not a registered action on {}'.\ assert (
format(method, self.__class__.__name__) method in self.registered_actions
), '{} is not a registered action on {}'.format(method, self.__class__.__name__)
return getattr(self, method)(*args, **kwargs) return getattr(self, method)(*args, **kwargs)
@ -62,6 +65,7 @@ class RunnablePlugin(Plugin):
""" """
Class for runnable plugins - i.e. plugins that have a start/stop method and can be started. Class for runnable plugins - i.e. plugins that have a start/stop method and can be started.
""" """
def __init__(self, poll_interval: Optional[float] = None, **kwargs): def __init__(self, poll_interval: Optional[float] = None, **kwargs):
""" """
:param poll_interval: How often the :meth:`.loop` function should be execute (default: None, no pause/interval). :param poll_interval: How often the :meth:`.loop` function should be execute (default: None, no pause/interval).
@ -78,6 +82,9 @@ class RunnablePlugin(Plugin):
def should_stop(self): def should_stop(self):
return self._should_stop.is_set() return self._should_stop.is_set()
def wait_stop(self, timeout=None):
return self._should_stop.wait(timeout=timeout)
def start(self): def start(self):
set_thread_name(self.__class__.__name__) set_thread_name(self.__class__.__name__)
self._thread = threading.Thread(target=self._runner) self._thread = threading.Thread(target=self._runner)

View file

@ -121,9 +121,7 @@ class NtfyPlugin(RunnablePlugin):
def main(self): def main(self):
if self._subscriptions: if self._subscriptions:
self._connect() self._connect()
self.wait_stop()
while not self._should_stop.is_set():
self._should_stop.wait(timeout=1)
def stop(self): def stop(self):
if self._ws_proc: if self._ws_proc: