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
This commit is contained in:
Fabio Manganiello 2018-12-18 15:51:37 +01:00
parent 8fe4d77e3d
commit c8f7eb30aa
2 changed files with 25 additions and 2 deletions

View file

@ -3,6 +3,7 @@ import json
from redis import Redis from redis import Redis
from platypush.backend import Backend from platypush.backend import Backend
from platypush.context import get_plugin
from platypush.message import Message from platypush.message import Message
@ -31,6 +32,15 @@ class RedisBackend(Backend):
self.queue = queue self.queue = queue
self.redis_args = redis_args 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) self.redis = Redis(**self.redis_args)

View file

@ -1,5 +1,6 @@
from redis import Redis from redis import Redis
from platypush.context import get_backend
from platypush.plugins import Plugin, action from platypush.plugins import Plugin, action
@ -17,6 +18,14 @@ class RedisPlugin(Plugin):
self.args = args self.args = args
self.kwargs = kwargs 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): def _get_redis(self):
return Redis(*self.args, **self.kwargs) return Redis(*self.args, **self.kwargs)
@ -38,8 +47,12 @@ class RedisPlugin(Plugin):
:type kwargs: dict :type kwargs: dict
""" """
redis = Redis(*args, **kwargs) if args or kwargs:
redis.rpush(queue, str(msg)) redis = Redis(*args, **kwargs)
else:
redis = self._get_redis()
return redis.rpush(queue, str(msg))
@action @action
def mget(self, keys, *args): def mget(self, keys, *args):