From 470f4a8fc98d690d21b8a4bd89ad472eccf19842 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 22 Dec 2019 19:38:01 +0100 Subject: [PATCH] Added disable_web_clients_notification on Event for events generated with high throughput that shouldn't be propagated to the connected websockets. --- platypush/__init__.py | 6 +---- platypush/event/processor/__init__.py | 36 +++++++++++++++------------ platypush/message/event/__init__.py | 5 ++-- platypush/message/event/distance.py | 3 ++- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/platypush/__init__.py b/platypush/__init__.py index a4556789..aa9433ad 100644 --- a/platypush/__init__.py +++ b/platypush/__init__.py @@ -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: diff --git a/platypush/event/processor/__init__.py b/platypush/event/processor/__init__.py index 7cf119fc..98cede2d 100644 --- a/platypush/event/processor/__init__.py +++ b/platypush/event/processor/__init__.py @@ -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. """ - 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: - diff --git a/platypush/message/event/__init__.py b/platypush/message/event/__init__.py index f2499b35..e91f3a5c 100644 --- a/platypush/message/event/__init__.py +++ b/platypush/message/event/__init__.py @@ -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) diff --git a/platypush/message/event/distance.py b/platypush/message/event/distance.py index b72beafa..70992dc7 100644 --- a/platypush/message/event/distance.py +++ b/platypush/message/event/distance.py @@ -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: