From c639018fb9572cc1b9fc1cd371d53853df2afdc8 Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Sat, 21 Apr 2018 19:39:59 +0700 Subject: [PATCH] Registry objects can be instantiated by passing a queue object. --- rq/registry.py | 15 +++++++++++---- tests/test_registry.py | 11 +++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/rq/registry.py b/rq/registry.py index 51e6dd0..74039fd 100644 --- a/rq/registry.py +++ b/rq/registry.py @@ -15,12 +15,19 @@ class BaseRegistry(object): job_class = Job key_template = 'rq:registry:{0}' - def __init__(self, name='default', connection=None, job_class=None): - self.name = name - self.key = self.key_template.format(name) - self.connection = resolve_connection(connection) + def __init__(self, name='default', connection=None, job_class=None, + queue=None): + if queue: + self.name = queue.name + self.connection = resolve_connection(queue.connection) + else: + self.name = name + self.connection = resolve_connection(connection) + + self.key = self.key_template.format(self.name) self.job_class = backend_class(self, 'job_class', override=job_class) + def __len__(self): """Returns the number of jobs in this registry""" return self.count diff --git a/tests/test_registry.py b/tests/test_registry.py index 3b2885d..295d284 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -23,6 +23,17 @@ class TestRegistry(RQTestCase): super(TestRegistry, self).setUp() self.registry = StartedJobRegistry(connection=self.testconn) + def test_init(self): + """Registry can be instantiated with queue or name/Redis connection""" + queue = Queue('foo', connection=self.testconn) + registry = StartedJobRegistry(queue=queue) + self.assertEqual(registry.name, queue.name) + self.assertEqual(registry.connection, queue.connection) + + registry = StartedJobRegistry('bar', self.testconn) + self.assertEqual(registry.name, 'bar') + self.assertEqual(registry.connection, self.testconn) + def test_key(self): self.assertEqual(self.registry.key, 'rq:wip:default')