mirror of
https://github.com/django-q2/django-q2.git
synced 2026-09-15 13:37:56 +08:00
* Support for multi-queue, multi-cluster configuration. API changes include: * Adding `cluster` to async_task() parameters and Task model * Adding argument --name to qcluster command * Necessary adjustments to Conf and Broker classes * Some admin improvements * Add settings.Q_CLUSTER['ALT_CLUSTERS']: q_cluster config overrides for alternative clusters; Add Conf.CLUSTER_NAME: separate usage from Conf.PREFIX; QueueAdmin/OrmQ detail page enhanced: now displaying args/kwargs/q_options instead of encrypted payload. * if `cluster` argument is not set (the default), async_task() and schedule() will be handled by the default cluster; Documentation update. * Text cleanup * Documentation update. * Fix TIMEOUT setting in Windows for non-default cluster --------- Co-authored-by: Stan Triepels <1939656+GDay@users.noreply.github.com>
67 lines
1.9 KiB
Python
67 lines
1.9 KiB
Python
import redis
|
|
from redis import Redis
|
|
|
|
from django_q.brokers import Broker
|
|
from django_q.conf import Conf, logger
|
|
|
|
try:
|
|
import django_redis
|
|
except ImportError:
|
|
django_redis = None
|
|
|
|
|
|
class Redis(Broker):
|
|
def __init__(self, list_key: str = None):
|
|
list_key = list_key or Conf.CLUSTER_NAME
|
|
super(Redis, self).__init__(list_key=f"django_q:{list_key}:q")
|
|
|
|
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):
|
|
return self.connection.delete(self.list_key)
|
|
|
|
def purge_queue(self):
|
|
return self.connection.ltrim(self.list_key, 1, 0)
|
|
|
|
def ping(self) -> bool:
|
|
try:
|
|
return self.connection.ping()
|
|
except redis.ConnectionError as e:
|
|
logger.error("Can not connect to Redis server.")
|
|
raise e
|
|
|
|
def info(self) -> str:
|
|
if not self._info:
|
|
info = self.connection.info("server")
|
|
self._info = f"Redis {info['redis_version']}"
|
|
return self._info
|
|
|
|
def set_stat(self, key: str, value: str, timeout: int):
|
|
self.connection.set(key, value, timeout)
|
|
|
|
def get_stat(self, key: str):
|
|
if self.connection.exists(key):
|
|
return self.connection.get(key)
|
|
|
|
def get_stats(self, pattern: str):
|
|
keys = self.connection.keys(pattern=pattern)
|
|
if keys:
|
|
return self.connection.mget(keys)
|
|
|
|
@staticmethod
|
|
def get_connection(list_key: str = None) -> Redis:
|
|
if django_redis and Conf.DJANGO_REDIS:
|
|
return django_redis.get_redis_connection(Conf.DJANGO_REDIS)
|
|
if isinstance(Conf.REDIS, str):
|
|
return redis.from_url(Conf.REDIS)
|
|
return redis.StrictRedis(**Conf.REDIS)
|