From f4da6ebb1b5cf510c856da7cafaf470e2b3e7c82 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 22 May 2018 14:44:17 +0000 Subject: [PATCH] Redis plugin added and Redis backend made more robust to work also with single-quoted JSON-like strings returned by the Redis lib --- platypush/backend/redis.py | 11 +++++++++-- platypush/plugins/redis.py | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 platypush/plugins/redis.py diff --git a/platypush/backend/redis.py b/platypush/backend/redis.py index 79c5bb31fb..2b8cb0ae74 100644 --- a/platypush/backend/redis.py +++ b/platypush/backend/redis.py @@ -42,8 +42,15 @@ class RedisBackend(Backend): while not self.should_stop(): try: - msg = self.redis.blpop(self.queue) - msg = Message.build(json.loads(msg[1].decode('utf-8'))) + msg = self.redis.blpop(self.queue)[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) except Exception as e: logging.exception(e) diff --git a/platypush/plugins/redis.py b/platypush/plugins/redis.py new file mode 100644 index 0000000000..466b5131cb --- /dev/null +++ b/platypush/plugins/redis.py @@ -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: +