diff --git a/django_q/brokers/__init__.py b/django_q/brokers/__init__.py index 90c8d4b..626717c 100644 --- a/django_q/brokers/__init__.py +++ b/django_q/brokers/__init__.py @@ -7,6 +7,7 @@ class Broker(object): self.connection = self.get_connection(list_key) self.list_key = list_key self.cache = self.get_cache() + self.task_cache = [] def enqueue(self, task): """ diff --git a/django_q/brokers/ironmq.py b/django_q/brokers/ironmq.py index 6388bf6..d8b92ec 100644 --- a/django_q/brokers/ironmq.py +++ b/django_q/brokers/ironmq.py @@ -1,18 +1,25 @@ -from django_q.conf import Conf +from django_q.conf import Conf, logger from django_q.brokers import Broker from iron_mq import IronMQ class IronMQBroker(Broker): - def enqueue(self, task): return self.connection.post(task)['ids'][0] def dequeue(self): - timeout = Conf.RETRY or None - task = self.connection.get(timeout=timeout, wait=1)['messages'] - if task: - return task[0]['id'], task[0]['body'] + t = None + if len(self.task_cache) > 0: + t = self.task_cache.pop() + else: + timeout = Conf.RETRY or None + tasks = self.connection.get(timeout=timeout, wait=1, max=Conf.IRON_MQ_MAX)['messages'] + if tasks: + t = tasks.pop() + if tasks: + self.task_cache = tasks + if t: + return t['id'], t['body'] def ping(self): return self.connection.name == self.list_key @@ -41,4 +48,4 @@ class IronMQBroker(Broker): @staticmethod def get_connection(list_key=Conf.PREFIX): ironmq = IronMQ(name=None, **Conf.IRON_MQ) - return ironmq.queue(queue_name=list_key) \ No newline at end of file + return ironmq.queue(queue_name=list_key) diff --git a/django_q/conf.py b/django_q/conf.py index 6893702..6128875 100644 --- a/django_q/conf.py +++ b/django_q/conf.py @@ -39,6 +39,7 @@ class Conf(object): # IronMQ broker IRON_MQ = conf.get('iron_mq', None) + IRON_MQ_MAX = conf.get('iron_mq_max', 1) # Name of the cluster or site. For when you run multiple sites on one redis server PREFIX = conf.get('name', 'default') diff --git a/django_q/tests/test_brokers.py b/django_q/tests/test_brokers.py index 09ce743..d1d8d7e 100644 --- a/django_q/tests/test_brokers.py +++ b/django_q/tests/test_brokers.py @@ -129,3 +129,4 @@ def test_ironmq(): assert broker.queue_size() == 0 # back to django-redis Conf.IRON_MQ = None + Conf.DJANGO_REDIS = 'default'