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 diff --git a/rq/queue.py b/rq/queue.py index 1694268..ff5f860 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, self.job_class): + depends_on = Job.fetch(id=depends_on, connection=self.connection) with self.connection.pipeline() as pipe: while True: try: 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