Skip to content

Commit

Permalink
Small refactor to simplify registry key setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jezdez committed Jan 25, 2017
1 parent b241d50 commit 27e4f3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
17 changes: 5 additions & 12 deletions rq/registry.py
Expand Up @@ -12,9 +12,11 @@ class BaseRegistry(object):
Each job is stored as a key in the registry, scored by expiration time
(unix timestamp).
"""
key_template = 'rq:registry:{0}'

def __init__(self, name='default', connection=None):
self.name = name
self.key = self.key_template.format(name)
self.connection = resolve_connection(connection)

def __len__(self):
Expand Down Expand Up @@ -66,10 +68,7 @@ class StartedJobRegistry(BaseRegistry):
Jobs are added to registry right before they are executed and removed
right after completion (success or failure).
"""

def __init__(self, name='default', connection=None):
super(StartedJobRegistry, self).__init__(name, connection)
self.key = 'rq:wip:{0}'.format(name)
key_template = 'rq:wip:{0}'

def cleanup(self, timestamp=None):
"""Remove expired jobs from registry and add them to FailedQueue.
Expand Down Expand Up @@ -105,10 +104,7 @@ class FinishedJobRegistry(BaseRegistry):
Registry of jobs that have been completed. Jobs are added to this
registry after they have successfully completed for monitoring purposes.
"""

def __init__(self, name='default', connection=None):
super(FinishedJobRegistry, self).__init__(name, connection)
self.key = 'rq:finished:{0}'.format(name)
key_template = 'rq:finished:{0}'

def cleanup(self, timestamp=None):
"""Remove expired jobs from registry.
Expand All @@ -125,10 +121,7 @@ class DeferredJobRegistry(BaseRegistry):
"""
Registry of deferred jobs (waiting for another job to finish).
"""

def __init__(self, name='default', connection=None):
super(DeferredJobRegistry, self).__init__(name, connection)
self.key = 'rq:deferred:{0}'.format(name)
key_template = 'rq:deferred:{0}'

def cleanup(self):
"""This method is only here to prevent errors because this method is
Expand Down
9 changes: 9 additions & 0 deletions tests/test_registry.py
Expand Up @@ -19,6 +19,9 @@ def setUp(self):
super(TestRegistry, self).setUp()
self.registry = StartedJobRegistry(connection=self.testconn)

def test_key(self):
self.assertEqual(self.registry.key, 'rq:wip:default')

def test_add_and_remove(self):
"""Adding and removing job to StartedJobRegistry."""
timestamp = current_timestamp()
Expand Down Expand Up @@ -129,6 +132,9 @@ def setUp(self):
super(TestFinishedJobRegistry, self).setUp()
self.registry = FinishedJobRegistry(connection=self.testconn)

def test_key(self):
self.assertEqual(self.registry.key, 'rq:finished:default')

def test_cleanup(self):
"""Finished job registry removes expired jobs."""
timestamp = current_timestamp()
Expand Down Expand Up @@ -165,6 +171,9 @@ def setUp(self):
super(TestDeferredRegistry, self).setUp()
self.registry = DeferredJobRegistry(connection=self.testconn)

def test_key(self):
self.assertEqual(self.registry.key, 'rq:deferred:default')

def test_add(self):
"""Adding a job to DeferredJobsRegistry."""
job = Job()
Expand Down

0 comments on commit 27e4f3a

Please sign in to comment.