changeset 28:baedd22fd73a

Implement timeout for Redis backend.
author btimby
date Sun, 05 Feb 2012 03:09:04 +0000
parents 336011507f75
children 2b46eceda739
files queues/backends/redisd.py
diffstat 1 files changed, 15 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/queues/backends/redisd.py	Tue May 31 17:36:47 2011 +0000
+++ b/queues/backends/redisd.py	Sun Feb 05 03:09:04 2012 +0000
@@ -19,9 +19,11 @@
     from django.conf import settings
     CONN = getattr(settings, 'QUEUE_REDIS_CONNECTION', None)
     DB = getattr(settings, 'QUEUE_REDIS_DB', None)
+    TIMEOUT = getattr(settings, 'QUEUE_REDIS_TIMEOUT', None)
 except:
     CONN = os.environ.get('QUEUE_REDIS_CONNECTION', None)
     DB = os.environ.get('QUEUE_REDIS_DB', None)
+    TIMEOUT = os.environ.get('QUEUE_REDIS_TIMEOUT', None)
 
 if not CONN:
     raise InvalidBackend("QUEUE_REDIS_CONNECTION not set.")
@@ -36,11 +38,22 @@
 except ValueError:
     raise InvalidBackend("Port portion of QUEUE_REDIS_CONNECTION should be an integer.")
 
-def _get_connection(host=host, port=port, db=DB):
+def _get_connection(host=host, port=port, db=DB, timeout=TIMEOUT):
     kwargs = {'host' : host, 'port' : port}
     if DB:
         kwargs['db'] = DB
-    return redis.Redis(**kwargs)
+    if timeout:
+        kwargs['timeout'] = float(timeout)
+    try:
+        # Try using the "official" redis kwargs
+        return redis.Redis(**kwargs)
+    except TypeError, e:
+        # Possibly 'timeout' caused an issue...
+        if 'timeout' not in kwargs:
+            raise
+        # Try using Andy McCurdy's library
+        kwargs['socket_timeout'] = kwargs.pop('timeout')
+        return redis.Redis(**kwargs)
 
 class Queue(BaseQueue):
     def __init__(self, name):