Using Redis backend as a fallback for send_message if a backend does not implement its own send_message

This commit is contained in:
Fabio Manganiello 2018-06-14 20:42:57 +02:00
parent 061b676fbc
commit bd18d1cbc1
2 changed files with 15 additions and 7 deletions

View file

@ -7,6 +7,7 @@ from threading import Thread
from platypush.bus import Bus from platypush.bus import Bus
from platypush.config import Config 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.utils import get_message_class_by_type, set_timeout, clear_timeout
from platypush.message import Message from platypush.message import Message
from platypush.message.event import Event, StopEvent from platypush.message.event import Event, StopEvent
@ -186,13 +187,21 @@ class Backend(Thread):
def send_message(self, msg, **kwargs): def send_message(self, msg, **kwargs):
""" """
Sends a platypush.message.Message to a node. Sends a platypush.message.Message to a node.
To be implemented in the derived classes. To be implemented in the derived classes. By default, if the Redis
Always call send_request or send_response instead of send_message directly backend is configured then it will try to deliver the message to
other consumers through the configured Redis main queue.
Param: Param:
msg -- The message 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): def run(self):

View file

@ -10,7 +10,6 @@ import urllib.parse
from dbus.exceptions import DBusException from dbus.exceptions import DBusException
from omxplayer import OMXPlayer from omxplayer import OMXPlayer
from platypush.context import get_bus
from platypush.plugins.media import PlayerState from platypush.plugins.media import PlayerState
from platypush.message.response import Response from platypush.message.response import Response
from platypush.message.event.video import VideoPlayEvent, VideoPauseEvent, \ from platypush.message.event.video import VideoPlayEvent, VideoPauseEvent, \
@ -198,17 +197,17 @@ class VideoOmxplayerPlugin(Plugin):
def on_play(self): def on_play(self):
def _f(player): def _f(player):
get_bus().post(VideoPlayEvent(video=self.player.get_source())) self.send_message(VideoPlayEvent(video=self.player.get_source()))
return _f return _f
def on_pause(self): def on_pause(self):
def _f(player): def _f(player):
get_bus().post(VideoPauseEvent(video=self.player.get_source())) self.send_message(VideoPauseEvent(video=self.player.get_source()))
return _f return _f
def on_stop(self): def on_stop(self):
def _f(player): def _f(player):
get_bus().post(VideoStopEvent()) self.send_message(VideoStopEvent())
return _f return _f