From df5e99ba0b3274ab734cde612c270a788ee47ffc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sim=C3=B3=20Albert=20i=20Beltran?= Date: Thu, 18 Jul 2024 04:42:21 +0200 Subject: [PATCH] Register queue also for deferred job (#2108) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add test to list queue with only deferred jobs Signed-off-by: Simó Albert i Beltran * Register queue also for deferred job Without this change queues initialized with only deferred jobs are not listed by `rq.Queue.all()`. Signed-off-by: Simó Albert i Beltran --------- Signed-off-by: Simó Albert i Beltran --- rq/queue.py | 7 +++++-- tests/test_queue.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/rq/queue.py b/rq/queue.py index 277dcef1..96851d03 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -1096,6 +1096,11 @@ class Queue: """ job.origin = self.name job = self.setup_dependencies(job, pipeline=pipeline) + # Add Queue key set + pipe = pipeline if pipeline is not None else self.connection.pipeline() + pipe.sadd(self.redis_queues_keys, self.key) + if pipeline is None: + pipe.execute() # If we do not depend on an unfinished job, enqueue the job. if job.get_status(refresh=False) != JobStatus.DEFERRED: return self._enqueue_job(job, pipeline=pipeline, at_front=at_front) @@ -1116,8 +1121,6 @@ class Queue: """ pipe = pipeline if pipeline is not None else self.connection.pipeline() - # Add Queue key set - pipe.sadd(self.redis_queues_keys, self.key) job.redis_server_version = self.get_redis_server_version() job.set_status(JobStatus.QUEUED, pipeline=pipe) diff --git a/tests/test_queue.py b/tests/test_queue.py index 46f597e3..57a21008 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -458,6 +458,21 @@ class TestQueue(RQTestCase): self.assertEqual(len(queues), 1) self.assertIs(queues[0].job_class, CustomJob) + def test_all_queues_with_only_deferred_jobs(self): + """All queues with only deferred jobs""" + queue_with_queued_jobs = Queue('queue_with_queued_jobs', connection=self.connection) + queue_with_deferred_jobs = Queue('queue_with_deferred_jobs', connection=self.connection) + + parent_job = queue_with_queued_jobs.enqueue(say_hello) + queue_with_deferred_jobs.enqueue(say_hello, depends_on=parent_job) + + # Ensure all queues are listed + self.assertEqual(len(Queue.all(connection=self.connection)), 2) + names = [q.name for q in Queue.all(connection=self.connection)] + # Verify names + self.assertTrue('queue_with_queued_jobs' in names) + self.assertTrue('queue_with_deferred_jobs' in names) + def test_from_queue_key(self): """Ensure being able to get a Queue instance manually from Redis""" q = Queue(connection=self.connection)