def _init(self): log.info("Initializing") # Set confirm buffer to 1 for heartbeat messages self.client = AMQPClient(logger=log_amqp, confirm_buffer=1) # Connect to AMQP host self.client.connect() # Declare queues and exchanges exchange = settings.EXCHANGE_GANETI exchange_dl = queues.convert_exchange_to_dead(exchange) self.client.exchange_declare(exchange=exchange, type="topic") self.client.exchange_declare(exchange=exchange_dl, type="topic") for queue in queues.QUEUES: # Queues are mirrored to all RabbitMQ brokers self.client.queue_declare(queue=queue, mirrored=True, dead_letter_exchange=exchange_dl) # Declare the corresponding dead-letter queue queue_dl = queues.convert_queue_to_dead(queue) self.client.queue_declare(queue=queue_dl, mirrored=True) # Bind queues to handler methods for binding in queues.BINDINGS: try: callback = getattr(callbacks, binding[3]) except AttributeError: log.error("Cannot find callback %s", binding[3]) raise SystemExit(1) queue = binding[0] exchange = binding[1] routing_key = binding[2] self.client.queue_bind(queue=queue, exchange=exchange, routing_key=routing_key) self.client.basic_consume(queue=binding[0], callback=callback, prefetch_count=5) queue_dl = queues.convert_queue_to_dead(queue) exchange_dl = queues.convert_exchange_to_dead(exchange) # Bind the corresponding dead-letter queue self.client.queue_bind(queue=queue_dl, exchange=exchange_dl, routing_key=routing_key) log.debug("Binding %s(%s) to queue %s with handler %s", exchange, routing_key, queue, binding[3]) # Declare the queue that will be used for receiving requests, e.g. a # status check request hostname, pid = get_hostname(), os.getpid() queue = queues.get_dispatcher_request_queue(hostname, pid) self.client.queue_declare(queue=queue, mirrored=True, ttl=REQUEST_QUEUE_TTL) self.client.basic_consume(queue=queue, callback=handle_request) log.debug("Binding %s(%s) to queue %s with handler 'hadle_request'", exchange, routing_key, queue)
def _init(self): log.info("Initializing") self.client = AMQPClient(logger=log_amqp) # Connect to AMQP host self.client.connect() # Declare queues and exchanges exchange = settings.EXCHANGE_GANETI exchange_dl = queues.convert_exchange_to_dead(exchange) self.client.exchange_declare(exchange=exchange, type="topic") self.client.exchange_declare(exchange=exchange_dl, type="topic") for queue in queues.QUEUES: # Queues are mirrored to all RabbitMQ brokers self.client.queue_declare(queue=queue, mirrored=True, dead_letter_exchange=exchange_dl) # Declare the corresponding dead-letter queue queue_dl = queues.convert_queue_to_dead(queue) self.client.queue_declare(queue=queue_dl, mirrored=True) # Bind queues to handler methods for binding in queues.BINDINGS: try: callback = getattr(callbacks, binding[3]) except AttributeError: log.error("Cannot find callback %s", binding[3]) raise SystemExit(1) queue = binding[0] exchange = binding[1] routing_key = binding[2] self.client.queue_bind(queue=queue, exchange=exchange, routing_key=routing_key) self.client.basic_consume(queue=binding[0], callback=callback, prefetch_count=5) queue_dl = queues.convert_queue_to_dead(queue) exchange_dl = queues.convert_exchange_to_dead(exchange) # Bind the corresponding dead-letter queue self.client.queue_bind(queue=queue_dl, exchange=exchange_dl, routing_key=routing_key) log.debug("Binding %s(%s) to queue %s with handler %s", exchange, routing_key, queue, binding[3])
def handle(self, *args, **options): verbose = (options["verbosity"] == "2") self.keep_zombies = options["keep_zombies"] log_level = logging.DEBUG if verbose else logging.WARNING log.setLevel(log_level) client = AMQPClient(confirms=False) client.connect() self.client = client for queue in queues.QUEUES: dead_queue = queues.convert_queue_to_dead(queue) while 1: message = client.basic_get(dead_queue) if not message: break log.debug("Received message %s", message) self.handle_message(message) client.close() return 0
def test_convert_queue_to_dead(self): converted = queues.convert_queue_to_dead(self.queue) self.assertEqual(self.queue + '-dl', converted)