def _declare_notification_queue_binding(self, target, stopwatch=pika_drv_cmns. INFINITE_STOP_WATCH): if stopwatch.expired(): raise exceptions.MessagingTimeout( "Timeout for current operation was expired.") try: timeout = stopwatch.leftover(return_none=True) with (self._pika_engine.connection_without_confirmation_pool. acquire)(timeout=timeout) as conn: self._pika_engine.declare_queue_binding_by_channel( conn.channel, exchange=(target.exchange or self._pika_engine.default_notification_exchange), queue=target.topic, routing_key=target.topic, exchange_type='direct', queue_expiration=None, durable=self._pika_engine.notification_persistence, ) except pika_pool.Timeout as e: raise exceptions.MessagingTimeout( "Timeout for current operation was expired. {}.".format( str(e)))
def _publish(pool, exchange, routing_key, body, properties, mandatory, stopwatch): """Execute pika publish method using connection from connection pool Also this message catches all pika related exceptions and raise oslo.messaging specific exceptions :param pool: Pool, pika connection pool for connection choosing :param exchange: String, RabbitMQ exchange name for message sending :param routing_key: String, RabbitMQ routing key for message routing :param body: Bytes, RabbitMQ message payload :param properties: Properties, RabbitMQ message properties :param mandatory: Boolean, RabbitMQ publish mandatory flag (raise exception if it is not possible to deliver message to any queue) :param stopwatch: StopWatch, stopwatch object for calculating allowed timeouts """ if stopwatch.expired(): raise exceptions.MessagingTimeout( "Timeout for current operation was expired.") try: timeout = stopwatch.leftover(return_none=True) with pool.acquire(timeout=timeout) as conn: if timeout is not None: properties.expiration = str(int(timeout * 1000)) conn.channel.publish(exchange=exchange, routing_key=routing_key, body=body, properties=properties, mandatory=mandatory) except pika_exceptions.NackError as e: raise pika_drv_exc.MessageRejectedException( "Can not send message: [body: {}], properties: {}] to " "target [exchange: {}, routing_key: {}]. {}".format( body, properties, exchange, routing_key, str(e))) except pika_exceptions.UnroutableError as e: raise pika_drv_exc.RoutingException( "Can not deliver message:[body:{}, properties: {}] to any " "queue using target: [exchange:{}, " "routing_key:{}]. {}".format(body, properties, exchange, routing_key, str(e))) except pika_pool.Timeout as e: raise exceptions.MessagingTimeout( "Timeout for current operation was expired. {}".format(str(e))) except pika_pool.Connection.connectivity_errors as e: if (isinstance(e, pika_exceptions.ChannelClosed) and e.args and e.args[0] == 404): raise pika_drv_exc.ExchangeNotFoundException( "Attempt to send message to not existing exchange " "detected, message: [body:{}, properties: {}], target: " "[exchange:{}, routing_key:{}]. {}".format( body, properties, exchange, routing_key, str(e))) raise pika_drv_exc.ConnectionException( "Connectivity problem detected during sending the message: " "[body:{}, properties: {}] to target: [exchange:{}, " "routing_key:{}]. {}".format(body, properties, exchange, routing_key, str(e))) except socket.timeout: raise pika_drv_exc.TimeoutConnectionException( "Socket timeout exceeded.")
def send(self, exchange, routing_key, reply_listener=None, stopwatch=pika_drv_cmns.INFINITE_STOP_WATCH, retrier=None): """Send RPC message with configured retrying :param exchange: String, RabbitMQ exchange name for message sending :param routing_key: String, RabbitMQ routing key for message routing :param reply_listener: RpcReplyPikaListener, listener for waiting reply. If None - return immediately without reply waiting :param stopwatch: StopWatch, stopwatch object for calculating allowed timeouts :param retrier: retrying.Retrier, configured retrier object for sending message, if None no retrying is performed """ msg_dict, msg_props = self._prepare_message_to_send() if reply_listener: self.msg_id = uuid.uuid4().hex msg_props.correlation_id = self.msg_id LOG.debug('MSG_ID is %s', self.msg_id) self.reply_q = reply_listener.get_reply_qname() msg_props.reply_to = self.reply_q future = reply_listener.register_reply_waiter(msg_id=self.msg_id) self._do_send(exchange=exchange, routing_key=routing_key, msg_dict=msg_dict, msg_props=msg_props, confirm=True, mandatory=True, persistent=False, stopwatch=stopwatch, retrier=retrier) try: return future.result(stopwatch.leftover(return_none=True)) except BaseException as e: reply_listener.unregister_reply_waiter(self.msg_id) if isinstance(e, futures.TimeoutError): e = exceptions.MessagingTimeout() raise e else: self._do_send(exchange=exchange, routing_key=routing_key, msg_dict=msg_dict, msg_props=msg_props, confirm=True, mandatory=True, persistent=False, stopwatch=stopwatch, retrier=retrier)
def test_index_503(self, mock_enforce): self._mock_enforce_setup(mock_enforce, 'index') req = self._get('/services') with mock.patch.object(self.controller.rpc_client, 'list_services', side_effect=exceptions.MessagingTimeout()): self.assertRaises(webob.exc.HTTPServiceUnavailable, self.controller.index, req, tenant_id=self.tenant)
def _declare_notification_queue_binding(self, target, timeout=None): if timeout is not None and timeout < 0: raise exceptions.MessagingTimeout( "Timeout for current operation was expired.") try: with (self._pika_engine.connection_without_confirmation_pool. acquire)(timeout=timeout) as conn: self._pika_engine.declare_queue_binding_by_channel( conn.channel, exchange=(target.exchange or self._pika_engine.default_notification_exchange), queue=target.topic, routing_key=target.topic, exchange_type='direct', queue_expiration=None, durable=self._pika_engine.notification_persistence, ) except pika_pool.Timeout as e: raise exceptions.MessagingTimeout( "Timeout for current operation was expired. {}.".format( str(e)))
def _declare_rpc_exchange(self, exchange, stopwatch): timeout = stopwatch.leftover(return_none=True) with (self._pika_engine.connection_without_confirmation_pool.acquire( timeout=timeout)) as conn: try: self._pika_engine.declare_exchange_by_channel( conn.channel, self._pika_engine.get_rpc_exchange_name(exchange), "direct", False) except pika_pool.Timeout as e: raise exceptions.MessagingTimeout( "Timeout for current operation was expired. {}.".format( str(e)))
def _on_timeout(self): """Invoked by the eventloop when the send fails to complete before the timeout is reached. """ self.timer = None msg = ("{name} message sent to {target} failed: timed" " out".format(name=self.name, target=self.target)) LOG.warning("%s", msg) # Only raise a MessagingTimeout if the caller has explicitly specified # a timeout. self._error = exceptions.MessagingTimeout(msg) \ if self.message.ttl else \ exceptions.MessageDeliveryFailure(msg) self._cleanup() self._wakeup.set()
def wait(self, timeout): """Wait for the send to complete, and, optionally, a reply message from the remote. Will raise MessagingTimeout if the send does not complete or no reply is received within timeout seconds. If the request has failed for any other reason, a MessagingException is raised. """ try: result = self._results_queue.get(timeout=timeout) except moves.queue.Empty: if self._wait_for_reply: reason = "Timed out waiting for a reply." else: reason = "Timed out waiting for send to complete." raise exceptions.MessagingTimeout(reason) if result["status"] == "OK": return result.get("response", None) raise result["error"]
def _on_timeout(self): """Invoked by the eventloop when the send fails to complete before the timeout is reached. """ if self._wakeup.is_set(): LOG.debug("Message send timeout occurred after send completed") return self.timer = None if self.message.ttl: msg = ("{name} message sent to {target} failed: timed" " out".format(name=self.name, target=self.target)) self._error = exceptions.MessagingTimeout(msg) else: msg = ("{name} message sent to {target} failed:" " undeliverable".format(name=self.name, target=self.target)) self._error = exceptions.MessageDeliveryFailure(msg) self._cleanup() self._wakeup.set()
def test_messaging_timeout(self): local = msg_exceptions.MessagingTimeout('took too long') self.assertRaises(msg_exceptions.MessagingTimeout, self.parent_resource.raise_local_exception, local)
def send(self, target, reply_listener=None, expiration_time=None, retrier=None): """Send RPC message with configured retrying :param target: Target, oslo.messaging target which defines RPC service :param reply_listener: RpcReplyPikaListener, listener for waiting reply. If None - return immediately without reply waiting :param expiration_time: Float, expiration time in seconds (like time.time()) :param retrier: retrying.Retrier, configured retrier object for sending message, if None no retrying is performed """ exchange = self._pika_engine.get_rpc_exchange_name( target.exchange, target.topic, target.fanout, retrier is None) queue = "" if target.fanout else self._pika_engine.get_rpc_queue_name( target.topic, target.server, retrier is None) msg_dict, msg_props = self._prepare_message_to_send() if reply_listener: self.msg_id = uuid.uuid4().hex msg_props.correlation_id = self.msg_id LOG.debug('MSG_ID is %s', self.msg_id) self.reply_q = reply_listener.get_reply_qname(expiration_time - time.time()) msg_props.reply_to = self.reply_q future = reply_listener.register_reply_waiter(msg_id=self.msg_id) self._do_send(exchange=exchange, routing_key=queue, msg_dict=msg_dict, msg_props=msg_props, confirm=True, mandatory=True, persistent=False, expiration_time=expiration_time, retrier=retrier) try: return future.result(expiration_time - time.time()) except BaseException as e: reply_listener.unregister_reply_waiter(self.msg_id) if isinstance(e, futures.TimeoutError): e = exceptions.MessagingTimeout() raise e else: self._do_send(exchange=exchange, routing_key=queue, msg_dict=msg_dict, msg_props=msg_props, confirm=True, mandatory=True, persistent=False, expiration_time=expiration_time, retrier=retrier)
def test_messaging_timeout(self): local = msg_exceptions.MessagingTimeout('took too long') self.assertRaises(msg_exceptions.MessagingTimeout, self.parent_resource.translate_remote_exceptions, local)