From 3467868f1f15392970d2d90d15021c7f6fb57e1c Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Tue, 11 Nov 2014 15:09:06 -0500 Subject: [PATCH 1/4] allow depends_on to be a job id or a job itself Other parts of the code (i.e.: the `.create()` method) allow the `depends_on` kwarg to be a `Job` object *or* a job id. This is an attempt to allow that same idea within the `.enqueue_call()` method for a queue. Since this part of the code requires actually knowing the precise redis key for the job that's depended on, my intuition is that a `.fetch()` is the only way to solve this. --- rq/queue.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rq/queue.py b/rq/queue.py index 1694268..ff40e03 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -189,6 +189,8 @@ class Queue(object): # If WatchError is raised in the process, that means something else is # modifying the dependency. In this case we simply retry if depends_on is not None: + if not isinstance(depends_on, Job): + depends_on = Job.fetch(id=depends_on, connection=self.connection) with self.connection.pipeline() as pipe: while True: try: From 14d118624156bfc625f07153a24818a073dcdae7 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Mon, 17 Nov 2014 15:04:20 -0500 Subject: [PATCH 2/4] use internal job_class for check --- rq/queue.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rq/queue.py b/rq/queue.py index ff40e03..ff5f860 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -189,7 +189,7 @@ class Queue(object): # If WatchError is raised in the process, that means something else is # modifying the dependency. In this case we simply retry if depends_on is not None: - if not isinstance(depends_on, Job): + if not isinstance(depends_on, self.job_class): depends_on = Job.fetch(id=depends_on, connection=self.connection) with self.connection.pipeline() as pipe: while True: From 629b3929244d12bdd5b54ede5caf6a22f9718620 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Mon, 17 Nov 2014 15:04:58 -0500 Subject: [PATCH 3/4] add PyCharm .idea folder to .gitignore --- .gitignore | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index ff9c616..25a046e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,7 @@ /build .tox .vagrant -Vagrantfile \ No newline at end of file +Vagrantfile + +# PyCharm +.idea From 82729c98dcb278c56ead02f5e318210228e3cc02 Mon Sep 17 00:00:00 2001 From: Travis Johnson Date: Mon, 17 Nov 2014 15:22:48 -0500 Subject: [PATCH 4/4] test for id based job dependancies --- tests/test_queue.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/test_queue.py b/tests/test_queue.py index e61568e..8990e35 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -352,6 +352,23 @@ class TestQueue(RQTestCase): self.assertEqual(q.job_ids, [job.id]) self.assertEqual(job.timeout, Queue.DEFAULT_TIMEOUT) + def test_enqueue_job_with_dependency_by_id(self): + """Enqueueing jobs should work as expected by id as well as job-objects.""" + parent_job = Job.create(func=say_hello) + # We need to save the job for the ID to exist in redis + parent_job.save() + + q = Queue() + q.enqueue_call(say_hello, depends_on=parent_job.id) + self.assertEqual(q.job_ids, []) + + # Jobs dependent on finished jobs are immediately enqueued + parent_job.set_status(Status.FINISHED) + parent_job.save() + job = q.enqueue_call(say_hello, depends_on=parent_job.id) + self.assertEqual(q.job_ids, [job.id]) + self.assertEqual(job.timeout, Queue.DEFAULT_TIMEOUT) + def test_enqueue_job_with_dependency_and_timeout(self): """Jobs still know their specified timeout after being scheduled as a dependency.""" # Job with unfinished dependency is not immediately enqueued