From 160fe99323313ad10e368d868c834fcc680643d2 Mon Sep 17 00:00:00 2001 From: Theo Date: Fri, 8 Sep 2017 12:59:36 +0100 Subject: [PATCH 1/2] Fixed #731 - Support for deleting Queues --- rq/queue.py | 6 ++++++ tests/test_queue.py | 14 ++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/rq/queue.py b/rq/queue.py index 9d2357f6..dc961b84 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -111,6 +111,12 @@ class Queue(object): script = self.connection.register_script(script) return script(keys=[self.key]) + def delete(self): + """Deletes the queue and all the associated messages on the queue.""" + self.empty() + self.connection.srem(self.redis_queues_keys, self._key) + self.connection.delete(self._key) + def is_empty(self): """Returns whether the current queue is empty.""" return self.count == 0 diff --git a/tests/test_queue.py b/tests/test_queue.py index 7129452f..5a6aa204 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -72,6 +72,20 @@ class TestQueue(RQTestCase): self.testconn.rpush('rq:queue:example', 'sentinel message') self.assertEqual(q.is_empty(), False) + def test_queue_delete(self): + """Test queue.delete properly removes queue""" + q = Queue('example') + self.testconn.rpush('rq:queue:example', 'foo') + self.testconn.rpush('rq:queue:example', 'bar') + + self.assertEqual(2, len(q.get_job_ids())) + + q.delete() + + self.assertEqual(0, len(q.get_job_ids())) + self.assertEqual(0, len(self.testconn.smembers(Queue.redis_queues_keys))) + self.assertEqual(False, self.testconn.exists(q.key)) + def test_remove(self): """Ensure queue.remove properly removes Job from queue.""" q = Queue('example') From c095fe1825cc10744e6fbbd6acadc375bb70e90a Mon Sep 17 00:00:00 2001 From: Theo Date: Wed, 4 Oct 2017 11:29:33 +0100 Subject: [PATCH 2/2] Fixed #731 - Code review issues. Added delete_jobs parameter and pipelining. --- rq/queue.py | 14 +++++++++----- tests/test_queue.py | 22 ++++++++++++++++++++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/rq/queue.py b/rq/queue.py index dc961b84..e966de2d 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -111,11 +111,15 @@ class Queue(object): script = self.connection.register_script(script) return script(keys=[self.key]) - def delete(self): - """Deletes the queue and all the associated messages on the queue.""" - self.empty() - self.connection.srem(self.redis_queues_keys, self._key) - self.connection.delete(self._key) + def delete(self, delete_jobs=True): + """Deletes the queue. If delete_jobs is true it removes all the associated messages on the queue first.""" + if delete_jobs: + self.empty() + + with self.connection._pipeline() as pipeline: + pipeline.srem(self.redis_queues_keys, self._key) + pipeline.delete(self._key) + pipeline.execute() def is_empty(self): """Returns whether the current queue is empty.""" diff --git a/tests/test_queue.py b/tests/test_queue.py index 5a6aa204..5020a130 100644 --- a/tests/test_queue.py +++ b/tests/test_queue.py @@ -75,14 +75,32 @@ class TestQueue(RQTestCase): def test_queue_delete(self): """Test queue.delete properly removes queue""" q = Queue('example') - self.testconn.rpush('rq:queue:example', 'foo') - self.testconn.rpush('rq:queue:example', 'bar') + job = q.enqueue(say_hello) + job2 = q.enqueue(say_hello) self.assertEqual(2, len(q.get_job_ids())) q.delete() self.assertEqual(0, len(q.get_job_ids())) + self.assertEqual(False, self.testconn.exists(job.key)) + self.assertEqual(False, self.testconn.exists(job2.key)) + self.assertEqual(0, len(self.testconn.smembers(Queue.redis_queues_keys))) + self.assertEqual(False, self.testconn.exists(q.key)) + + def test_queue_delete_but_keep_jobs(self): + """Test queue.delete properly removes queue but keeps the job keys in the redis store""" + q = Queue('example') + job = q.enqueue(say_hello) + job2 = q.enqueue(say_hello) + + self.assertEqual(2, len(q.get_job_ids())) + + q.delete(delete_jobs=False) + + self.assertEqual(0, len(q.get_job_ids())) + self.assertEqual(True, self.testconn.exists(job.key)) + self.assertEqual(True, self.testconn.exists(job2.key)) self.assertEqual(0, len(self.testconn.smembers(Queue.redis_queues_keys))) self.assertEqual(False, self.testconn.exists(q.key))