@ -13,6 +13,7 @@ import time
import traceback
import traceback
import warnings
import warnings
from datetime import timedelta
from datetime import timedelta
from distutils . version import StrictVersion
from uuid import uuid4
from uuid import uuid4
try :
try :
@ -23,7 +24,7 @@ except ImportError:
from redis import WatchError
from redis import WatchError
from . import worker_registration
from . import worker_registration
from . compat import PY2 , as_text , hmset, string_types, text_type
from . compat import PY2 , as_text , string_types, text_type
from . connections import get_current_connection , push_connection , pop_connection
from . connections import get_current_connection , push_connection , pop_connection
from . defaults import ( DEFAULT_RESULT_TTL ,
from . defaults import ( DEFAULT_RESULT_TTL ,
@ -167,6 +168,8 @@ class Worker(object):
connection = get_current_connection ( )
connection = get_current_connection ( )
self . connection = connection
self . connection = connection
self . redis_server_version = None
if prepare_for_work :
if prepare_for_work :
self . hostname = socket . gethostname ( )
self . hostname = socket . gethostname ( )
self . pid = os . getpid ( )
self . pid = os . getpid ( )
@ -216,6 +219,15 @@ class Worker(object):
elif exception_handlers is not None :
elif exception_handlers is not None :
self . push_exc_handler ( exception_handlers )
self . push_exc_handler ( exception_handlers )
def get_redis_server_version ( self ) :
""" Return Redis server version of connection """
if not self . redis_server_version :
self . redis_server_version = StrictVersion (
self . connection . info ( " server " ) [ " redis_version " ]
)
return self . redis_server_version
def validate_queues ( self ) :
def validate_queues ( self ) :
""" Sanity check for the given queues. """
""" Sanity check for the given queues. """
for queue in self . queues :
for queue in self . queues :
@ -268,7 +280,8 @@ class Worker(object):
now = utcnow ( )
now = utcnow ( )
now_in_string = utcformat ( now )
now_in_string = utcformat ( now )
self . birth_date = now
self . birth_date = now
hmset ( p , key , mapping = {
mapping = {
' birth ' : now_in_string ,
' birth ' : now_in_string ,
' last_heartbeat ' : now_in_string ,
' last_heartbeat ' : now_in_string ,
' queues ' : queues ,
' queues ' : queues ,
@ -276,7 +289,13 @@ class Worker(object):
' hostname ' : self . hostname ,
' hostname ' : self . hostname ,
' version ' : self . version ,
' version ' : self . version ,
' python_version ' : self . python_version ,
' python_version ' : self . python_version ,
} )
}
if self . get_redis_server_version ( ) > = StrictVersion ( " 4.0.0 " ) :
p . hset ( key , mapping = mapping )
else :
p . hmset ( key , mapping )
worker_registration . register ( self , p )
worker_registration . register ( self , p )
p . expire ( key , self . default_worker_ttl + 60 )
p . expire ( key , self . default_worker_ttl + 60 )
p . execute ( )
p . execute ( )
@ -581,6 +600,7 @@ class Worker(object):
if result is not None :
if result is not None :
job , queue = result
job , queue = result
job . redis_server_version = self . get_redis_server_version ( )
if self . log_job_description :
if self . log_job_description :
self . log . info (
self . log . info (
' %s : %s ( %s ) ' , green ( queue . name ) ,
' %s : %s ( %s ) ' , green ( queue . name ) ,