From 196a9815d2e70485e5666372e672a271f618fdb1 Mon Sep 17 00:00:00 2001 From: Vincent Driessen Date: Tue, 15 Nov 2011 09:36:29 +0100 Subject: [PATCH] Add dequeue method to Queue. --- rq/job.py | 1 - rq/queue.py | 5 +++++ tests/test_rq.py | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/rq/job.py b/rq/job.py index c073ced..9de638e 100644 --- a/rq/job.py +++ b/rq/job.py @@ -48,4 +48,3 @@ class job(object): return f.enqueue(self.queue, *args, **kwargs) f.delay = delay return f - diff --git a/rq/queue.py b/rq/queue.py index d183805..0ad9270 100644 --- a/rq/queue.py +++ b/rq/queue.py @@ -1,3 +1,4 @@ +from pickle import loads from .proxy import conn def to_queue_key(queue_name): @@ -30,5 +31,9 @@ class Queue(object): def enqueue(self, job): return job.enqueue(self) + def dequeue(self): + s = conn.lpop(self.key) + return loads(s) + def __str__(self): return self.name diff --git a/tests/test_rq.py b/tests/test_rq.py index 44615ab..bd7ba42 100644 --- a/tests/test_rq.py +++ b/tests/test_rq.py @@ -99,6 +99,15 @@ class TestQueue(RQTestCase): self.assertEquals(q.empty, False) self.assertQueueContains(q, testjob) + def test_dequeue(self): + """Fetching work from specific queue.""" + q = Queue('foo') + testjob.enqueue(q, 'Rick') + + # Pull it off the queue (normally, a worker would do this) + f, rv_key, args, kwargs = q.dequeue() + self.assertEquals(f, testjob) + self.assertEquals(args[0], 'Rick') class TestJob(RQTestCase): def test_job_methods(self):