From ed59b9248aeb87c566661d0f5ee2c2e934c41440 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=B3=20Albert=20i=20Beltran?= Date: Tue, 28 Feb 2023 00:03:47 +0100 Subject: [PATCH] fix: Dependency list in depends_on (#1843) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test: Dependency list in depends_on Signed-off-by: Simó Albert i Beltran * fix: Dependency list in depends_on Signed-off-by: Simó Albert i Beltran --------- Signed-off-by: Simó Albert i Beltran --- rq/job.py | 17 +++++++++++------ tests/test_dependencies.py | 13 +++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/rq/job.py b/rq/job.py index 81ed98e..0dcbf28 100644 --- a/rq/job.py +++ b/rq/job.py @@ -254,12 +254,17 @@ class Job: # dependency could be job instance or id, or iterable thereof if depends_on is not None: - if isinstance(depends_on, Dependency): - job.enqueue_at_front = depends_on.enqueue_at_front - job.allow_dependency_failures = depends_on.allow_failure - depends_on_list = depends_on.dependencies - else: - depends_on_list = ensure_list(depends_on) + depends_on = ensure_list(depends_on) + depends_on_list = [] + for depends_on_item in depends_on: + if isinstance(depends_on_item, Dependency): + # If a Dependency has enqueue_at_front or allow_failure set to True, these behaviors are used for + # all dependencies. + job.enqueue_at_front = job.enqueue_at_front or depends_on_item.enqueue_at_front + job.allow_dependency_failures = job.allow_dependency_failures or depends_on_item.allow_failure + depends_on_list.extend(depends_on_item.dependencies) + else: + depends_on_list.extend(ensure_list(depends_on_item)) job._dependency_ids = [dep.id if isinstance(dep, Job) else dep for dep in depends_on_list] return job diff --git a/tests/test_dependencies.py b/tests/test_dependencies.py index 26b115d..980e805 100644 --- a/tests/test_dependencies.py +++ b/tests/test_dependencies.py @@ -117,6 +117,19 @@ class TestDependencies(RQTestCase): self.assertEqual(q.job_ids, ["fake_job_id_2", "fake_job_id_1"]) + def test_dependency_list_in_depends_on(self): + """Enqueue with Dependency list in depends_on""" + q = Queue(connection=self.testconn) + w = SimpleWorker([q], connection=q.connection) + + # enqueue dependent job when parent successfully finishes + parent_job1 = q.enqueue(say_hello) + parent_job2 = q.enqueue(say_hello) + job = q.enqueue_call(say_hello, depends_on=[Dependency([parent_job1]), Dependency([parent_job2])]) + w.work(burst=True) + self.assertEqual(job.get_status(), JobStatus.FINISHED) + + def test_dependencies_are_met_if_parent_is_canceled(self): """When parent job is canceled, it should be treated as failed""" queue = Queue(connection=self.testconn)