Python pika.exceptions() Examples

The following are 10 code examples of pika.exceptions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pika , or try the search function .
Example #1
Source File: amqp_client.py    From cloudify-plugins-common with Apache License 2.0 6 votes vote down vote up
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) 
Example #2
Source File: rabbitmq.py    From pyspider with Apache License 2.0 5 votes vote down vote up
def catch_error(func):
    """Catch errors of rabbitmq then reconnect"""
    import amqp
    try:
        import pika.exceptions
        connect_exceptions = (
            pika.exceptions.ConnectionClosed,
            pika.exceptions.AMQPConnectionError,
        )
    except ImportError:
        connect_exceptions = ()

    connect_exceptions += (
        select.error,
        socket.error,
        amqp.ConnectionError
    )

    def wrap(self, *args, **kwargs):
        try:
            return func(self, *args, **kwargs)
        except connect_exceptions as e:
            logging.error('RabbitMQ error: %r, reconnect.', e)
            self.reconnect()
            return func(self, *args, **kwargs)
    return wrap 
Example #3
Source File: rabbitmq.py    From pyspider with Apache License 2.0 5 votes vote down vote up
def reconnect(self):
        """Reconnect to rabbitmq server"""
        import pika
        import pika.exceptions

        self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
        self.channel = self.connection.channel()
        try:
            self.channel.queue_declare(self.name)
        except pika.exceptions.ChannelClosed:
            self.connection = pika.BlockingConnection(pika.URLParameters(self.amqp_url))
            self.channel = self.connection.channel()
        #self.channel.queue_purge(self.name) 
Example #4
Source File: rabbitmq.py    From pyspider with Apache License 2.0 5 votes vote down vote up
def reconnect(self):
        """Reconnect to rabbitmq server"""
        parsed = urlparse.urlparse(self.amqp_url)
        port = parsed.port or 5672
        self.connection = amqp.Connection(host="%s:%s" % (parsed.hostname, port),
                                          userid=parsed.username or 'guest',
                                          password=parsed.password or 'guest',
                                          virtual_host=unquote(
                                              parsed.path.lstrip('/') or '%2F')).connect()
        self.channel = self.connection.channel()
        try:
            self.channel.queue_declare(self.name)
        except amqp.exceptions.PreconditionFailed:
            pass
        #self.channel.queue_purge(self.name) 
Example #5
Source File: amqp_getters_pushers.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def _make_connection(self):
        exc = None
        for attempt in xrange(self.CONNECTION_ATTEMPTS):
            parameters = pika.ConnectionParameters(**self._connection_params_dict)
            try:
                return pika.BlockingConnection(parameters)
            except pika.exceptions.AMQPConnectionError as exc:
                time.sleep(self.CONNECTION_RETRY_DELAY)
        assert exc is not None
        raise exc 
Example #6
Source File: amqp_getters_pushers.py    From n6 with GNU Affero General Public License v3.0 5 votes vote down vote up
def _handle_data(self, data, routing_key, custom_prop_kwargs):
        if self._serialize is not None:
            data = self._serialize(data)
        try:
            self._publish(data, routing_key, custom_prop_kwargs)
        except pika.exceptions.ConnectionClosed:
            if self._publishing:
                self._setup_communication()
                self._publish(data, routing_key, custom_prop_kwargs)
            else:
                # the pusher is being shut down
                # => do not try to reconnect
                raise 
Example #7
Source File: api_tools.py    From listenbrainz-server with GNU General Public License v2.0 5 votes vote down vote up
def publish_data_to_queue(data, exchange, queue, error_msg):
    """ Publish specified data to the specified queue.

    Args:
        data: the data to be published
        exchange (str): the name of the exchange
        queue (str): the name of the queue
        error_msg (str): the error message to be returned in case of an error
    """
    try:
        with rabbitmq_connection._rabbitmq.get() as connection:
            channel = connection.channel
            channel.exchange_declare(exchange=exchange, exchange_type='fanout')
            channel.queue_declare(queue, durable=True)
            channel.basic_publish(
                exchange=exchange,
                routing_key='',
                body=ujson.dumps(data),
                properties=pika.BasicProperties(delivery_mode=2, ),
            )
    except pika.exceptions.ConnectionClosed as e:
        current_app.logger.error("Connection to rabbitmq closed while trying to publish: %s" % str(e), exc_info=True)
        raise APIServiceUnavailable(error_msg)
    except Exception as e:
        current_app.logger.error("Cannot publish to rabbitmq channel: %s / %s" % (type(e).__name__, str(e)), exc_info=True)
        raise APIServiceUnavailable(error_msg) 
Example #8
Source File: pika.py    From rasa-for-botfront with Apache License 2.0 5 votes vote down vote up
def close_pika_channel(
    channel: "Channel",
    attempts: int = 1000,
    time_between_attempts_in_seconds: float = 0.001,
) -> None:
    """Attempt to close Pika channel and wait until it is closed.

    Args:
        channel: Pika `Channel` to close.
        attempts: How many times to try to confirm that the channel has indeed been
            closed.
        time_between_attempts_in_seconds: Wait time between attempts to confirm closed
            state.
    """
    from pika.exceptions import AMQPError

    try:
        channel.close()
        logger.debug("Successfully initiated closing of Pika channel.")
    except AMQPError:
        logger.exception("Failed to initiate closing of Pika channel.")

    while attempts:
        if channel.is_closed:
            logger.debug("Successfully closed Pika channel.")
            return None

        time.sleep(time_between_attempts_in_seconds)
        attempts -= 1

    logger.exception("Failed to close Pika channel.") 
Example #9
Source File: pika.py    From rasa-for-botfront with Apache License 2.0 5 votes vote down vote up
def close_pika_connection(connection: "Connection") -> None:
    """Attempt to close Pika connection."""
    from pika.exceptions import AMQPError

    try:
        connection.close()
        logger.debug("Successfully closed Pika connection with host.")
    except AMQPError:
        logger.exception("Failed to close Pika connection with host.") 
Example #10
Source File: amqp_getters_pushers.py    From n6 with GNU Affero General Public License v3.0 4 votes vote down vote up
def __init__(self,
                 output_fifo_max_size=20000,
                 error_callback=None,
                 # 15 seems to be conservative enough, even paranoic a bit :-)
                 publishing_thread_join_timeout=15,
                 **kwargs):
        """
        Initialize the instance and start the publishing co-thread.

        Kwargs -- the same as for AMQPSimplePusher plus also:

            `output_fifo_max_size` (int; default: 20000):
                Maximum length of the internal output fifo.

            `error_callback` (None or a callable object; default: None):
                A callback to be used when an exception (being an instance
                of an Exception subclass) is caught in the publishing
                co-thread while trying to publish some data.  The callback
                will be called with the exception object as the sole
                argument, in the publishing co-thread.

                If there is no callback, i.e. `error_callback` is None:
                the exception's traceback will be printed to sys.stderr.

                If the callback throws another exception it will be caught
                and its traceback will be printed to sys.stderr.

            `publishing_thread_join_timeout` (int; default: 15):
                Related to pusher shut down: the timeout value (in seconds)
                for joining the publishing thread before checking the internal
                heartbeat flag; the value should not be smaller than a
                reasonable duration of one iteration of the publishing thread
                loop (which includes getting a message from the inner queue,
                serializing the message and sending it to the AMQP broker,
                handling any exception etc.).

        Raises:
            A pika.exceptions.AMQPError subclass:
                If AMQP connection cannot be set up.
        """

        self._output_fifo = Queue.Queue(maxsize=output_fifo_max_size)
        self._error_callback = error_callback
        self._publishing_thread_join_timeout = publishing_thread_join_timeout
        self._publishing_thread = None  # to be set in _start_publishing()
        self._publishing_thread_heartbeat_flag = False

        super(AMQPThreadedPusher, self).__init__(**kwargs)


    #
    # Non-public methods