def _on_connection_closed(self, method_frame, from_adapter=False): """Called when the connection is closed remotely. The from_adapter value will be true if the connection adapter has been disconnected from the broker and the method was invoked directly instead of by receiving a Connection.Close frame. :param pika.frame.Method: The Connection.Close frame :param bool from_adapter: Called by the connection adapter :raises: pika.exceptions.ConnectionClosed """ if self._is_connection_close_frame(method_frame): self.closing = (method_frame.method.reply_code, method_frame.method.reply_text) LOGGER.warning("Disconnected from RabbitMQ at %s:%i (%s): %s", self.params.host, self.params.port, self.closing[0], self.closing[1]) self._set_connection_state(self.CONNECTION_CLOSED) self._remove_connection_callbacks() if not from_adapter: self._adapter_disconnect() for channel in self._channels: self._channels[channel]._on_close(method_frame) self._remove_connection_callbacks() if self.closing[0] not in [0, 200]: raise exceptions.ConnectionClosed(*self.closing)
def threaded_function(self, args): while not self.done: try: self.connection.process_data_events() except exceptions.ConnectionClosed(): self.init_connection() self.connection.process_data_events() sleep(10)
def _check_state_on_disconnect(self): """Checks closing corner cases to see why we were disconnected and if we should raise exceptions for the anticipated exception types. """ super(BlockingConnection, self)._check_state_on_disconnect() if self.is_open: # already logged a warning in the base class, now fire an exception raise exceptions.ConnectionClosed()
def _notify_all_futures_connection_close(self): while self._task_queue: try: method_res_future = self._task_queue.pop()[3] except KeyError: break else: method_res_future.set_exception( pika_exceptions.ConnectionClosed()) while self._pending_connection_futures: try: pending_connection_future = ( self._pending_connection_futures.pop()) except KeyError: break else: pending_connection_future.set_exception( pika_exceptions.ConnectionClosed())
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 __init__(self, rabbit_host, rabbit_port, q_name): self.q_name = q_name self.rabbit_host = rabbit_host self.rabbit_port = rabbit_port self.conumer_tag = str(socket.gethostname()) + "_worker_" + str( uuid.uuid4()) try: self.init_connection() except exceptions.ConnectionClosed(): self.init_connection() self.thread = Thread(target=self.threaded_function, args=(1, )) self.util = Util() self.argo = Argo() self.done = False self.done_count = 0
def test_publish_reconnect_failed_rejected(self): # The publisher must try to re-establish a connection on publish, and # close the connection if it can't be established. self.publisher._channel.publish.side_effect = pika_errs.ConnectionClosed( 200, "I wanted to") connection_class_mock = mock.Mock() connection_mock = mock.Mock() connection_class_mock.return_value = connection_mock connection_mock.channel.side_effect = pika_errs.NackError( [self.message]) with mock.patch("fedora_messaging._session.pika.BlockingConnection", connection_class_mock): self.assertRaises(PublishReturned, self.publisher.publish, self.message) # Check that the connection was reestablished connection_class_mock.assert_called_with(self.publisher._parameters) self.assertEqual(self.publisher._connection, connection_mock)
def test_publish_disconnected(self): # The publisher must try to re-establish a connection on publish. self.publisher._channel.publish.side_effect = pika_errs.ConnectionClosed( 200, "I wanted to") connection_class_mock = mock.Mock() connection_mock = mock.Mock() channel_mock = mock.Mock() connection_class_mock.return_value = connection_mock connection_mock.channel.return_value = channel_mock with mock.patch("fedora_messaging._session.pika.BlockingConnection", connection_class_mock): self.publisher.publish(self.message) # Check that the connection was reestablished connection_class_mock.assert_called_with(self.publisher._parameters) channel_mock.confirm_delivery.assert_called_once() self.assertEqual(self.publisher._connection, connection_mock) self.assertEqual(self.publisher._channel, channel_mock) channel_mock.publish.assert_called_once()
def _validate_connection_and_channel(self): if self.connection.is_closed(): raise exceptions.ConnectionClosed() if self.is_closed: raise exceptions.ChannelClosed()