From 10bda9684d8b1af534b7ea6b4d9dad482b67af66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=2E=20=C4=B0brahim=20G=C3=BCng=C3=B6r?= Date: Wed, 21 Aug 2013 17:51:14 +0300 Subject: [PATCH] Pass description parameter to job constructor in order to distinguish job names in queue.jobs or in rq-dashboard. Add related test case. --- rq/job.py | 4 ++-- rq/queue.py | 8 +++++--- tests/test_job.py | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/rq/job.py b/rq/job.py index 42653e0..fdc4b57 100644 --- a/rq/job.py +++ b/rq/job.py @@ -68,7 +68,7 @@ class Job(object): # Job construction @classmethod def create(cls, func, args=None, kwargs=None, connection=None, - result_ttl=None, status=None): + result_ttl=None, status=None, description=None): """Creates a new Job instance for the given function, arguments, and keyword arguments. """ @@ -88,7 +88,7 @@ class Job(object): job._func_name = func job._args = args job._kwargs = kwargs - job.description = job.get_call_string() + job.description = description or job.get_call_string() job.result_ttl = result_ttl job._status = status return job diff --git a/rq/queue.py b/rq/queue.py index 3f9cd96..427b665 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -129,7 +129,7 @@ class Queue(object): """Pushes a job ID on the corresponding Redis queue.""" self.connection.rpush(self.key, job_id) - def enqueue_call(self, func, args=None, kwargs=None, timeout=None, result_ttl=None): # noqa + def enqueue_call(self, func, args=None, kwargs=None, description=None, timeout=None, result_ttl=None): # noqa """Creates a job to represent the delayed function call and enqueues it. @@ -138,7 +138,7 @@ class Queue(object): contain options for RQ itself. """ timeout = timeout or self._default_timeout - job = Job.create(func, args, kwargs, connection=self.connection, + job = Job.create(func, args, kwargs, description=description, connection=self.connection, result_ttl=result_ttl, status=Status.QUEUED) return self.enqueue_job(job, timeout=timeout) @@ -164,15 +164,17 @@ class Queue(object): # Detect explicit invocations, i.e. of the form: # q.enqueue(foo, args=(1, 2), kwargs={'a': 1}, timeout=30) timeout = None + description=None result_ttl = None if 'args' in kwargs or 'kwargs' in kwargs: assert args == (), 'Extra positional arguments cannot be used when using explicit args and kwargs.' # noqa timeout = kwargs.pop('timeout', None) + description = kwargs.pop('description', None) args = kwargs.pop('args', None) result_ttl = kwargs.pop('result_ttl', None) kwargs = kwargs.pop('kwargs', None) - return self.enqueue_call(func=f, args=args, kwargs=kwargs, + return self.enqueue_call(func=f, args=args, kwargs=kwargs, description=description, timeout=timeout, result_ttl=result_ttl) def enqueue_job(self, job, timeout=None, set_meta_data=True): diff --git a/tests/test_job.py b/tests/test_job.py index 0f376ec..899fbce 100644 --- a/tests/test_job.py +++ b/tests/test_job.py @@ -203,6 +203,20 @@ class TestJob(RQTestCase): job_from_queue = Job.fetch(job.id, connection=self.testconn) self.assertEqual(job.result_ttl, None) + def test_description_is_persisted(self): + """Ensure that job's custom description is set properly""" + description = 'Say hello!' + job = Job.create(func=say_hello, args=('Lionel',), description=description) + job.save() + job_from_queue = Job.fetch(job.id, connection=self.testconn) + self.assertEqual(job.description, description) + + # Ensure job description is constructed from function call string + job = Job.create(func=say_hello, args=('Lionel',)) + job.save() + job_from_queue = Job.fetch(job.id, connection=self.testconn) + self.assertEqual(job.description, job.get_call_string()) + def test_job_access_within_job_function(self): """The current job is accessible within the job function.""" # Executing the job function from outside of RQ throws an exception