class Queue(object): """ A custom Queue API for our workers so that we can plug an play with multiple Queue implementations. """ def __init__(self, name): if isinstance(name, basestring) and name.strip(): self.q = RetaskQueue(name) self.q.connect() self.name = name else: raise QueueException( "Bad value for 'name'. 'name' has to be a non empty string.") def get(self, count=1, wait=False): """ Get task from Queue. Args: wait: A boolean deciding whether to retrieve a task from queue in a blocking fashion or not. Returns: A list of retask.Task instances. """ tasks = [] for n in range(count or 1): if wait: task = self.q.wait() else: task = self.q.dequeue() tasks.append(task) return tasks def put(self, data): """ Create a task from data and put it to Queue. Args: data: A dictionary. """ task = Task(data) job = self.q.enqueue(task) return job def send(self, task, data): """ Update result for a task and send it to the producer. Args: task: A retask.Task instance data: A string. """ self.q.send(task, data) def __unicode__(self): return self.name
from retask import Task from retask import Queue import time queue = Queue('example') queue.connect() task = queue.wait() print(task.data) time.sleep(15) queue.send(task, "We received your information dear %s" % task.data['user'])
def runTest(self): queue = Queue('testqueue') queue.connect() task = queue.dequeue() i = task.data self.assertEqual(task.data['name'], 'kushal')
def setUp(self): queue = Queue('testqueue') queue.connect() t = Task({'name': 'kushal'}) queue.enqueue(t)
def runTest(self): queue = Queue('testqueue') queue.connect() t = Task({'name': 'kushal'}) self.assertTrue(queue.enqueue(t))
def runTest(self, mock_redis): m = mock_redis.return_value m.llen.return_value = 2 queue = Queue('testqueue') queue.connect() self.assertEqual(queue.length, 2)
def runTest(self): queue = Queue('testqueue') self.assertTrue(queue.connect())
def setUp(self): queue = Queue('testqueue') queue.connect() t = Task({'name':'kushal'}) queue.enqueue(t)
def runTest(self): queue = Queue('testqueue') queue.connect() t = Task({'name':'kushal'}) self.assertTrue(queue.enqueue(t))
def runTest(self): queue = Queue('lambda') queue.connect() results = queue.names() self.assertEqual(results[0], 'retaskqueue-lambda')