def test_get_connection(rabbit_config): amqp_uri = rabbit_config['AMQP_URI'] connection_ids = [] with get_connection(amqp_uri) as connection: connection_ids.append(id(connection)) assert isinstance(connection, Connection) with get_connection(amqp_uri) as connection: connection_ids.append(id(connection)) assert len(set(connection_ids)) == 1
def setup(self): exchange = self.exchange queue = self.queue verify_amqp_uri(self.amqp_uri) with get_connection(self.amqp_uri) as conn: if queue is not None: maybe_declare(queue, conn) elif exchange is not None: maybe_declare(exchange, conn)
def get(queue_name, ack=True, block=True, timeout=1, accept=None): with get_connection(amqp_uri) as conn: queue = conn.SimpleQueue(queue_name) # queue doesn't allow passing of `accept` to its consumer, # so we patch it on after instantiation if accept is not None: queue.consumer.accept = accept message = queue.get(block=block, timeout=timeout) if ack: message.ack() else: message.requeue() queue.close() return message
def setup(self): super(ReplyConsumer, self).setup() config = self.container.config """Declare consumer queue for this service in current region""" self.queue = Queue( exchange=orders_exchange, routing_key='{}_{}'.format(config['REGION'], ROUTING_KEY_CALCULATE_TAXES), name='fed.{}_{}'.format(config['REGION'], ROUTING_KEY_CALCULATE_TAXES)) """Bind federated queues in all regions to `orders` exchange with correct routing key. """ with get_connection(config[AMQP_URI_CONFIG_KEY]) as connection: maybe_declare(orders_exchange, connection) self._bind_queues_in_for_all_regions(ROUTING_KEY_CALCULATE_TAXES, connection) self._bind_queues_in_for_all_regions( ROUTING_KEY_CALCULATE_TAXES_REPLY, connection)
def dispatch(service_name, event_type, event_data): """ Dispatch an event claiming to originate from `service_name` with the given `event_type` and `event_data`. """ serializer = nameko_config.get(SERIALIZER_CONFIG_KEY, DEFAULT_SERIALIZER) exchange = get_event_exchange(service_name) with get_connection(amqp_uri) as connection: exchange.maybe_bind(connection) # TODO: reqd? maybe_declare? with get_producer(amqp_uri, use_confirms) as producer: msg = event_data routing_key = event_type producer.publish(msg, exchange=exchange, serializer=serializer, routing_key=routing_key, retry=retry, retry_policy=retry_policy, mandatory=mandatory, **kwargs) if mandatory: if not use_confirms: warnings.warn( "Mandatory delivery was requested, but " "unroutable messages cannot be detected without " "publish confirms enabled.") try: returned_messages = producer.channel.returned_messages returned = returned_messages.get_nowait() except queue.Empty: pass else: raise UndeliverableMessage(returned)
def _create_exchange(self) -> None: with get_connection(self._dispatcher.amqp_uri) as connection: exchange = get_event_exchange(self._name) exchange.maybe_bind(connection) exchange.declare()