Only save queue to set on enqueue.

Validate queue key still exists when all() is called
main
Rob Harrigan 11 years ago
parent c08b96e027
commit 5e4d5f642e

@ -30,7 +30,7 @@ class Queue(object):
def to_queue(queue_key): def to_queue(queue_key):
return cls.from_queue_key(as_text(queue_key), return cls.from_queue_key(as_text(queue_key),
connection=connection) connection=connection)
return list(map(to_queue, connection.smembers(cls.redis_queues_keys))) return [to_queue(rq_key) for rq_key in connection.smembers(cls.redis_queues_keys) if rq_key]
@classmethod @classmethod
def from_queue_key(cls, queue_key, connection=None): def from_queue_key(cls, queue_key, connection=None):
@ -52,7 +52,6 @@ class Queue(object):
self._key = '%s%s' % (prefix, name) self._key = '%s%s' % (prefix, name)
self._default_timeout = default_timeout self._default_timeout = default_timeout
self._async = async self._async = async
self.connection.sadd(self.redis_queues_keys, self.key)
@property @property
def key(self): def key(self):
@ -190,7 +189,10 @@ class Queue(object):
the properties `origin` and `enqueued_at`. the properties `origin` and `enqueued_at`.
If Queue is instantiated with async=False, job is executed immediately. If Queue is instantiated with async=False, job is executed immediately.
""" """
# Add Queue key set
self.connection.sadd(self.redis_queues_keys, self.key)
if set_meta_data: if set_meta_data:
job.origin = self.name job.origin = self.name
job.enqueued_at = times.now() job.enqueued_at = times.now()

@ -256,13 +256,27 @@ class TestQueue(RQTestCase):
q = Queue('first-queue') q = Queue('first-queue')
r = Queue('second-queue') r = Queue('second-queue')
s = Queue('third-queue') s = Queue('third-queue')
all = Queue.all()
self.assertEquals(len(all), 3) # Ensure a queue is added only once a job is enqueued
names = [q.name for q in all] self.assertEquals(len(Queue.all()), 0)
q.enqueue(say_hello)
self.assertEquals(len(Queue.all()), 1)
# Ensure this holds true for multiple queues
r.enqueue(say_hello)
s.enqueue(say_hello)
names = [q.name for q in Queue.all()]
self.assertEquals(len(Queue.all()), 3)
# Verify names
self.assertTrue('first-queue' in names) self.assertTrue('first-queue' in names)
self.assertTrue('second-queue' in names) self.assertTrue('second-queue' in names)
self.assertTrue('third-queue' in names) self.assertTrue('third-queue' in names)
# Ensure we no longer return queues whose keys do not exist
self.testconn.srem(Queue.redis_queues_keys, s.key)
self.assertEquals(len(Queue.all()), 2)
class TestFailedQueue(RQTestCase): class TestFailedQueue(RQTestCase):
def test_requeue_job(self): def test_requeue_job(self):
"""Requeueing existing jobs.""" """Requeueing existing jobs."""

Loading…
Cancel
Save