Register queue also for deferred job (#2108)

* Add test to list queue with only deferred jobs

Signed-off-by: Simó Albert i Beltran <sim6@probeta.net>

* 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 <sim6@probeta.net>

---------

Signed-off-by: Simó Albert i Beltran <sim6@probeta.net>
This commit is contained in:
Simó Albert i Beltran 2024-07-18 04:42:21 +02:00 committed by GitHub
parent 180c9afba0
commit df5e99ba0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 2 deletions

View File

@ -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)

View File

@ -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)