def get(self): try: item = RedisQueue.conn.brpop(self.key)[1] except redis.RedisError as e: raise errors.DependencyError( 'failed to pop from redis server: {}'.format(e)) return self.deserialize(item)
def size(self): try: sz = int(RedisQueue.conn.llen(self.key)) except redis.RedisError as e: raise errors.DependencyError( 'failed to get length from redis server: {}'.format(e)) return sz
def put(self, task): try: RedisQueue.conn.lpush(self.key, self.serialize(task)) if self.max_size: RedisQueue.conn.ltrim(self.key, 0, self.max_size - 1) except redis.RedisError as e: raise errors.DependencyError( 'failed to push to redis server: {}'.format(e))
def __init__(self, *args, **kwargs): TaskQueue.__init__(self, *args, **kwargs) params = { 'host': settings.REDIS['host'], 'port': settings.REDIS.get('port', 6379), 'db': settings.REDIS.get('db', 0), } if settings.REDIS.get('password'): params.update({'password': settings.REDIS['password']}) if RedisQueue.conn is None: try: RedisQueue.conn = redis.Redis( **params) # redis.StrictRedis(**params) except redis.RedisError as e: raise errors.DependencyError( 'failed to connect to redis server: {}'.format(e)) self.max_size = int(settings.REDIS.get('max_size', 0))
def close(self): try: RedisQueue.conn.ltrim(self.key, 0, 0) except redis.RedisError as e: raise errors.DependencyError( 'failed to delete list from redis server: {}'.format(e))