Selaa lähdekoodia

Redis: Compatibility for redis-py < 2.10.0. Closes #2903

Ask Solem 9 vuotta sitten
vanhempi
commit
5d49ae6bc4
2 muutettua tiedostoa jossa 20 lisäystä ja 6 poistoa
  1. 19 6
      celery/backends/redis.py
  2. 1 0
      celery/tests/backends/test_redis.py

+ 19 - 6
celery/backends/redis.py

@@ -63,6 +63,7 @@ class RedisBackend(KeyValueStoreBackend):
         conf = self.app.conf
         if self.redis is None:
             raise ImproperlyConfigured(REDIS_MISSING)
+        self._client_capabilities = self._detect_client_capabilities()
 
         # For compatibility with the old REDIS_* configuration keys.
         def _get(key):
@@ -242,16 +243,28 @@ class RedisBackend(KeyValueStoreBackend):
                 callback.id, exc=ChordError('Join error: {0!r}'.format(exc)),
             )
 
+    def _detect_client_capabilities(self, client_connect_timeout=False):
+        if self.redis.VERSION < (2, 4, 4):
+            raise ImproperlyConfigured(
+                'Redis backend requires redis-py versions 2.4.4 or later. '
+                'You have {0.__version__}'.format(redis))
+        if self.redis.VERSION >= (2, 10):
+            client_connect_timeout = True
+        return {'client_connect_timeout': client_connect_timeout}
+
     def _create_client(self, socket_timeout=None, socket_connect_timeout=None,
                        **params):
-        return self.redis.Redis(
-            connection_pool=self.ConnectionPool(
-                socket_timeout=socket_timeout and float(socket_timeout),
-                socket_connect_timeout=socket_connect_timeout and float(
-                    socket_connect_timeout),
-                **params),
+        return self._new_redis_client(
+            socket_timeout=socket_timeout and float(socket_timeout),
+            socket_connect_timeout=socket_connect_timeout and float(
+                socket_connect_timeout),
         )
 
+    def _new_redis_client(self, **params):
+        if not self._client_capabilities['client_connect_timeout']:
+            params.pop('client_connect_timeout', None)
+        return self.redis.Redis(connection_pool=self.ConnectionPool(**params))
+
     @property
     def ConnectionPool(self):
         if self._ConnectionPool is None:

+ 1 - 0
celery/tests/backends/test_redis.py

@@ -98,6 +98,7 @@ class Redis(MockCallbacks):
 
 
 class redis(object):
+    VERSION = (2, 4, 10)
     Redis = Redis
 
     class ConnectionPool(object):