def main(): if len(sys.argv) < 4: print_help() return address = sys.argv[1] exchange_name = sys.argv[2] message = sys.argv[3] connection = kombu.BrokerConnection(address) exchange = kombu.Exchange(exchange_name) channel = connection.channel() producer = kombu.Producer(channel, exchange=exchange, serializer="json") producer.publish(message) producer.close() connection.close()
def _declare_queue(target): connection = kombu.connection.BrokerConnection(transport='memory') # Kludge to speed up tests. connection.transport.polling_interval = 0.0 connection.connect() channel = connection.channel() # work around 'memory' transport bug in 1.1.3 channel._new_queue('ae.undeliver') if target.fanout: exchange = kombu.entity.Exchange(name=target.topic + '_fanout', type='fanout', durable=False, auto_delete=True) queue = kombu.entity.Queue(name=target.topic + '_fanout_12345', channel=channel, exchange=exchange, routing_key=target.topic) elif target.server: exchange = kombu.entity.Exchange(name='openstack', type='topic', durable=False, auto_delete=False) topic = '%s.%s' % (target.topic, target.server) queue = kombu.entity.Queue(name=topic, channel=channel, exchange=exchange, routing_key=topic) else: exchange = kombu.entity.Exchange(name='openstack', type='topic', durable=False, auto_delete=False) queue = kombu.entity.Queue(name=target.topic, channel=channel, exchange=exchange, routing_key=target.topic) queue.declare() return connection, channel, queue
def __init__(self, name, connection, deployment, durable): self.connection = connection self.deployment = deployment self.durable = durable self.name = name self.last_time = None self.pmi = None self.processed = 0 self.total_processed = 0 self.channel = connection.channel() self.nova_exchange = kombu.entity.Exchange("nova", type="topic", exclusive=False, durable=self.durable, auto_delete=False) self.nova_queues = [ kombu.Queue("stacktash.notifications.info", self.nova_exchange, durable=self.durable, auto_delete=False, exclusive=False, routing_key='notifications.info'), kombu.Queue("stacktash.notifications.error", self.nova_exchange, durable=self.durable, auto_delete=False, exclusive=False, routing_key='notifications.error'), ]
def listen(host_id, amqp_url, notifications_exchange_name, rpc_exchange_name, notification_queue): """Listen for messages from AMQP and deliver them to the in-process queue provided. """ LOG.debug('%s starting to listen on %s', host_id, amqp_url) conn_info = urlparse.urlparse(amqp_url) connection = kombu.connection.BrokerConnection( hostname=conn_info.hostname, userid=conn_info.username, password=conn_info.password, virtual_host=conn_info.path, port=conn_info.port, ) try: connection.ensure_connection( errback=_handle_connection_error, **_kombu_configuration(cfg)) except (connection.connection_errors): LOG.exception('Error establishing connection, ' 'shutting down...') raise RuntimeError("Error establishing connection to broker.") channel = connection.channel() # The notifications coming from quantum/neutron. notifications_exchange = kombu.entity.Exchange( name=notifications_exchange_name, type='topic', durable=False, auto_delete=False, internal=False, channel=channel, ) # The RPC instructions coming from quantum/neutron. agent_exchange = kombu.entity.Exchange( name=rpc_exchange_name, type='fanout', durable=False, auto_delete=True, internal=False, channel=channel, ) queues = [ kombu.entity.Queue( 'akanda.notifications', exchange=notifications_exchange, routing_key='notifications.*', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.l3_agent', exchange=agent_exchange, routing_key='l3_agent', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.l3_agent.' + host_id, exchange=agent_exchange, routing_key='l3_agent.' + host_id, channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.dhcp_agent', exchange=agent_exchange, routing_key='dhcp_agent', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.dhcp_agent.' + host_id, exchange=agent_exchange, routing_key='dhcp_agent.' + host_id, channel=channel, durable=False, auto_delete=False, ), ] for q in queues: LOG.debug('setting up queue %s', q) q.declare() def _process_message(body, message): "Send the message through the notification queue" # LOG.debug('received %r', body) # TODO: # 1. Ignore notification messages that we don't care about. # 2. Convert notification and rpc messages to a common format # so the lower layer does not have to understand both try: event = _make_event_from_message(body) if event: LOG.debug('received message for %s', event.tenant_id) notification_queue.put((event.tenant_id, event)) except: LOG.exception('could not process message: %s' % unicode(body)) message.reject() else: message.ack() consumer = kombu.messaging.Consumer(channel, queues) consumer.register_callback(_process_message) consumer.consume() while True: try: connection.drain_events() except (KeyboardInterrupt, SystemExit): LOG.info('Caught exit signal, exiting...') break except socket.timeout: LOG.info('Socket connection timed out, retrying connection') try: connection.ensure_connection(errback=_handle_connection_error, **_kombu_configuration(cfg)) except (connection.connection_errors): LOG.exception('Unable to re-establish connection, ' 'shutting down...') break else: continue except: LOG.exception('Unhandled exception while draining events from ' 'queue') time.sleep(1) connection.release()
def listen(host_id, amqp_url, notifications_exchange_name, rpc_exchange_name, notification_queue): """Listen for messages from AMQP and deliver them to the in-process queue provided. """ LOG.debug('%s starting to listen on %s', host_id, amqp_url) conn_info = urlparse.urlparse(amqp_url) connection = kombu.connection.BrokerConnection( hostname=conn_info.hostname, userid=conn_info.username, password=conn_info.password, virtual_host=conn_info.path, port=conn_info.port, ) try: connection.ensure_connection(errback=_handle_connection_error, **_kombu_configuration(cfg)) except (connection.connection_errors): LOG.exception('Error establishing connection, ' 'shutting down...') raise RuntimeError("Error establishing connection to broker.") channel = connection.channel() # The notifications coming from quantum/neutron. notifications_exchange = kombu.entity.Exchange( name=notifications_exchange_name, type='topic', durable=False, auto_delete=False, internal=False, channel=channel, ) # The RPC instructions coming from quantum/neutron. agent_exchange = kombu.entity.Exchange( name=rpc_exchange_name, type='fanout', durable=False, auto_delete=True, internal=False, channel=channel, ) queues = [ kombu.entity.Queue( 'akanda.notifications', exchange=notifications_exchange, routing_key='notifications.*', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.l3_agent', exchange=agent_exchange, routing_key='l3_agent', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.l3_agent.' + host_id, exchange=agent_exchange, routing_key='l3_agent.' + host_id, channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.dhcp_agent', exchange=agent_exchange, routing_key='dhcp_agent', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.dhcp_agent.' + host_id, exchange=agent_exchange, routing_key='dhcp_agent.' + host_id, channel=channel, durable=False, auto_delete=False, ), ] for q in queues: LOG.debug('setting up queue %s', q) q.declare() def _process_message(body, message): "Send the message through the notification queue" # LOG.debug('received %r', body) # TODO: # 1. Ignore notification messages that we don't care about. # 2. Convert notification and rpc messages to a common format # so the lower layer does not have to understand both try: event = _make_event_from_message(body) if event: LOG.debug('received message for %s', event.tenant_id) notification_queue.put((event.tenant_id, event)) except: LOG.exception('could not process message: %s' % unicode(body)) message.reject() else: message.ack() consumer = kombu.messaging.Consumer(channel, queues) consumer.register_callback(_process_message) consumer.consume() while True: try: connection.drain_events() except (KeyboardInterrupt, SystemExit): LOG.info('Caught exit signal, exiting...') break except socket.timeout: LOG.info('Socket connection timed out, retrying connection') try: connection.ensure_connection(errback=_handle_connection_error, **_kombu_configuration(cfg)) except (connection.connection_errors): LOG.exception('Unable to re-establish connection, ' 'shutting down...') break else: continue except: LOG.exception('Unhandled exception while draining events from ' 'queue') time.sleep(1) connection.release()
def listen(host_id, amqp_url, notifications_exchange_name, rpc_exchange_name, notification_queue): """Listen for messages from AMQP and deliver them to the in-process queue provided. """ LOG.debug('%s starting to listen on %s', host_id, amqp_url) conn_info = urlparse.urlparse(amqp_url) connection = kombu.connection.BrokerConnection( hostname=conn_info.hostname, userid=conn_info.username, password=conn_info.password, virtual_host=conn_info.path, port=conn_info.port, ) connection.connect() channel = connection.channel() # The notifications coming from quantum/neutron. notifications_exchange = kombu.entity.Exchange( name=notifications_exchange_name, type='topic', durable=False, auto_delete=False, internal=False, channel=channel, ) # The RPC instructions coming from quantum/neutron. agent_exchange = kombu.entity.Exchange( name=rpc_exchange_name, type='fanout', durable=False, auto_delete=True, internal=False, channel=channel, ) queues = [ kombu.entity.Queue( 'akanda.notifications', exchange=notifications_exchange, routing_key='notifications.*', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.l3_agent', exchange=agent_exchange, routing_key='l3_agent', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.l3_agent.' + host_id, exchange=agent_exchange, routing_key='l3_agent.' + host_id, channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.dhcp_agent', exchange=agent_exchange, routing_key='dhcp_agent', channel=channel, durable=False, auto_delete=False, ), kombu.entity.Queue( 'akanda.dhcp_agent.' + host_id, exchange=agent_exchange, routing_key='dhcp_agent.' + host_id, channel=channel, durable=False, auto_delete=False, ), ] for q in queues: LOG.debug('setting up queue %s', q) q.declare() def _process_message(body, message): "Send the message through the notification queue" # LOG.debug('received %r', body) # TODO: # 1. Ignore notification messages that we don't care about. # 2. Convert notification and rpc messages to a common format # so the lower layer does not have to understand both try: event = _make_event_from_message(body) if event: LOG.debug('received message for %s', event.tenant_id) notification_queue.put((event.tenant_id, event)) except: LOG.exception('could not process message: %s' % unicode(body)) message.reject() else: message.ack() consumer = kombu.messaging.Consumer(channel, queues) consumer.register_callback(_process_message) consumer.consume() while True: try: connection.drain_events() except: # noqa LOG.exception('exception while draining events from queue') time.sleep(1) # FIXME(dhellmann): Make this function a class so we can # control the loop variable and stop draining events # before sending the shutdown to the workers. connection.release()