Beispiel #1
0
    def setup(self):
        if self.use_ssl:
            self.connection = amqpstorm.Connection(
                self.host,
                self.username,
                self.password,
                port=5671,
                ssl=True,
                ssl_options={
                    'ssl_version': ssl.PROTOCOL_TLSv1_2,
                    'cert_reqs': ssl.CERT_REQUIRED,
                    'keyfile': self.tls_config.get('keyfile'),
                    'certfile': self.tls_config.get('cerfile'),
                    'ca_certs': self.tls_config.get('cafile'),
                })
        else:
            self.connection = amqpstorm.Connection(self.host, self.username,
                                                   self.password)
        self.channel = self.connection.channel()

        result = self.channel.queue.declare(exclusive=True)
        self.resp_queue = result['queue']
        self.channel.basic.consume(self._on_response,
                                   no_ack=True,
                                   queue=self.resp_queue)

        self.channel.queue.declare(queue=self.on_request)
        self.channel.queue.purge(queue=self.on_request)
        self.channel.basic.qos(prefetch_count=100)
        self.channel.basic.consume(self._on_request, queue=self.on_request)
Beispiel #2
0
    def connect(self):
        """Create a connection (or reconnect after error).

        If connection can't be established, try it again indefinitely.
        """
        if self.connection:
            self.connection.close()
        attempts = 0
        while True:
            attempts += 1
            try:
                self.connection = amqpstorm.Connection(**self.conn_params)
                self.log.debug("AMQP connection created, server: '{hostname}:{port}/{virtual_host}'".format_map(self.conn_params))
                if attempts > 1:
                    # This was a repeated attempt, print success message with ERROR level
                    self.log.error("... it's OK now, we're successfully connected!")

                self.channel = self.connection.channel()
                self.channel.confirm_deliveries()
                self.channel.basic.qos(PREFETCH_COUNT)
                break
            except amqpstorm.AMQPError as e:
                sleep_time = RECONNECT_DELAYS[min(attempts, len(RECONNECT_DELAYS))-1]
                self.log.error("RabbitMQ connection error (will try to reconnect in {}s): {}".format(sleep_time, e))
                time.sleep(sleep_time)
            except KeyboardInterrupt:
                break
Beispiel #3
0
    def test_channel0_on_hearbeat_registers_heartbeat(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        last_heartbeat = connection.heartbeat._last_heartbeat
        start_time = time.time()
        channel = Channel0(connection)

        time.sleep(0.1)

        def fake(*_):
            pass

        # Don't try to write to socket during test.
        channel._write_frame = fake

        # As the heartbeat timer was never started, it should be 0.
        self.assertEqual(connection.heartbeat._last_heartbeat, 0.0)

        channel.on_frame(Heartbeat())

        self.assertNotEqual(connection.heartbeat._last_heartbeat,
                            last_heartbeat)
        self.assertGreater(connection.heartbeat._last_heartbeat, start_time)
Beispiel #4
0
 def test_channel0_heartbeat(self):
     connection = amqpstorm.Connection('localhost',
                                       'guest',
                                       'guest',
                                       lazy=True)
     channel = Channel0(connection)
     self.assertIsNone(channel.on_frame(Heartbeat()))
Beispiel #5
0
 def test_channel0_invalid_authentication_mechanism(self):
     connection = amqpstorm.Connection('localhost',
                                       'guest',
                                       'guest',
                                       lazy=True)
     channel = Channel0(connection)
     channel._send_start_ok_frame(
         Connection.Start(mechanisms='CRAM-MD5 SCRAM-SHA-1 SCRAM-SHA-256'))
     self.assertRaises(AMQPConnectionError, connection.check_for_errors)
Beispiel #6
0
    def __enter__(self):
        self._connection = amqpstorm.Connection(
            self._configuration.host,
            self._configuration.user,
            self._configuration.password,
        ).__enter__()
        self._channel = self._connection.channel().__enter__()

        return self
Beispiel #7
0
    def test_channel0_unhandled_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel0(connection)

        channel.on_frame(FakeFrame())

        self.assertEqual(self.logging_handler.messages['error'][0],
                         "[Channel0] Unhandled Frame: FakeFrame")
Beispiel #8
0
 def test_channel0_forcefully_closed_connection(self):
     connection = amqpstorm.Connection('localhost',
                                       'guest',
                                       'guest',
                                       lazy=True)
     connection.set_state(connection.OPEN)
     channel = Channel0(connection)
     channel._close_connection(
         Connection.Close(reply_text=b'', reply_code=500))
     self.assertTrue(connection.is_closed)
     self.assertRaises(AMQPConnectionError, connection.check_for_errors)
Beispiel #9
0
    def test_channel_basic_cancel_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel(0, connection, rpc_timeout=360)

        channel.on_frame(specification.Basic.Cancel('unit-test'))

        self.assertEqual(self.logging_handler.messages['warning'][0],
                         'Received Basic.Cancel on consumer_tag: unit-test')
    def test_channel_basic_cancel_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel(0, connection, rpc_timeout=1)

        channel.on_frame(specification.Basic.Cancel('travis-ci'))

        self.assertEqual(self.get_last_log(),
                         'Received Basic.Cancel on consumer_tag: travis-ci')
Beispiel #11
0
    def test_channel_close_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel(0, connection, rpc_timeout=360)

        channel.on_frame(
            specification.Channel.Close(reply_code=500, reply_text='test'))

        self.assertEqual(str(channel.exceptions[0]),
                         'Channel 0 was closed by remote server: test')
 def open(self):
     """Open Connection."""
     self.connection = amqpstorm.Connection(self.host, self.username,
                                            self.password)
     self.channel = self.connection.channel()
     self.channel.queue.declare(self.rpc_queue)
     result = self.channel.queue.declare(exclusive=True)
     self.callback_queue = result['queue']
     self.channel.basic.consume(self._on_response,
                                no_ack=True,
                                queue=self.callback_queue)
     self._create_process_thread()
Beispiel #13
0
    def open(self):
        self.connection = amqpstorm.Connection(self.host,
                                               self.username,
                                               self.password)

        self.channel = self.connection.channel()

        result = self.channel.queue.declare(exclusive=True)
        self.callback_queue = result['queue']

        self.channel.basic.consume(self._on_response, no_ack=True,
                                   queue=self.callback_queue)
Beispiel #14
0
    def test_channel0_open_ok_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel0(connection)

        self.assertFalse(connection.is_open)

        channel.on_frame(Connection.OpenOk())

        self.assertTrue(connection.is_open)
Beispiel #15
0
    def channel(self):
        if self._channel is None:
            self._connection = amqpstorm.Connection(settings.MESSAGING_HOST,
                                                    settings.MESSAGING_USER,
                                                    settings.MESSAGING_PASSWORD,
                                                    port=settings.MESSAGING_PORT,
                                                    ssl=settings.MESSAGING_SSL)

            self._channel = self._connection.channel()

            self._channel.exchange.declare(exchange=settings.MESSAGING_EXCHANGE_NAME, exchange_type='fanout')
        return self._channel
    def test_channel_unhandled_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel(0, connection, rpc_timeout=1)

        channel.on_frame(FakeFrame())

        self.assertEqual(
            self.get_last_log(), "[Channel0] Unhandled Frame: FakeFrame -- "
            "{'data_1': 'hello world'}")
Beispiel #17
0
    def test_channel_unhandled_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel(0, connection, rpc_timeout=360)

        channel.on_frame(FakeFrame())

        self.assertEqual(
            self.logging_handler.messages['error'][0],
            "[Channel0] Unhandled Frame: FakeFrame -- "
            "{'data_1': 'hello world'}")
Beispiel #18
0
    def run(self):
        self._logger.info("Starting RabbitMQ Subscriber.")

        with amqpstorm.Connection(
                self._configuration.host,
                self._configuration.user,
                self._configuration.password,
        ) as connection:
            with connection.channel() as channel:
                self._logger.info("Declaring %s Exchange." %
                                  self._configuration.exchange)
                # Ensure the Exchange exists.
                channel.exchange.declare(self._configuration.exchange,
                                         exchange_type="direct")

                # Set QoS to limit number of messages that the consumer will prefetch
                # from the remote.
                channel.basic.qos(self._configuration.prefetch_count)

                # Set up the Subscriptions.
                for subscription in self._subscriptions:
                    self._logger.info("Declaring %s Queue." %
                                      subscription.queue)
                    # Ensure the Queue exists.
                    channel.queue.declare(subscription.queue, durable=True)

                    self._logger.info("Binding queue %s to %s, on %s." % (
                        subscription.queue,
                        subscription.routing_key,
                        self._configuration.exchange,
                    ))
                    # Bind the Queue to the Exchange for the Routing Key.
                    channel.queue.bind(
                        subscription.queue,
                        exchange=self._configuration.exchange,
                        routing_key=subscription.routing_key,
                    )

                    self._logger.info("Adding a Subscription for %s." %
                                      (subscription.queue))
                    # Configure our subscription to consume from the Queue
                    channel.basic.consume(
                        partial(self._on_message, subscription.handler,
                                subscription.queue),
                        subscription.queue,
                        no_ack=False,
                    )

                self._logger.info("Starting Consumer.")
                # Start consuming the queue, using the on_message callback.
                channel.start_consuming()
Beispiel #19
0
    def test_channel_closed_after_connection_exception(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel(0, connection, 360)
        connection.exceptions.append(AMQPConnectionError('travis-ci'))
        channel.set_state(channel.OPEN)

        self.assertTrue(connection.is_closed)
        self.assertTrue(channel.is_open)
        self.assertRaisesRegexp(exception.AMQPConnectionError, 'travis-ci',
                                channel.check_for_errors)
        self.assertTrue(channel.is_closed)
Beispiel #20
0
    def test_channel0_is_blocked(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel0(connection)

        self.assertFalse(channel.is_blocked)

        channel.on_frame(Connection.Blocked('unit-test'))

        self.assertTrue(channel.is_blocked)
        self.assertEqual(self.logging_handler.messages['warning'][0],
                         'Connection is blocked by remote server: unit-test')
Beispiel #21
0
    def test_channel0_unblocked(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel0(connection)

        channel.on_frame(Connection.Blocked())

        self.assertTrue(channel.is_blocked)

        channel.on_frame(Connection.Unblocked())

        self.assertFalse(channel.is_blocked)
        self.assertEqual(self.logging_handler.messages['info'][0],
                         'Connection is no longer blocked by remote server')
Beispiel #22
0
 def __open_connection(self):
     self.log.info("Initializing AMQP connection.")
     self.storm_connection = amqpstorm.Connection(
         hostname=self.config['host'].split(":")[0],
         username=self.config['userid'],
         password=self.config['password'],
         port=int(self.config['host'].split(":")[1]),
         virtual_host=self.config['virtual_host'],
         heartbeat=self.config['heartbeat'],
         timeout=self.config['socket_timeout'],
         ssl=True,
         ssl_options={
             'ca_certs': self.config['sslopts']['ca_certs'],
             'certfile': self.config['sslopts']['certfile'],
             'keyfile': self.config['sslopts']['keyfile'],
         })
Beispiel #23
0
    def test_channel_basic_return_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        channel = Channel(0, connection, rpc_timeout=360)

        channel.on_frame(
            specification.Basic.Return(reply_code=500,
                                       reply_text='test',
                                       exchange='exchange',
                                       routing_key='routing_key'))

        self.assertEqual(
            str(channel.exceptions[0]),
            "Message not delivered: test (500) to queue "
            "'routing_key' from exchange 'exchange'")
Beispiel #24
0
    def test_channel0_on_close_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        connection.set_state(connection.OPEN)
        channel = Channel0(connection)

        self.assertFalse(connection.exceptions)

        channel.on_frame(Connection.Close())

        self.assertTrue(connection.exceptions)
        self.assertTrue(connection.is_closed)
        self.assertRaisesRegexp(AMQPConnectionError,
                                'Connection was closed by remote server: ',
                                connection.check_for_errors)
 def channel_publish(self, callback_data):
     try:
         self.channel.basic.publish(body=json.dumps(callback_data),
                                    routing_key=self.conf['RABBITMQ_QUEUE'],
                                    exchange='')
     except AMQPConnectionError as e:
         self.connection = amqpstorm.Connection(
             hostname=self.conf['RABBITMQ_HOST'],
             username=self.conf['RABBITMQ_USER'],
             password=self.conf['RABBITMQ_PASSWORD'],
             virtual_host=self.conf['RABBITMQ_VHOST'],
             port=self.conf['RABBITMQ_PORT'])
         self.channel = self.connection.channel()
         self.channel.queue.declare(queue=self.conf['RABBITMQ_QUEUE'])
         logger.error(e)
         self.channel.basic.publish(body=json.dumps(callback_data),
                                    routing_key=self.conf['RABBITMQ_QUEUE'],
                                    exchange='')
Beispiel #26
0
    def test_channel_consume_exception_when_recoverable(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        connection.set_state(connection.OPEN)
        channel = Channel(0, connection, 360)
        channel.set_state(channel.OPEN)
        channel.exceptions.append(AMQPChannelError('no-route'))

        self.assertTrue(connection.is_open)
        self.assertTrue(channel.is_open)

        self.assertRaisesRegexp(exception.AMQPChannelError, 'no-route',
                                channel.check_for_errors)

        self.assertTrue(channel.is_open)

        channel.check_for_errors()
    def handle_start(self, app):
        config = {
            self.CONFIG_TRANSLATIONS.get(k, k): v
            for k, v in self.plugin_config.items()
        }

        uri = config.pop('uri')
        if uri:
            uri = urlparse.urlparse(uri)
            if uri.scheme == 'amqp':
                config.update({
                    'hostname': uri.hostname or '127.0.0.1',
                    'port': uri.port or 5672,
                    'username': uri.username or 'guest',
                    'password': uri.password or 'guest',
                    'virtual_host': uri.path or '/'
                })

        self.connection = amqpstorm.Connection(**config)
    def test_channel_basic_return_frame(self):
        connection = amqpstorm.Connection('localhost',
                                          'guest',
                                          'guest',
                                          lazy=True)
        connection.set_state(connection.OPEN)
        channel = Channel(0, connection, rpc_timeout=1)
        channel.set_state(channel.OPEN)

        channel.on_frame(
            specification.Basic.Return(reply_code=500,
                                       reply_text='travis-ci',
                                       exchange='exchange',
                                       routing_key='routing_key'))

        self.assertRaisesRegexp(
            AMQPMessageError,
            "Message not delivered: travis-ci \(500\) to queue "
            "'routing_key' from exchange 'exchange'", channel.check_for_errors)
 def __init__(self):
     """
     RABBITMQ_HOST: 127.0.0.1
     RABBITMQ_USER: guest
     RABBITMQ_PASSWORD: guest
     RABBITMQ_PORT: 5672
     RABBITMQ_VHOST: /
     RABBITMQ_SIZE: 100
     RABBITMQ_PREFETCH_COUNT: 1
     RABBITMQ_QUEUE: async_send_queue
     """
     self.conf = self.load_yaml_conf()
     self.connection = amqpstorm.Connection(
         hostname=self.conf['RABBITMQ_HOST'],
         username=self.conf['RABBITMQ_USER'],
         password=self.conf['RABBITMQ_PASSWORD'],
         virtual_host=self.conf['RABBITMQ_VHOST'],
         port=self.conf['RABBITMQ_PORT'])
     self.channel = self.connection.channel()
     self.channel.queue.declare(queue=self.conf['RABBITMQ_QUEUE'])
"""
RPC Server example based on code from the official RabbitMQ Tutorial.
http://www.rabbitmq.com/tutorials/tutorial-six-python.html
"""
import amqpstorm

from amqpstorm import Message

CONNECTION = amqpstorm.Connection('127.0.0.1', 'guest', 'guest')
CHANNEL = CONNECTION.channel()
CHANNEL.queue.declare(queue='rpc_queue')


def fib(number):
    if number == 0:
        return 0
    elif number == 1:
        return 1
    else:
        return fib(number - 1) + fib(number - 2)


def on_request(message):
    number = int(message.body)

    print(" [.] fib(%s)" % (number, ))

    response = str(fib(number))

    properties = {'correlation_id': message.correlation_id}