From faf9d3e66838a71e5b2e28add0a503e3fdc4fa03 Mon Sep 17 00:00:00 2001 From: Selwin Ong Date: Sat, 23 May 2015 08:46:00 +0700 Subject: [PATCH] Added clean_registries(queue) function to clean job registries related to that queue. --- rq/registry.py | 13 +++++++++++-- tests/test_registry.py | 19 +++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/rq/registry.py b/rq/registry.py index 1180d4ff..ded86da8 100644 --- a/rq/registry.py +++ b/rq/registry.py @@ -8,8 +8,9 @@ from .utils import current_timestamp 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): @@ -134,3 +135,11 @@ class DeferredJobRegistry(BaseRegistry): 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() diff --git a/tests/test_registry.py b/tests/test_registry.py index 628636ad..9bb18562 100644 --- a/tests/test_registry.py +++ b/tests/test_registry.py @@ -6,8 +6,8 @@ from rq.job import Job, JobStatus 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 @@ -107,6 +107,21 @@ class TestRegistry(RQTestCase): 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):