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

View file

@ -1,33 +1,35 @@
import logging
import sys import sys
from ..hook import EventHook from ..hook import EventHook
from platypush.config import Config from platypush.config import Config
from platypush.context import get_backend from platypush.context import get_backend
from platypush.message.event import Event
class EventProcessor(object): class EventProcessor(object):
""" Event processor class. Checks an event against the configured """ Event processor class. Checks an event against the configured
rules and executes any matching event hooks """ rules and executes any matching event hooks """
def __init__(self, hooks=None, **kwargs): def __init__(self, hooks=None):
""" """
Params: Params:
hooks -- List of event hooks (default: any entry in the config hooks -- List of event hooks (default: any entry in the config
named as event.hook.<hook_name> """ 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 = [] self.hooks = []
for (name, hook) in hooks.items(): for (name, hook) in hooks.items():
h = EventHook.build(name=name, hook=hook) h = EventHook.build(name=name, hook=hook)
self.hooks.append(h) self.hooks.append(h)
@staticmethod
def notify_web_clients(self, event): def notify_web_clients(event):
backends = Config.get_backends() backends = Config.get_backends()
if 'http' not in backends: return if 'http' not in backends:
return
backend = get_backend('http') backend = get_backend('http')
if backend: if backend:
@ -37,26 +39,29 @@ class EventProcessor(object):
if backend: if backend:
backend.notify_web_clients(event) backend.notify_web_clients(event)
def process_event(self, event: Event):
def process_event(self, event):
""" Processes an event and runs the matched hooks with the highest score """ """ Processes an event and runs the matched hooks with the highest score """
if not event.disable_web_clients_notification:
self.notify_web_clients(event) self.notify_web_clients(event)
matched_hooks = set(); priority_hooks = set()
max_score = -sys.maxsize; max_prio = 0 matched_hooks = set()
priority_hooks = set()
max_score = -sys.maxsize
max_priority = 0
for hook in self.hooks: for hook in self.hooks:
match = hook.matches_event(event) match = hook.matches_event(event)
if match.is_match: if match.is_match:
if match.score > max_score: if match.score > max_score:
matched_hooks = set((hook,)) matched_hooks = {hook}
max_score = match.score max_score = match.score
elif match.score == max_score: elif match.score == max_score:
matched_hooks.add(hook) matched_hooks.add(hook)
if hook.priority > max_prio: if hook.priority > max_priority:
priority_hooks = set((hook,)) priority_hooks = {hook}
elif hook.priority == max_prio and max_prio > 0: elif hook.priority == max_priority and max_priority > 0:
priority_hooks.add(hook) priority_hooks.add(hook)
matched_hooks.update(priority_hooks) matched_hooks.update(priority_hooks)
@ -65,4 +70,3 @@ class EventProcessor(object):
# vim:sw=4:ts=4:et: # 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 # will be disabled. Logging is usually disabled for events with a very
# high frequency that would otherwise pollute the logs e.g. camera capture # high frequency that would otherwise pollute the logs e.g. camera capture
# events # events
disable_logging = False
def __init__(self, target=None, origin=None, id=None, timestamp=None, 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: Params:
target -- Target node [String] target -- Target node [String]
@ -39,6 +37,7 @@ class Event(Message):
self.__class__.__name__) self.__class__.__name__)
self.args = kwargs self.args = kwargs
self.disable_logging = disable_logging self.disable_logging = disable_logging
self.disable_web_clients_notification = disable_web_clients_notification
for arg, value in self.args.items(): for arg, value in self.args.items():
self.__setattr__(arg, value) 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. Event triggered when a new value is processed by a distance sensor.
""" """
def __init__(self, distance: float, unit: str = 'mm', *args, **kwargs): 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: # vim:sw=4:ts=4:et: