Added disable_monitor flag to RunnablePlugin.

This is useful when users want to use a runnable plugin in a stateless
way. In some cases (for example systems with high latency or limited
quotas for API calls) the user may want to leverage the actions of a
plugin, but without running monitoring/polling logic nor generating
events.
This commit is contained in:
Fabio Manganiello 2024-01-17 22:29:29 +01:00
parent fb91be7628
commit dd5bc7639b
Signed by: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -138,6 +138,7 @@ class RunnablePlugin(Plugin):
self, self,
poll_interval: Optional[float] = 15, poll_interval: Optional[float] = 15,
stop_timeout: Optional[float] = PLUGIN_STOP_TIMEOUT, stop_timeout: Optional[float] = PLUGIN_STOP_TIMEOUT,
disable_monitor: bool = False,
**kwargs, **kwargs,
): ):
""" """
@ -147,10 +148,15 @@ class RunnablePlugin(Plugin):
deprecated. deprecated.
:param stop_timeout: How long we should wait for any running :param stop_timeout: How long we should wait for any running
threads/processes to stop before exiting (default: 5 seconds). threads/processes to stop before exiting (default: 5 seconds).
:param disable_monitor: If set to True then the plugin will not monitor
for new events. This is useful if you want to run a plugin in
stateless mode and only leverage its actions, without triggering any
events. Defaults to False.
""" """
super().__init__(**kwargs) super().__init__(**kwargs)
self.poll_interval = poll_interval self.poll_interval = poll_interval
self.bus: Optional[Bus] = None self.bus: Optional[Bus] = None
self.disable_monitor = disable_monitor
self._should_stop = threading.Event() self._should_stop = threading.Event()
self._stop_timeout = stop_timeout self._stop_timeout = stop_timeout
self._thread: Optional[threading.Thread] = None self._thread: Optional[threading.Thread] = None
@ -178,6 +184,10 @@ class RunnablePlugin(Plugin):
""" """
Wait until a stop event is received. Wait until a stop event is received.
""" """
if self.disable_monitor:
# Wait indefinitely if the monitor is disabled
return self._should_stop.wait(timeout=None)
return self._should_stop.wait(timeout=timeout) return self._should_stop.wait(timeout=timeout)
def start(self): def start(self):
@ -217,6 +227,9 @@ class RunnablePlugin(Plugin):
""" """
Implementation of the runner thread. Implementation of the runner thread.
""" """
if self.disable_monitor:
return
self.logger.info('Starting %s', self.__class__.__name__) self.logger.info('Starting %s', self.__class__.__name__)
while not self.should_stop(): while not self.should_stop():