From bd18d1cbc18176ea013c6040965b3d2ef731ceb8 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Thu, 14 Jun 2018 20:42:57 +0200 Subject: [PATCH] Using Redis backend as a fallback for send_message if a backend does not implement its own send_message --- platypush/backend/__init__.py | 15 ++++++++++++--- platypush/plugins/video/omxplayer.py | 7 +++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/platypush/backend/__init__.py b/platypush/backend/__init__.py index f7dc225b7..559f41e4a 100644 --- a/platypush/backend/__init__.py +++ b/platypush/backend/__init__.py @@ -7,6 +7,7 @@ from threading import Thread from platypush.bus import Bus from platypush.config import Config +from platypush.context import get_backend from platypush.utils import get_message_class_by_type, set_timeout, clear_timeout from platypush.message import Message from platypush.message.event import Event, StopEvent @@ -186,13 +187,21 @@ class Backend(Thread): def send_message(self, msg, **kwargs): """ Sends a platypush.message.Message to a node. - To be implemented in the derived classes. - Always call send_request or send_response instead of send_message directly + To be implemented in the derived classes. By default, if the Redis + backend is configured then it will try to deliver the message to + other consumers through the configured Redis main queue. Param: msg -- The message """ - pass + try: + redis = get_backend('redis') + except KeyError: + self.logger.warning("Backend {} does not implement send_message " + + "and the fallback Redis backend isn't configured") + return + + redis.send_message(msg) def run(self): diff --git a/platypush/plugins/video/omxplayer.py b/platypush/plugins/video/omxplayer.py index 655a9764b..38e22d504 100644 --- a/platypush/plugins/video/omxplayer.py +++ b/platypush/plugins/video/omxplayer.py @@ -10,7 +10,6 @@ import urllib.parse from dbus.exceptions import DBusException from omxplayer import OMXPlayer -from platypush.context import get_bus from platypush.plugins.media import PlayerState from platypush.message.response import Response from platypush.message.event.video import VideoPlayEvent, VideoPauseEvent, \ @@ -198,17 +197,17 @@ class VideoOmxplayerPlugin(Plugin): def on_play(self): def _f(player): - get_bus().post(VideoPlayEvent(video=self.player.get_source())) + self.send_message(VideoPlayEvent(video=self.player.get_source())) return _f def on_pause(self): def _f(player): - get_bus().post(VideoPauseEvent(video=self.player.get_source())) + self.send_message(VideoPauseEvent(video=self.player.get_source())) return _f def on_stop(self): def _f(player): - get_bus().post(VideoStopEvent()) + self.send_message(VideoStopEvent()) return _f