|
|
@ -1,4 +1,5 @@
|
|
|
|
import importlib
|
|
|
|
import importlib
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
import times
|
|
|
|
import times
|
|
|
|
from uuid import uuid4
|
|
|
|
from uuid import uuid4
|
|
|
|
from cPickle import loads, dumps, UnpicklingError
|
|
|
|
from cPickle import loads, dumps, UnpicklingError
|
|
|
@ -7,8 +8,8 @@ from .exceptions import UnpickleError, NoSuchJobError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JOB_ATTRS = set(['origin', '_func_name', 'ended_at', 'description', '_args',
|
|
|
|
JOB_ATTRS = set(['origin', '_func_name', 'ended_at', 'description', '_args',
|
|
|
|
'created_at', 'enqueued_at', 'connection', '_result', 'timeout',
|
|
|
|
'created_at', 'enqueued_at', 'connection', '_result',
|
|
|
|
'_kwargs', 'exc_info', '_id', 'data'])
|
|
|
|
'timeout', '_kwargs', 'exc_info', '_id', 'data'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def unpickle(pickled_string):
|
|
|
|
def unpickle(pickled_string):
|
|
|
@ -55,6 +56,10 @@ class Job(object):
|
|
|
|
"""
|
|
|
|
"""
|
|
|
|
connection = kwargs.pop('connection', None)
|
|
|
|
connection = kwargs.pop('connection', None)
|
|
|
|
job = cls(connection=connection)
|
|
|
|
job = cls(connection=connection)
|
|
|
|
|
|
|
|
if inspect.ismethod(func):
|
|
|
|
|
|
|
|
job._instance = func.im_self
|
|
|
|
|
|
|
|
job._func_name = func.__name__
|
|
|
|
|
|
|
|
else:
|
|
|
|
job._func_name = '%s.%s' % (func.__module__, func.__name__)
|
|
|
|
job._func_name = '%s.%s' % (func.__module__, func.__name__)
|
|
|
|
job._args = args
|
|
|
|
job._args = args
|
|
|
|
job._kwargs = kwargs
|
|
|
|
job._kwargs = kwargs
|
|
|
@ -71,10 +76,17 @@ class Job(object):
|
|
|
|
if func_name is None:
|
|
|
|
if func_name is None:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.instance:
|
|
|
|
|
|
|
|
return getattr(self.instance, func_name)
|
|
|
|
|
|
|
|
|
|
|
|
module_name, func_name = func_name.rsplit('.', 1)
|
|
|
|
module_name, func_name = func_name.rsplit('.', 1)
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
module = importlib.import_module(module_name)
|
|
|
|
return getattr(module, func_name)
|
|
|
|
return getattr(module, func_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
|
|
|
def instance(self):
|
|
|
|
|
|
|
|
return self._instance
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def args(self):
|
|
|
|
def args(self):
|
|
|
|
return self._args
|
|
|
|
return self._args
|
|
|
@ -105,6 +117,7 @@ class Job(object):
|
|
|
|
self._id = id
|
|
|
|
self._id = id
|
|
|
|
self.created_at = times.now()
|
|
|
|
self.created_at = times.now()
|
|
|
|
self._func_name = None
|
|
|
|
self._func_name = None
|
|
|
|
|
|
|
|
self._instance = None
|
|
|
|
self._args = None
|
|
|
|
self._args = None
|
|
|
|
self._kwargs = None
|
|
|
|
self._kwargs = None
|
|
|
|
self.description = None
|
|
|
|
self.description = None
|
|
|
@ -141,12 +154,11 @@ class Job(object):
|
|
|
|
"""The Redis key that is used to store job hash under."""
|
|
|
|
"""The Redis key that is used to store job hash under."""
|
|
|
|
return self.key_for(self.id)
|
|
|
|
return self.key_for(self.id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@property # noqa
|
|
|
|
@property # noqa
|
|
|
|
def job_tuple(self):
|
|
|
|
def job_tuple(self):
|
|
|
|
"""Returns the job tuple that encodes the actual function call that
|
|
|
|
"""Returns the job tuple that encodes the actual function call that
|
|
|
|
this job represents."""
|
|
|
|
this job represents."""
|
|
|
|
return (self.func_name, self.args, self.kwargs)
|
|
|
|
return (self.func_name, self.instance, self.args, self.kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@property
|
|
|
|
def return_value(self):
|
|
|
|
def return_value(self):
|
|
|
@ -190,7 +202,8 @@ class Job(object):
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return times.to_universal(date_str)
|
|
|
|
return times.to_universal(date_str)
|
|
|
|
self._func_name, self._args, self._kwargs = unpickle(obj.get('data'))
|
|
|
|
|
|
|
|
|
|
|
|
self._func_name, self._instance, self._args, self._kwargs = unpickle(obj.get('data')) # noqa
|
|
|
|
self.created_at = to_date(obj.get('created_at'))
|
|
|
|
self.created_at = to_date(obj.get('created_at'))
|
|
|
|
self.origin = obj.get('origin')
|
|
|
|
self.origin = obj.get('origin')
|
|
|
|
self.description = obj.get('description')
|
|
|
|
self.description = obj.get('description')
|
|
|
@ -199,9 +212,8 @@ class Job(object):
|
|
|
|
self._result = obj.get('result')
|
|
|
|
self._result = obj.get('result')
|
|
|
|
self.exc_info = obj.get('exc_info')
|
|
|
|
self.exc_info = obj.get('exc_info')
|
|
|
|
self.timeout = int(obj.get('timeout')) if obj.get('timeout') else None
|
|
|
|
self.timeout = int(obj.get('timeout')) if obj.get('timeout') else None
|
|
|
|
"""
|
|
|
|
|
|
|
|
Overwrite job's additional attributes (those not in JOB_ATTRS), if any.
|
|
|
|
# Overwrite job's additional attrs (those not in JOB_ATTRS), if any
|
|
|
|
"""
|
|
|
|
|
|
|
|
additional_attrs = set(obj.keys()).difference(JOB_ATTRS)
|
|
|
|
additional_attrs = set(obj.keys()).difference(JOB_ATTRS)
|
|
|
|
for attr in additional_attrs:
|
|
|
|
for attr in additional_attrs:
|
|
|
|
setattr(self, attr, obj[attr])
|
|
|
|
setattr(self, attr, obj[attr])
|
|
|
|