Skip to content

Commit

Permalink
Added clean_registries(queue) function to clean job registries relate…
Browse files Browse the repository at this point in the history
…d to that queue.
  • Loading branch information
selwin committed May 23, 2015
1 parent d51f020 commit faf9d3e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
13 changes: 11 additions & 2 deletions rq/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

class BaseRegistry(object):
"""
Base implementation of job registry, implemented in Redis sorted set. Each job
is stored as a key in the registry, scored by expiration time (unix timestamp).
Base implementation of a job registry, implemented in Redis sorted set.
Each job is stored as a key in the registry, scored by expiration time
(unix timestamp).
"""

def __init__(self, name='default', connection=None):
Expand Down Expand Up @@ -134,3 +135,11 @@ def cleanup(self):
automatically called by `count()` and `get_job_ids()` methods
implemented in BaseRegistry."""
pass


def clean_registries(queue):
"""Cleans StartedJobRegistry and FinishedJobRegistry of a queue."""
registry = FinishedJobRegistry(name=queue.name, connection=queue.connection)
registry.cleanup()
registry = StartedJobRegistry(name=queue.name, connection=queue.connection)
registry.cleanup()
19 changes: 17 additions & 2 deletions tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from rq.queue import FailedQueue, Queue
from rq.utils import current_timestamp
from rq.worker import Worker
from rq.registry import (DeferredJobRegistry, FinishedJobRegistry,
StartedJobRegistry)
from rq.registry import (clean_registries, DeferredJobRegistry,
FinishedJobRegistry, StartedJobRegistry)

from tests import RQTestCase
from tests.fixtures import div_by_zero, say_hello
Expand Down Expand Up @@ -107,6 +107,21 @@ def test_get_job_count(self):
self.assertEqual(self.registry.count, 2)
self.assertEqual(len(self.registry), 2)

def test_clean_registries(self):
"""clean_registries() cleans Started and Finished job registries."""

queue = Queue(connection=self.testconn)

finished_job_registry = FinishedJobRegistry(connection=self.testconn)
self.testconn.zadd(finished_job_registry.key, 1, 'foo')

started_job_registry = StartedJobRegistry(connection=self.testconn)
self.testconn.zadd(started_job_registry.key, 1, 'foo')

clean_registries(queue)
self.assertEqual(self.testconn.zcard(finished_job_registry.key), 0)
self.assertEqual(self.testconn.zcard(started_job_registry.key), 0)


class TestFinishedJobRegistry(RQTestCase):

Expand Down

0 comments on commit faf9d3e

Please sign in to comment.