Encapsulate internal function call representation.

This means it's not allowed anymore to directly set func, args, and
kwargs.  Instead, use the for_call() constructor.
main
Vincent Driessen 13 years ago
parent 370399f8f7
commit bffe6cbbde

@ -31,11 +31,24 @@ class Job(object):
keyword arguments. keyword arguments.
""" """
job = Job() job = Job()
job.func = func job._func = func
job.args = args job._args = args
job.kwargs = kwargs job._kwargs = kwargs
job.description = job.get_call_string()
return job return job
@property
def func(self):
return self._func
@property
def args(self):
return self._args
@property
def kwargs(self):
return self._kwargs
@classmethod @classmethod
def fetch(cls, id): def fetch(cls, id):
"""Fetches a persisted job from its corresponding Redis key and """Fetches a persisted job from its corresponding Redis key and
@ -47,12 +60,14 @@ class Job(object):
def __init__(self, id=None): def __init__(self, id=None):
self._id = id self._id = id
self.func = None
self.args = None
self.kwargs = None
self.origin = None
self.created_at = times.now() self.created_at = times.now()
self._func = None
self._args = None
self._kwargs = None
self.description = None
self.origin = None
self.enqueued_at = None self.enqueued_at = None
self.ended_at = None
self.result = None self.result = None
self.exc_info = None self.exc_info = None
@ -121,13 +136,12 @@ class Job(object):
raise NoSuchJobError('No such job: %s' % (key,)) raise NoSuchJobError('No such job: %s' % (key,))
self.origin = conn.hget(key, 'origin') self.origin = conn.hget(key, 'origin')
self.func, self.args, self.kwargs = unpickle(pickled_data) self._func, self._args, self._kwargs = unpickle(data)
self.created_at = times.to_universal(conn.hget(key, 'created_at')) self.created_at = times.to_universal(conn.hget(key, 'created_at'))
def save(self): def save(self):
"""Persists the current job instance to its corresponding Redis key.""" """Persists the current job instance to its corresponding Redis key."""
pickled_data = dumps(self.job_tuple)
key = self.key key = self.key
conn.hset(key, 'data', pickled_data) conn.hset(key, 'data', pickled_data)
conn.hset(key, 'origin', self.origin) conn.hset(key, 'origin', self.origin)
@ -142,18 +156,20 @@ class Job(object):
# Representation # Representation
@property def get_call_string(self):
def call_string(self):
"""Returns a string representation of the call, formatted as a regular """Returns a string representation of the call, formatted as a regular
Python function invocation statement. Python function invocation statement.
""" """
arg_list = map(repr, self.args) if self.func is None:
arg_list += map(lambda tup: '%s=%r' % (tup[0], tup[1]), return None
self.kwargs.items())
return '%s(%s)' % (self.func.__name__, ', '.join(arg_list)) arg_list = [repr(arg) for arg in self.args]
arg_list += ['%s=%r' % (k, v) for k, v in self.kwargs.items()]
args = ', '.join(arg_list)
return '%s(%s)' % (self.func.__name__, args)
def __str__(self): def __str__(self):
return '<Job %s: %s>' % (self.id, self.call_string) return '<Job %s: %s>' % (self.id, self.description)
# Job equality # Job equality

@ -74,7 +74,7 @@ class Queue(object):
conn.rpush(self.key, job_id) conn.rpush(self.key, job_id)
def enqueue(self, f, *args, **kwargs): def enqueue(self, f, *args, **kwargs):
"""Enqueues a function call for delayed execution. """Creates a job to represent the delayed function call and enqueues it.
Expects the function to call, along with the arguments and keyword Expects the function to call, along with the arguments and keyword
arguments. arguments.

@ -319,7 +319,7 @@ class Worker(object):
job.func.__name__, job.func.__name__,
job.origin.name, time.time())) job.origin.name, time.time()))
msg = 'Got job %s from %s' % ( msg = 'Got job %s from %s' % (
job.call_string, job.description,
job.origin.name) job.origin.name)
self.log.info(msg) self.log.info(msg)
try: try:

Loading…
Cancel
Save