Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def get_celery_queue_length():
try:
app = app_or_default(None)
with app.connection_or_acquire() as conn:
return conn.default_channel.queue_declare(
queue=getattr(settings, 'CELERY_DEFAULT_QUEUE', 'celery'), passive=True).message_count
except amqp.exceptions.ChannelError:
# Queue doesn't exist, probably empty
return 0
except Exception:
logger.exception("Failed to get celery queue length")
return -1
def measure_queues_length(self):
for queue in self.queue_list:
try:
length = self.connection.default_channel.queue_declare(queue=queue, passive=True).message_count
except (amqp.exceptions.ChannelError,) as e:
logging.warning("Queue Not Found: {}. Setting its value to zero. Error: {}".format(queue, str(e)))
length = 0
self.set_queue_length(queue, length)
def _AMQPChannel_size(self, queue):
try:
from librabbitmq import ChannelError
except ImportError:
from amqp.exceptions import ChannelError
try:
queue = self.queue_declare(queue, passive=True)
except ChannelError:
# The requested queue has not been created yet
count = 0
else:
count = queue.message_count
return count
pyamqp.Channel._size = _AMQPChannel_size
Returns:
list[TurbiniaRequest]: all evidence requests.
"""
requests = []
while True:
try:
message = self.queue.get(block=False)
request = self._validate_message(message.payload)
if request:
requests.append(request)
if self.queue.queue.durable:
message.ack()
except queue.Empty:
break
except ChannelError:
break
except OperationalError as e:
log.warning(
'Caught recoverable message transport connection error when ' +
'fetching from queue: {0!s}'.format(e))
break
log.debug('Received {0:d} messages'.format(len(requests)))
return requests