Small refactor to simplify registry key setup.

main
Jannis Leidel 8 years ago
parent b241d50c10
commit 27e4f3a768
No known key found for this signature in database
GPG Key ID: C795956FB489DCA9

@ -12,9 +12,11 @@ class BaseRegistry(object):
Each job is stored as a key in the registry, scored by expiration time Each job is stored as a key in the registry, scored by expiration time
(unix timestamp). (unix timestamp).
""" """
key_template = 'rq:registry:{0}'
def __init__(self, name='default', connection=None): def __init__(self, name='default', connection=None):
self.name = name self.name = name
self.key = self.key_template.format(name)
self.connection = resolve_connection(connection) self.connection = resolve_connection(connection)
def __len__(self): def __len__(self):
@ -66,10 +68,7 @@ class StartedJobRegistry(BaseRegistry):
Jobs are added to registry right before they are executed and removed Jobs are added to registry right before they are executed and removed
right after completion (success or failure). right after completion (success or failure).
""" """
key_template = 'rq:wip:{0}'
def __init__(self, name='default', connection=None):
super(StartedJobRegistry, self).__init__(name, connection)
self.key = 'rq:wip:{0}'.format(name)
def cleanup(self, timestamp=None): def cleanup(self, timestamp=None):
"""Remove expired jobs from registry and add them to FailedQueue. """Remove expired jobs from registry and add them to FailedQueue.
@ -105,10 +104,7 @@ class FinishedJobRegistry(BaseRegistry):
Registry of jobs that have been completed. Jobs are added to this Registry of jobs that have been completed. Jobs are added to this
registry after they have successfully completed for monitoring purposes. registry after they have successfully completed for monitoring purposes.
""" """
key_template = 'rq:finished:{0}'
def __init__(self, name='default', connection=None):
super(FinishedJobRegistry, self).__init__(name, connection)
self.key = 'rq:finished:{0}'.format(name)
def cleanup(self, timestamp=None): def cleanup(self, timestamp=None):
"""Remove expired jobs from registry. """Remove expired jobs from registry.
@ -125,10 +121,7 @@ class DeferredJobRegistry(BaseRegistry):
""" """
Registry of deferred jobs (waiting for another job to finish). Registry of deferred jobs (waiting for another job to finish).
""" """
key_template = 'rq:deferred:{0}'
def __init__(self, name='default', connection=None):
super(DeferredJobRegistry, self).__init__(name, connection)
self.key = 'rq:deferred:{0}'.format(name)
def cleanup(self): def cleanup(self):
"""This method is only here to prevent errors because this method is """This method is only here to prevent errors because this method is

@ -19,6 +19,9 @@ class TestRegistry(RQTestCase):
super(TestRegistry, self).setUp() super(TestRegistry, self).setUp()
self.registry = StartedJobRegistry(connection=self.testconn) self.registry = StartedJobRegistry(connection=self.testconn)
def test_key(self):
self.assertEqual(self.registry.key, 'rq:wip:default')
def test_add_and_remove(self): def test_add_and_remove(self):
"""Adding and removing job to StartedJobRegistry.""" """Adding and removing job to StartedJobRegistry."""
timestamp = current_timestamp() timestamp = current_timestamp()
@ -129,6 +132,9 @@ class TestFinishedJobRegistry(RQTestCase):
super(TestFinishedJobRegistry, self).setUp() super(TestFinishedJobRegistry, self).setUp()
self.registry = FinishedJobRegistry(connection=self.testconn) self.registry = FinishedJobRegistry(connection=self.testconn)
def test_key(self):
self.assertEqual(self.registry.key, 'rq:finished:default')
def test_cleanup(self): def test_cleanup(self):
"""Finished job registry removes expired jobs.""" """Finished job registry removes expired jobs."""
timestamp = current_timestamp() timestamp = current_timestamp()
@ -165,6 +171,9 @@ class TestDeferredRegistry(RQTestCase):
super(TestDeferredRegistry, self).setUp() super(TestDeferredRegistry, self).setUp()
self.registry = DeferredJobRegistry(connection=self.testconn) self.registry = DeferredJobRegistry(connection=self.testconn)
def test_key(self):
self.assertEqual(self.registry.key, 'rq:deferred:default')
def test_add(self): def test_add(self):
"""Adding a job to DeferredJobsRegistry.""" """Adding a job to DeferredJobsRegistry."""
job = Job() job = Job()

Loading…
Cancel
Save