Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def callback(ch, method, properties, body):
try:
ev = json.loads(body)
output = cloudify.logs.create_event_message_prefix(ev)
if output:
sys.stdout.write('{0}\n'.format(output))
except:
logger.error('event/log format error - output: {0}'
.format(body), exc_info=True)
channel.basic_consume(callback, queue=queues[0], no_ack=True)
channel.basic_consume(callback, queue=queues[1], no_ack=True)
try:
channel.start_consuming()
except pika.exceptions.ConnectionClosed:
pass
self.ch.basic_qos(prefetch_count=100)
self.ch.basic_consume(
consumer_callback=self.on_consume,
queue='default',
no_ack=False,
)
print("[x] Waiting message ...")
self.is_init = True
# Begin consuming
self.ch.start_consuming()
except KeyboardInterrupt:
self.conn.close()
print("[x] Agent interrupted by users ...")
sys.exit(0)
except pika.exceptions.ConnectionClosed as e:
# self.conn.close()
print("Exception while init: {0}. Now sleep 5s and retry ...".format(e))
time.sleep(5)
except Exception as e:
# self.conn.close()
print("Exception while init: {0}. Now sleep 5s and retry ...".format(e))
time.sleep(5)
while True:
self.connect_to_rabbitmq()
self.channel = self.connection.channel()
self.channel.exchange_declare(exchange=current_app.config['UNIQUE_EXCHANGE'], exchange_type='fanout')
self.channel.queue_declare(current_app.config['UNIQUE_QUEUE'], durable=True)
self.channel.queue_bind(exchange=current_app.config['UNIQUE_EXCHANGE'], queue=current_app.config['UNIQUE_QUEUE'])
self.channel.basic_consume(
lambda ch, method, properties, body: self.static_callback(ch, method, properties, body, obj=self),
queue=current_app.config['UNIQUE_QUEUE'],
)
self.channel.basic_qos(prefetch_count=PREFETCH_COUNT)
current_app.logger.info("bigquery-writer started")
try:
self.channel.start_consuming()
except pika.exceptions.ConnectionClosed:
current_app.logger.warn("Connection to rabbitmq closed. Re-opening.")
self.connection = None
self.channel = None
continue
self.connection.close()
def ask_exam_enrollment_form(stud, off_year):
if 'exam_enrollment' in settings.INSTALLED_APPS and hasattr(settings, 'QUEUES') and settings.QUEUES:
try:
message_published = ask_queue_for_exam_enrollment_form(stud, off_year)
except (RuntimeError, pika.exceptions.ConnectionClosed, pika.exceptions.ChannelClosed,
pika.exceptions.AMQPError):
return HttpResponse(status=400)
if message_published:
return HttpResponse(status=200)
return HttpResponse(status=405)
def current_status():
load = "%.2f %.2f %.2f" % os.getloadavg()
try:
with rabbitmq_connection._rabbitmq.get() as connection:
queue = connection.channel.queue_declare(current_app.config['INCOMING_QUEUE'], passive=True, durable=True)
incoming_len_msg = format(int(queue.method.message_count), ',d')
queue = connection.channel.queue_declare(current_app.config['UNIQUE_QUEUE'], passive=True, durable=True)
unique_len_msg = format(int(queue.method.message_count), ',d')
except (pika.exceptions.ConnectionClosed, pika.exceptions.ChannelClosed):
current_app.logger.error('Unable to get the length of queues', exc_info=True)
incoming_len_msg = 'Unknown'
unique_len_msg = 'Unknown'
listen_count = _influx.get_total_listen_count()
try:
user_count = format(int(_get_user_count()), ',d')
except DatabaseException as e:
user_count = 'Unknown'
return render_template(
"index/current-status.html",
load=load,
listen_count=format(int(listen_count), ",d"),
incoming_len=incoming_len_msg,
unique_len=unique_len_msg,
def resend_messages_to_queue(self, request, queryset):
counter = 0
for record in queryset:
try:
queue_sender.send_message(settings.QUEUES.get('QUEUES_NAME').get('EXAM_ENROLLMENT_FORM_SUBMISSION'),
record.document)
counter += 1
except (ChannelClosed, ConnectionClosed):
self.message_user(request,
'Message %s not sent to %s.' % (record.pk, record.queue_name),
level=messages.ERROR)
self.message_user(request, "{} message(s) sent.".format(counter), level=messages.SUCCESS)
raise socket.timeout("timed out")
def on_data_available(self, buf):
self._event_counter += 1
self.Super.on_data_available(self, buf)
def close(self):
self.client = None
self.Super.close(self)
class SyncTransport(base.Transport):
default_port = DEFAULT_PORT
connection_errors = (socket.error,
exceptions.ConnectionClosed,
exceptions.ChannelClosed,
exceptions.LoginError,
exceptions.NoFreeChannels,
exceptions.DuplicateConsumerTag,
exceptions.UnknownConsumerTag,
exceptions.RecursiveOperationDetected,
exceptions.ContentTransmissionForbidden,
exceptions.ProtocolSyntaxError)
channel_errors = (exceptions.ChannelClosed,
exceptions.DuplicateConsumerTag,
exceptions.UnknownConsumerTag,
exceptions.ProtocolSyntaxError)
Message = Message
Connection = BlockingConnection
def process_data_events(self):
"""Will make sure that data events are processed. Your app can
block on this method.
"""
try:
if self._handle_read():
self._socket_timeouts = 0
except AttributeError:
raise exceptions.ConnectionClosed()
except socket.timeout:
self._handle_timeout()
self._flush_outbound()
self.process_timeouts()
def connect(self):
if not self._conn or self._conn.is_closed:
# RMQ.
for i in range(10):
try:
self._conn = pika.BlockingConnection(self._params)
except pika.exceptions.ConnectionClosed:
logger.error('Connection to RMQ failed. retring. ({}/{})'.format(i, self.MAX_RETRIES))
time.sleep(5)
continue
else:
logger.info('Connected to RMQ')
break
# Experience channel.
self._xp_channel = self._conn.channel()
self._xp_channel.basic_qos(prefetch_count=self.prefetch_count)
self._xp_channel.queue_declare(queue=self.EXPERIENCE_QUEUE_NAME)
# Model Exchange.
if self.use_model_exchange:
self._model_exchange = self._conn.channel()
self._model_exchange.exchange_declare(
def _on_connection_close(self, connection, reply_code_or_reason, reply_text=None):
"""
Callback invoked when a previously-opened connection is closed.
Args:
connection (pika.connection.SelectConnection): The connection that
was just closed.
reply_code_or_reason (int|Exception): The reason why the channel
was closed. In older versions of pika, this is the AMQP code.
reply_text (str): The human-readable reason the connection was
closed (only in older versions of pika)
"""
self._channel = None
if isinstance(reply_code_or_reason, pika_errs.ConnectionClosed):
reply_code = reply_code_or_reason.reply_code
reply_text = reply_code_or_reason.reply_text
elif isinstance(reply_code_or_reason, int):
reply_code = reply_code_or_reason
else:
reply_code = 0
reply_text = str(reply_code_or_reason)
if reply_code == 200:
# Normal shutdown, exit the consumer.
_log.info("Server connection closed (%s), shutting down", reply_text)
connection.ioloop.stop()
else:
_log.warning(
"Connection to %s closed unexpectedly (%d): %s",
connection.params.host,