Added disable_web_clients_notification on Event for events

generated with high throughput that shouldn't be propagated
to the connected websockets.
This commit is contained in:
Fabio Manganiello 2019-12-22 19:38:01 +01:00
parent d6515ed991
commit 470f4a8fc9
4 changed files with 25 additions and 25 deletions

View file

@ -9,9 +9,6 @@ import argparse
import logging
import os
import sys
import traceback
from threading import Thread
from .bus import Bus
from .bus.redis import RedisBus
@ -33,6 +30,7 @@ __version__ = '0.11.2'
LOGGER = logging.getLogger(__name__)
LOGGER.setLevel(logging.INFO)
class Daemon:
""" Main class for the Platypush daemon """
@ -149,14 +147,12 @@ class Daemon:
return _f
def stop_app(self):
""" Stops the backends and the bus """
for backend in self.backends.values():
backend.stop()
self.bus.stop()
def start(self):
""" Start the daemon """
if not self.no_capture_stdout:

View file

@ -1,33 +1,35 @@
import logging
import sys
from ..hook import EventHook
from platypush.config import Config
from platypush.context import get_backend
from platypush.message.event import Event
class EventProcessor(object):
""" Event processor class. Checks an event against the configured
rules and executes any matching event hooks """
def __init__(self, hooks=None, **kwargs):
def __init__(self, hooks=None):
"""
Params:
hooks -- List of event hooks (default: any entry in the config
named as event.hook.<hook_name> """
if hooks is None: hooks = Config.get_event_hooks()
if hooks is None:
hooks = Config.get_event_hooks()
self.hooks = []
for (name, hook) in hooks.items():
h = EventHook.build(name=name, hook=hook)
self.hooks.append(h)
def notify_web_clients(self, event):
@staticmethod
def notify_web_clients(event):
backends = Config.get_backends()
if 'http' not in backends: return
if 'http' not in backends:
return
backend = get_backend('http')
if backend:
@ -37,26 +39,29 @@ class EventProcessor(object):
if backend:
backend.notify_web_clients(event)
def process_event(self, event):
def process_event(self, event: Event):
""" Processes an event and runs the matched hooks with the highest score """
self.notify_web_clients(event)
matched_hooks = set(); priority_hooks = set()
max_score = -sys.maxsize; max_prio = 0
if not event.disable_web_clients_notification:
self.notify_web_clients(event)
matched_hooks = set()
priority_hooks = set()
max_score = -sys.maxsize
max_priority = 0
for hook in self.hooks:
match = hook.matches_event(event)
if match.is_match:
if match.score > max_score:
matched_hooks = set((hook,))
matched_hooks = {hook}
max_score = match.score
elif match.score == max_score:
matched_hooks.add(hook)
if hook.priority > max_prio:
priority_hooks = set((hook,))
elif hook.priority == max_prio and max_prio > 0:
if hook.priority > max_priority:
priority_hooks = {hook}
elif hook.priority == max_priority and max_priority > 0:
priority_hooks.add(hook)
matched_hooks.update(priority_hooks)
@ -65,4 +70,3 @@ class EventProcessor(object):
# vim:sw=4:ts=4:et:

View file

@ -19,10 +19,8 @@ class Event(Message):
# will be disabled. Logging is usually disabled for events with a very
# high frequency that would otherwise pollute the logs e.g. camera capture
# events
disable_logging = False
def __init__(self, target=None, origin=None, id=None, timestamp=None,
disable_logging=disable_logging, **kwargs):
disable_logging=False, disable_web_clients_notification=False, **kwargs):
"""
Params:
target -- Target node [String]
@ -39,6 +37,7 @@ class Event(Message):
self.__class__.__name__)
self.args = kwargs
self.disable_logging = disable_logging
self.disable_web_clients_notification = disable_web_clients_notification
for arg, value in self.args.items():
self.__setattr__(arg, value)

View file

@ -6,7 +6,8 @@ class DistanceSensorEvent(Event):
Event triggered when a new value is processed by a distance sensor.
"""
def __init__(self, distance: float, unit: str = 'mm', *args, **kwargs):
super().__init__(*args, distance=distance, unit=unit, **kwargs)
super().__init__(*args, distance=distance, unit=unit, disable_logging=True,
disable_web_clients_notification=True, **kwargs)
# vim:sw=4:ts=4:et: