mirror of
https://github.com/django-q2/django-q2.git
synced 2026-10-09 08:08:11 +08:00
Adds more brokers
* added IronMQ * added Aws SQS * removed SafeRedis. During tests the 'safe' part wasn't consistent enough
This commit is contained in:
@@ -3,30 +3,74 @@ from django.core.cache import caches, InvalidCacheBackendError
|
||||
|
||||
|
||||
class Broker(object):
|
||||
def __init__(self, list_key=Conf.Q_LIST):
|
||||
self.connection = self.get_connection()
|
||||
def __init__(self, list_key=Conf.PREFIX):
|
||||
self.connection = self.get_connection(list_key)
|
||||
self.list_key = list_key
|
||||
self.cache = self.get_cache()
|
||||
|
||||
def enqueue(self, task):
|
||||
"""
|
||||
Puts a task onto the queue
|
||||
:type task: str
|
||||
:return: task id
|
||||
"""
|
||||
pass
|
||||
|
||||
def dequeue(self):
|
||||
"""
|
||||
Gets a task from the queue
|
||||
:return: tuple with task id and task message
|
||||
"""
|
||||
pass
|
||||
|
||||
def queue_size(self):
|
||||
"""
|
||||
:return: the amount of tasks in the queue
|
||||
"""
|
||||
pass
|
||||
|
||||
def delete_queue(self, list_key=None):
|
||||
def delete_queue(self):
|
||||
"""
|
||||
Deletes the queue from the broker
|
||||
"""
|
||||
pass
|
||||
|
||||
def acknowledge(self, ack_id):
|
||||
def purge_queue(self):
|
||||
"""
|
||||
Purges the queue of any tasks
|
||||
"""
|
||||
pass
|
||||
|
||||
def delete(self, task_id):
|
||||
"""
|
||||
Deletes a task from the queue
|
||||
:param task_id: the id of the task
|
||||
"""
|
||||
pass
|
||||
|
||||
def acknowledge(self, task_id):
|
||||
"""
|
||||
Acknowledges completion of the task and removes it from the queue.
|
||||
:param task_id: the id of the task
|
||||
"""
|
||||
pass
|
||||
|
||||
def ping(self):
|
||||
"""
|
||||
Checks whether the broker connection is available
|
||||
:rtype: bool
|
||||
"""
|
||||
pass
|
||||
|
||||
def set_stat(self, key, value, timeout):
|
||||
"""
|
||||
Saves a cluster statistic to the cache provider
|
||||
:type key: str
|
||||
:type value: str
|
||||
:type timeout: int
|
||||
"""
|
||||
if not self.cache:
|
||||
return
|
||||
key_list = self.cache.get(Conf.Q_STAT, [])
|
||||
if key not in key_list:
|
||||
key_list.append(key)
|
||||
@@ -34,9 +78,23 @@ class Broker(object):
|
||||
return self.cache.set(key, value, timeout)
|
||||
|
||||
def get_stat(self, key):
|
||||
"""
|
||||
Gets a cluster statistic from the cache provider
|
||||
:type key: str
|
||||
:return: a cluster Stat
|
||||
"""
|
||||
if not self.cache:
|
||||
return
|
||||
return self.cache.get(key)
|
||||
|
||||
def get_stats(self, pattern):
|
||||
"""
|
||||
Returns a list of all cluster stats from the cache provider
|
||||
:type pattern: str
|
||||
:return: a list of Stats
|
||||
"""
|
||||
if not self.cache:
|
||||
return
|
||||
key_list = self.cache.get(Conf.Q_STAT)
|
||||
if not key_list or len(key_list) == 0:
|
||||
return []
|
||||
@@ -52,23 +110,41 @@ class Broker(object):
|
||||
|
||||
@staticmethod
|
||||
def get_cache():
|
||||
"""
|
||||
Gets the current cache provider
|
||||
:return: a cache provider
|
||||
"""
|
||||
try:
|
||||
return caches[Conf.CACHE]
|
||||
except InvalidCacheBackendError:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def get_connection():
|
||||
def get_connection(list_key=Conf.PREFIX):
|
||||
"""
|
||||
Gets a connection to the broker
|
||||
:param list_key: Optional queue name
|
||||
:return: a broker connection
|
||||
"""
|
||||
return 0
|
||||
|
||||
|
||||
def get_broker(list_key=Conf.Q_LIST):
|
||||
if Conf.DJANGO_REDIS:
|
||||
from brokers import djangoredis
|
||||
return djangoredis.DjangoRedis(list_key=list_key)
|
||||
def get_broker(list_key=Conf.PREFIX):
|
||||
"""
|
||||
Gets the configured broker type
|
||||
:param list_key: optional queue name
|
||||
:type list_key: str
|
||||
:return:
|
||||
"""
|
||||
if Conf.IRONMQ:
|
||||
from brokers import iron_mq
|
||||
return iron_mq.IronMQBroker(list_key=list_key)
|
||||
elif Conf.DISQUE:
|
||||
from brokers import disque
|
||||
return disque.Disque(list_key=list_key)
|
||||
elif Conf.SQS:
|
||||
from brokers import aws_sqs
|
||||
return aws_sqs.Sqs(list_key=list_key)
|
||||
# default to redis
|
||||
else:
|
||||
from brokers import redis_broker
|
||||
|
||||
Reference in New Issue
Block a user