From c8f7eb30aaec8f676922357b09427f689a8d0f69 Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Tue, 18 Dec 2018 15:51:37 +0100 Subject: [PATCH] Made Redis backend and plugin more robust. - Redis arguments read either from the backend or the plugin configuration as a fallback in case of partial conf - send_message now falls back on the default Redis connection configuration if *args and **kwargs are missing --- platypush/backend/redis.py | 10 ++++++++++ platypush/plugins/redis.py | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/platypush/backend/redis.py b/platypush/backend/redis.py index 3d4dd0e0..b1d80534 100644 --- a/platypush/backend/redis.py +++ b/platypush/backend/redis.py @@ -3,6 +3,7 @@ import json from redis import Redis from platypush.backend import Backend +from platypush.context import get_plugin from platypush.message import Message @@ -31,6 +32,15 @@ class RedisBackend(Backend): self.queue = queue self.redis_args = redis_args + + if not redis_args: + try: + redis_plugin = get_plugin('redis') + if redis_plugin and redis_plugin.kwargs: + self.redis_args = redis_plugin.kwargs + except: + pass + self.redis = Redis(**self.redis_args) diff --git a/platypush/plugins/redis.py b/platypush/plugins/redis.py index dfce8d51..6cd62f38 100644 --- a/platypush/plugins/redis.py +++ b/platypush/plugins/redis.py @@ -1,5 +1,6 @@ from redis import Redis +from platypush.context import get_backend from platypush.plugins import Plugin, action @@ -17,6 +18,14 @@ class RedisPlugin(Plugin): self.args = args self.kwargs = kwargs + if not kwargs: + try: + redis_backend = get_backend('redis') + if redis_backend and redis_backend.redis_args: + self.kwargs = redis_backend.redis_args + except: + pass + def _get_redis(self): return Redis(*self.args, **self.kwargs) @@ -38,8 +47,12 @@ class RedisPlugin(Plugin): :type kwargs: dict """ - redis = Redis(*args, **kwargs) - redis.rpush(queue, str(msg)) + if args or kwargs: + redis = Redis(*args, **kwargs) + else: + redis = self._get_redis() + + return redis.rpush(queue, str(msg)) @action def mget(self, keys, *args):