diff --git a/rq/job.py b/rq/job.py new file mode 100644 index 0000000..4b58105 --- /dev/null +++ b/rq/job.py @@ -0,0 +1,41 @@ +from pickle import loads +from .exceptions import DequeueError + + +class Job(object): + """A Job is just a convenient datastructure to pass around job (meta) data. + """ + __slots__ = ['func', 'args', 'kwargs', 'rv_key', 'origin'] + + @classmethod + def unpickle(cls, pickle_data): + """Constructs a Job instance form the given pickle'd job tuple data.""" + try: + job_tuple = loads(pickle_data) + return Job(job_tuple) + except (AttributeError, ValueError, IndexError): + raise DequeueError('Could not decode job tuple.') + + def __init__(self, job_tuple, origin=None): + self.func, self.args, self.kwargs, self.rv_key = job_tuple + self.origin = origin + + def perform(self): + """Invokes the job function with the job arguments. + """ + return self.func(*self.args, **self.kwargs) + + @property + def call_string(self): + """Returns a string representation of the call, formatted as a regular + Python function invocation statement. + """ + arg_list = map(repr, self.args) + arg_list += map(lambda tup: '%s=%r' % (tup[0], tup[1]), + self.kwargs.items()) + return '%s(%s)' % (self.func.__name__, ', '.join(arg_list)) + + def __str__(self): + return '' % self.call_string + + diff --git a/rq/queue.py b/rq/queue.py index 4e078b1..bdd37a0 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -2,7 +2,8 @@ import uuid from functools import total_ordering from pickle import loads, dumps from .proxy import conn -from .exceptions import DequeueError +from .job import Job + class DelayedResult(object): """Proxy object that is returned as a result of `Queue.enqueue()` calls. @@ -37,43 +38,6 @@ class DelayedResult(object): return self._rv -class Job(object): - """A Job is just a convenient datastructure to pass around job (meta) data. - """ - __slots__ = ['func', 'args', 'kwargs', 'rv_key', 'origin'] - - @classmethod - def unpickle(cls, pickle_data): - """Constructs a Job instance form the given pickle'd job tuple data.""" - try: - job_tuple = loads(pickle_data) - return Job(job_tuple) - except (AttributeError, ValueError, IndexError): - raise DequeueError('Could not decode job tuple.') - - def __init__(self, job_tuple, origin=None): - self.func, self.args, self.kwargs, self.rv_key = job_tuple - self.origin = origin - - def perform(self): - """Invokes the job function with the job arguments. - """ - return self.func(*self.args, **self.kwargs) - - @property - def call_string(self): - """Returns a string representation of the call, formatted as a regular - Python function invocation statement. - """ - arg_list = map(repr, self.args) - arg_list += map(lambda tup: '%s=%r' % (tup[0], tup[1]), - self.kwargs.items()) - return '%s(%s)' % (self.func.__name__, ', '.join(arg_list)) - - def __str__(self): - return '' % self.call_string - - @total_ordering class Queue(object): redis_queue_namespace_prefix = 'rq:queue:'