Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.logger.info("processor_count (%s) > cpu_count. Defaulting to cpu_count", (processor_count, cpu_count))
processor_count = cpu_count
self.event_processor = EventProcessor(self.forwarder_options)
self.processor_pool = multiprocessing.Pool(processor_count)
while True:
try:
self.consume_message_bus(test=self.testing)
except Exception as e:
self.retry_attempts += 1
if self.retry_attempts > self.max_retry_attempts:
self.logger.critical("Too many attempts to reconnect (%d). Exiting now." % self.max_retry_attempts)
break
if isinstance(e, pika.exceptions.AMQPConnectionError) or isinstance(e, pika.exceptions.ConnectionClosed):
self.logger.error("Connection is closed or refused, retrying in %s seconds" % self.retry_interval)
else:
self.logger.exception("An unexpected error occurred, retrying in %s seconds" % self.retry_interval)
if self.connection is not None:
self.connection.close()
self.connection = None
time.sleep(self.retry_interval)
def test_connection_close(self):
self.obj._idle_byte_intervals = 3
self.obj._idle_heartbeat_intervals = 4
self.obj._close_connection()
reason = self.obj._STALE_CONNECTION % self.obj._timeout
self.mock_conn._terminate_stream.assert_called_once_with(mock.ANY)
self.assertIsInstance(self.mock_conn._terminate_stream.call_args[0][0],
pika.exceptions.AMQPHeartbeatTimeout)
self.assertEqual(
self.mock_conn._terminate_stream.call_args[0][0].args[0],
reason)
except socket.error as err:
if err.errno != errno.EWOULDBLOCK:
recv_data = False
continue_recv = False
# send events to rabbit
try:
buffers = buffer.strip().split(b'\n')
for buff in buffers:
self.channel.basic_publish(exchange=self.exchange,
routing_key=self.routing_key,
body=buff,
properties=pika.BasicProperties(
delivery_mode=2,
))
buffer = b''
except pika.exceptions.AMQPError as err:
print("Unable to send event to RabbitMQ because: %s" % err)
print("The following event will be retried: %r" % buffer)
self.rabbit_conn()
sys.stdout.flush()
self.sock.close()
def start(self):
disconnected = True
while disconnected:
try:
disconnected = False
self.channel.start_consuming() # blocking call
except pika.exceptions.ConnectionClosed: # when connection is lost, e.g. rabbitmq not running
logging.error("Lost connection to rabbitmq service on manager")
disconnected = True
time.sleep(10) # reconnect timer
logging.info("Trying to reconnect...")
self.connect()
self.clear_message_queue() #could this make problems if the manager replies too fast?
def channel(self, channel_number=None, on_open_callback=None):
"""Create a new channel with the next available channel number or pass
in a channel number to use. Must be non-zero if you would like to
specify but it is recommended that you let Pika manage the channel
numbers.
:param int channel_number: The channel number to use, defaults to the
next available.
:param callable on_open_callback: The callback when the channel is
opened. The callback will be invoked with the `Channel` instance
as its only argument.
:rtype: pika.channel.Channel
"""
if not self.is_open:
raise exceptions.ConnectionWrongStateError(
'Channel allocation requires an open connection: %s' % self)
validators.rpc_completion_callback(on_open_callback)
if not channel_number:
channel_number = self._next_channel_number()
self._channels[channel_number] = self._create_channel(
channel_number, on_open_callback)
self._add_channel_callbacks(channel_number)
self._channels[channel_number].open()
return self._channels[channel_number]
class Connection(object):
"""
Connection acquired from a `Pool` instance. Get them like this:
.. code:: python
with pool.acquire() as cxn:
print cxn.channel
"""
#: Exceptions that imply connection has been invalidated.
connectivity_errors = (
pika.exceptions.AMQPConnectionError,
pika.exceptions.ConnectionClosed,
pika.exceptions.ChannelClosed,
select.error, # XXX: https://github.com/pika/pika/issues/412
)
@classmethod
def is_connection_invalidated(cls, exc):
"""
Says whether the given exception indicates the connection has been invalidated.
:param exc: Exception object.
:return: True if connection has been invalidted, otherwise False.
"""
return any(
isinstance(exc, error)for error in cls.connectivity_errors
try:
del self._protocol._consumers[self.queue]
except (KeyError, AttributeError):
pass
try:
del self._protocol.factory._consumers[self.queue]
except (KeyError, AttributeError):
pass
# Signal to the _read loop it's time to stop and wait for it to finish
# with whatever message it might be working on, then wait for the deferred
# to fire which indicates it is done.
self._running = False
yield self._read_loop
try:
yield self._channel.basic_cancel(consumer_tag=self._tag)
except pika.exceptions.AMQPChannelError:
# Consumers are tied to channels, so if this channel is dead the
# consumer should already be canceled (and we can't get to it anyway)
pass
try:
yield self._channel.close()
except pika.exceptions.AMQPChannelError:
pass
if not self.result.called:
self.result.callback(self)
def _check_for_protocol_mismatch(self, value):
"""Invoked when starting a connection to make sure it's a supported
protocol.
:param pika.frame.Method value: The frame to check
:raises: ProtocolVersionMismatch
"""
if ((value.method.version_major, value.method.version_minor) !=
spec.PROTOCOL_VERSION[0:2]):
raise exceptions.ProtocolVersionMismatch(frame.ProtocolHeader(),
value)
def write(self, data):
"""Publishes data to RabbitMQ
"""
try:
self.unique_ch.basic_publish(
exchange=config.SPARK_EXCHANGE,
routing_key='',
body=json.dumps(data),
properties=pika.BasicProperties(delivery_mode = 2,),
)
return True
except pika.exceptions.ConnectionClosed:
logging.error("Connection to rabbitmq closed while trying to publish. Re-opening.", exc_info=True)
except:
return False
def publish_message(self, message, message_type):
if self._is_closed:
raise exceptions.ClosedAMQPClientException(
'Publish failed, AMQP client already closed')
if message_type == 'event':
exchange = self.EVENTS_EXCHANGE_NAME
else:
exchange = self.LOGS_EXCHANGE_NAME
routing_key = ''
body = json.dumps(message)
try:
self.channel.basic_publish(exchange=exchange,
routing_key=routing_key,
body=body)
except pika.exceptions.ConnectionClosed as e:
logger.warn(
'Connection closed unexpectedly for thread {0}, '
'reconnecting. ({1}: {2})'
.format(threading.current_thread(), type(e).__name__, repr(e)))
# obviously, there is no need to close the current
# channel/connection.
self._connect()
self.channel.basic_publish(exchange=exchange,
routing_key=routing_key,
body=body)