Redis plugin added and Redis backend made more robust to work also with single-quoted JSON-like strings returned by the Redis lib

This commit is contained in:
Fabio Manganiello 2018-05-22 14:44:17 +00:00
parent 18efdb1c70
commit f4da6ebb1b
2 changed files with 26 additions and 2 deletions

View file

@ -42,8 +42,15 @@ class RedisBackend(Backend):
while not self.should_stop(): while not self.should_stop():
try: try:
msg = self.redis.blpop(self.queue) msg = self.redis.blpop(self.queue)[1].decode('utf-8')
msg = Message.build(json.loads(msg[1].decode('utf-8')))
try:
msg = Message.build(json.loads(msg))
except:
import ast
msg = Message.build(ast.literal_eval(msg))
logging.info('Received message on the Redis backend: {}'.format(msg))
self.bus.post(msg) self.bus.post(msg)
except Exception as e: except Exception as e:
logging.exception(e) logging.exception(e)

View file

@ -0,0 +1,17 @@
import logging
from redis import Redis
from platypush.message.response import Response
from platypush.plugins import Plugin
class RedisPlugin(Plugin):
def send_message(self, queue, msg, *args, **kwargs):
redis = Redis(*args, **kwargs)
redis.rpush(queue, msg)
return Response(output={'state': 'ok'})
# vim:sw=4:ts=4:et: