Files
django-q2/django_q/brokers/redis.py
T
Ilan Steemers 4f39062785 Adds django cache as monitor cache
Not the best solution yet, but it solves monitoring problems with brokers that don't have a pattern getter for cluster stats.
2015-08-30 18:28:26 +02:00

44 lines
1.2 KiB
Python

import redis
from django_q.brokers import Broker
from django_q.conf import Conf, logger
class Redis(Broker):
def enqueue(self, task):
return self.connection.rpush(self.list_key, task)
def dequeue(self):
task = self.connection.blpop(self.list_key, 1)
if task:
return None, task[1]
def queue_size(self):
return self.connection.llen(self.list_key)
def delete_queue(self, list_key=None):
list_key = list_key if list_key else self.list_key
return self.connection.delete(list_key)
def ping(self):
try:
return self.connection.ping()
except Exception as e:
logger.error('Can not connect to Redis server.')
raise e
def set_stat(self, key, value, timeout):
self.connection.set(key, value, timeout)
def get_stat(self, key):
if self.connection.exists(key):
return self.connection.get(key)
def get_stats(self, pattern):
keys = self.connection.keys(pattern=pattern)
if keys:
return self.connection.mget(keys)
@staticmethod
def get_connection():
return redis.StrictRedis(**Conf.REDIS)