get_redis_server_version() should handle 4 digit version numbers (#1322)

main
Selwin Ong 4 years ago committed by GitHub
parent 7bf100ebe7
commit 39fb709c10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -18,7 +18,7 @@ from .connections import resolve_connection
from .exceptions import NoSuchJobError from .exceptions import NoSuchJobError
from .local import LocalStack from .local import LocalStack
from .serializers import resolve_serializer from .serializers import resolve_serializer
from .utils import (enum, import_attribute, parse_timeout, str_to_date, from .utils import (enum, get_version, import_attribute, parse_timeout, str_to_date,
utcformat, utcnow) utcformat, utcnow)
# Serialize pickle dumps using the highest pickle protocol (binary, default # Serialize pickle dumps using the highest pickle protocol (binary, default
@ -590,9 +590,7 @@ class Job(object):
def get_redis_server_version(self): def get_redis_server_version(self):
"""Return Redis server version of connection""" """Return Redis server version of connection"""
if not self.redis_server_version: if not self.redis_server_version:
self.redis_server_version = StrictVersion( self.redis_server_version = get_version(self.connection)
self.connection.info("server")["redis_version"]
)
return self.redis_server_version return self.redis_server_version

@ -15,7 +15,7 @@ from .defaults import DEFAULT_RESULT_TTL
from .exceptions import DequeueTimeout, NoSuchJobError from .exceptions import DequeueTimeout, NoSuchJobError
from .job import Job, JobStatus from .job import Job, JobStatus
from .serializers import resolve_serializer from .serializers import resolve_serializer
from .utils import backend_class, import_attribute, parse_timeout, utcnow from .utils import backend_class, get_version, import_attribute, parse_timeout, utcnow
def compact(lst): def compact(lst):
@ -92,10 +92,7 @@ class Queue(object):
def get_redis_server_version(self): def get_redis_server_version(self):
"""Return Redis server version of connection""" """Return Redis server version of connection"""
if not self.redis_server_version: if not self.redis_server_version:
self.redis_server_version = StrictVersion( self.redis_server_version = get_version(self.connection)
self.connection.info("server")["redis_version"]
)
return self.redis_server_version return self.redis_server_version
@property @property

@ -14,10 +14,9 @@ import importlib
import logging import logging
import numbers import numbers
import sys import sys
try:
from collections.abc import Iterable from collections import Iterable
except ImportError: from distutils.version import StrictVersion
from collections import Iterable
from .compat import as_text, is_python_version, string_types from .compat import as_text, is_python_version, string_types
from .exceptions import TimeoutFormatError from .exceptions import TimeoutFormatError
@ -248,3 +247,12 @@ def parse_timeout(timeout):
'such as "1h", "23m".') 'such as "1h", "23m".')
return timeout return timeout
def get_version(connection):
"""
Return StrictVersion of Redis server version.
This function also correctly handles 4 digit redis server versions.
"""
version_string = connection.info("server")["redis_version"]
return StrictVersion('.'.join(version_string.split('.')[:3]))

@ -39,7 +39,7 @@ from .registry import FailedJobRegistry, StartedJobRegistry, clean_registries
from .scheduler import RQScheduler from .scheduler import RQScheduler
from .suspension import is_suspended from .suspension import is_suspended
from .timeouts import JobTimeoutException, HorseMonitorTimeoutException, UnixSignalDeathPenalty from .timeouts import JobTimeoutException, HorseMonitorTimeoutException, UnixSignalDeathPenalty
from .utils import (backend_class, ensure_list, enum, from .utils import (backend_class, ensure_list, enum, get_version,
make_colorizer, utcformat, utcnow, utcparse) make_colorizer, utcformat, utcnow, utcparse)
from .version import VERSION from .version import VERSION
from .worker_registration import clean_worker_registry, get_keys from .worker_registration import clean_worker_registry, get_keys
@ -223,10 +223,7 @@ class Worker(object):
def get_redis_server_version(self): def get_redis_server_version(self):
"""Return Redis server version of connection""" """Return Redis server version of connection"""
if not self.redis_server_version: if not self.redis_server_version:
self.redis_server_version = StrictVersion( self.redis_server_version = get_version(self.connection)
self.connection.info("server")["redis_version"]
)
return self.redis_server_version return self.redis_server_version
def validate_queues(self): def validate_queues(self):

@ -554,7 +554,7 @@ class TestQueue(RQTestCase):
q = Queue() q = Queue()
with patch('rq.queue.Job.create', new=MultipleDependencyJob.create): with patch('rq.queue.Job.create', new=MultipleDependencyJob.create):
job = q.enqueue(say_hello, depends_on=parent_jobs[0], job = q.enqueue(say_hello, depends_on=parent_jobs[0],
_dependency_ids = [job.id for job in parent_jobs]) _dependency_ids=[job.id for job in parent_jobs])
self.assertEqual(job.get_status(), JobStatus.DEFERRED) self.assertEqual(job.get_status(), JobStatus.DEFERRED)
self.assertEqual(q.job_ids, []) self.assertEqual(q.job_ids, [])
self.assertEqual(job.fetch_dependencies(), parent_jobs) self.assertEqual(job.fetch_dependencies(), parent_jobs)

@ -3,8 +3,14 @@ from __future__ import (absolute_import, division, print_function,
unicode_literals) unicode_literals)
import re import re
import datetime import datetime
import mock
from distutils.version import StrictVersion
from redis import Redis
from tests import RQTestCase, fixtures from tests import RQTestCase, fixtures
from rq.utils import parse_timeout, first, is_nonstring_iterable, ensure_list, utcparse, backend_class from rq.utils import backend_class, ensure_list, first, get_version, is_nonstring_iterable, parse_timeout, utcparse
from rq.exceptions import TimeoutFormatError from rq.exceptions import TimeoutFormatError
@ -66,3 +72,20 @@ class TestUtils(RQTestCase):
self.assertEqual(fixtures.DummyQueue, backend_class(fixtures, 'DummyQueue', override=fixtures.DummyQueue)) self.assertEqual(fixtures.DummyQueue, backend_class(fixtures, 'DummyQueue', override=fixtures.DummyQueue))
self.assertEqual(fixtures.DummyQueue, self.assertEqual(fixtures.DummyQueue,
backend_class(fixtures, 'DummyQueue', override='tests.fixtures.DummyQueue')) backend_class(fixtures, 'DummyQueue', override='tests.fixtures.DummyQueue'))
def test_get_redis_version(self):
"""Ensure get_version works properly"""
redis = Redis()
self.assertTrue(isinstance(get_version(redis), StrictVersion))
# Parses 3 digit version numbers correctly
class DummyRedis(Redis):
def info(*args):
return {'redis_version': '4.0.8'}
self.assertEqual(get_version(DummyRedis()), StrictVersion('4.0.8'))
# Parses 3 digit version numbers correctly
class DummyRedis(Redis):
def info(*args):
return {'redis_version': '3.0.7.9'}
self.assertEqual(get_version(DummyRedis()), StrictVersion('3.0.7'))
Loading…
Cancel
Save