Esempio n. 1
0
    def add_on_channel_close_callback(self):
        """This method tells pika to call the on_channel_closed method if
        RabbitMQ unexpectedly closes the channel.

        """
        logger.debug('Adding channel close callback')
        self._channel.add_on_close_callback(self.on_channel_closed)
Esempio n. 2
0
    def close_channel(self):
        """Call to close the channel with RabbitMQ cleanly by issuing the
        Channel.Close RPC command.

        """
        logger.debug('Closing the channel')
        self._channel.close()
Esempio n. 3
0
    def stop_consuming(self):
        """Tell RabbitMQ that you would like to stop consuming by sending the
        Basic.Cancel RPC command.

        """
        if self._channel:
            logger.debug('Sending a Basic.Cancel RPC command to RabbitMQ')
            self._channel.basic_cancel(self.on_cancelok, self._consumer_tag)
Esempio n. 4
0
    def add_on_cancel_callback(self):
        """Add a callback that will be invoked if RabbitMQ cancels the consumer
        for some reason. If RabbitMQ does cancel the consumer,
        on_consumer_cancelled will be invoked by pika.

        """
        logger.debug('Adding consumer cancellation callback')
        self._channel.add_on_cancel_callback(self.on_consumer_cancelled)
Esempio n. 5
0
    def acknowledge_message(self, delivery_tag):
        """Acknowledge the message delivery from RabbitMQ by sending a
        Basic.Ack RPC method for the delivery tag.

        :param int delivery_tag: The delivery tag from the Basic.Deliver frame

        """
        logger.debug('Acknowledging message %s', delivery_tag)
        self._channel.basic_ack(delivery_tag)
Esempio n. 6
0
    def on_exchange_declareok(self, unused_frame):
        """Invoked by pika when RabbitMQ has finished the Exchange.Declare RPC
        command.

        :param pika.Frame.Method unused_frame: Exchange.DeclareOk response frame

        """
        logger.debug('Exchange declared')
        self.setup_queue(self._queue)
Esempio n. 7
0
    def on_bindok(self, unused_frame):
        """Invoked by pika when the Queue.Bind method has completed. At this
        point we will start consuming messages by calling start_consuming
        which will invoke the needed RPC commands to start the process.

        :param pika.frame.Method unused_frame: The Queue.BindOk response frame

        """
        logger.debug('Queue bound')
        self.start_consuming()
Esempio n. 8
0
    def on_cancelok(self, unused_frame):
        """This method is invoked by pika when RabbitMQ acknowledges the
        cancellation of a consumer. At this point we will close the channel.
        This will invoke the on_channel_closed method once the channel has been
        closed, which will in-turn close the connection.

        :param pika.frame.Method unused_frame: The Basic.CancelOk frame

        """
        logger.debug('RabbitMQ acknowledged the cancellation of the consumer')
        self.close_channel()
Esempio n. 9
0
    def start_consuming(self):
        """This method sets up the consumer by first calling
        add_on_cancel_callback so that the object is notified if RabbitMQ
        cancels the consumer. It then issues the Basic.Consume RPC command
        which returns the consumer tag that is used to uniquely identify the
        consumer with RabbitMQ. We keep the value to use it when we want to
        cancel consuming. The on_message method is passed in as a callback pika
        will invoke when a message is fully received.

        """
        logger.debug('Issuing consumer related RPC commands')
        self.add_on_cancel_callback()
        self._consumer_tag = self._channel.basic_consume(self.on_message,
                                                         self._queue)
Esempio n. 10
0
def handle_message(message):
    # This is setup to handle stack notifications. Changes will be needed to
    # accomodate resources once it has been decided how Heat generates those
    # notifications.

    event_type = message.get('event_type')
    payload = message.get('payload')
    stack_id = payload.get('stack_identity')
    timestamp = message.get('timestamp')

    # If the notification is a start, then save its timestamp to redis
    if re.search('start$', event_type):
        logger.debug('Saving %s for %s to %s' % (event_type, stack_id,
                     timestamp))
        redis.set(stack_id, timestamp)

    # if the notification is an end, retreive the start timestamp from redis,
    # determine the time taken from start to end, and send a timing event to
    # statsd.
    if re.search('end$', event_type):
        event_type = re.sub('\.end$', '', event_type)
        start = redis.get(stack_id)
        logger.debug('Retrieved %s for %s from redis' % (start, stack_id))
        if start:
            start_time = datetime.strptime(start, '%Y-%m-%d %H:%M:%S.%f')
            end_time = datetime.strptime(message.get('timestamp'),
                                         '%Y-%m-%d %H:%M:%S.%f')
            duration_td = end_time - start_time
            duration = duration_td.total_seconds()

            logger.debug('Sending %s to statsd: %s' % (event_type, duration))
            statsd.timing(event_type, duration)

            logger.debug('Deleting key %s from redis' % stack_id)
            redis.delete(stack_id)
Esempio n. 11
0
    def on_message(self, unused_channel, basic_deliver, properties, body):
        """Invoked by pika when a message is delivered from RabbitMQ. The
        channel is passed for your convenience. The basic_deliver object that
        is passed in carries the exchange, routing key, delivery tag and
        a redelivered flag for the message. The properties passed in is an
        instance of BasicProperties with the message properties and the body
        is the message that was sent.

        :param pika.channel.Channel unused_channel: The channel object
        :param pika.Spec.Basic.Deliver: basic_deliver method
        :param pika.Spec.BasicProperties: properties
        :param str|unicode body: The message body

        """
        logger.debug('Received message # %s', basic_deliver.delivery_tag)
        oslo_message = json.loads(body)
        message = json.loads(oslo_message.get('oslo.message'))
        handle_message(message)
        self.acknowledge_message(basic_deliver.delivery_tag)