def update_queue_information(self, queue_data): if not 'messages' in queue_data: # FIXME: We should do something here, probably delete the queue, # as it's in a weird state. More investigation is required. # See bug 1066338. return None q_size, q_name, q_durable = (queue_data['messages'], queue_data['name'], queue_data['durable']) queue = Queue.query.filter(Queue.name == q_name).first() # If the queue doesn't exist in the db, create it. if queue is None: m = re.match('queue/([^/]+)/', q_name) logging.info("New queue '{0}' encountered. " "Adding to the database.".format(q_name)) if m: owner_name = m.group(1) owner = PulseUser.query.filter( PulseUser.username == owner_name).first() # If the queue was created by a user that isn't in the # pulseguardian database, skip the queue. if owner is None: logging.info( "Queue '{0}' owner, {1}, isn't in the db. Creating " "the user.".format(q_name, owner_name)) owner = PulseUser.new_user(owner_name) # Assign the user to the queue. logging.info("Assigning queue '{0}' to user " "{1}.".format(q_name, owner)) else: logging.warn( "'{0}' is not a standard queue name.".format(q_name)) owner = None queue = Queue(name=q_name, owner=owner) # add the queue bindings to the db. bindings = pulse_management.queue_bindings(config.rabbit_vhost, queue.name) for binding in bindings: db_binding = Binding.query.filter( Binding.exchange == binding["source"], Binding.routing_key == binding["routing_key"], Binding.queue_name == queue.name).first() if not db_binding: # need to create the binding in the DB binding = Binding(exchange=binding["source"], routing_key=binding["routing_key"], queue_name=queue.name) db_session.add(binding) # Update the saved queue size. queue.size = q_size queue.durable = q_durable db_session.add(queue) db_session.commit() return queue
def bindings_listing(queue_name): queue = Queue.query.get(queue_name) bindings = [] if queue: bindings = pulse_management.queue_bindings(vhost=config.rabbit_vhost, queue=queue.name) return jsonify({"queue_name": queue_name, "bindings": bindings})
def test_bindings(exp_routing_keys): queues = Queue.query.all() self.assertEqual(len(queues), 1) self.assertEqual(len(queues[0].bindings), len(exp_routing_keys)) db_queue = queues[0] mgmt_bindings = pulse_management.queue_bindings('/', db_queue.name) mgmt_routing_keys = {x["routing_key"] for x in mgmt_bindings} self.assertEqual(mgmt_routing_keys, exp_routing_keys) db_routing_keys = {x.routing_key for x in db_queue.bindings} self.assertEqual(db_routing_keys, exp_routing_keys)
def test_bindings(exp_routing_keys): queues = Queue.query.all() self.assertEqual(len(queues), 1) self.assertEqual(len(queues[0].bindings), len(exp_routing_keys)) db_queue = queues[0] mgmt_bindings = pulse_management.queue_bindings('/', db_queue.name) mgmt_routing_keys = {x["routing_key"] for x in mgmt_bindings} self.assertEqual(mgmt_routing_keys, exp_routing_keys) db_routing_keys = {x.routing_key for x in db_queue.bindings} self.assertEqual(db_routing_keys, exp_routing_keys)
def clear_deleted_bindings(self, queue_name): rabbit_bindings = pulse_management.queue_bindings( config.rabbit_vhost, queue_name) db_bindings = Binding.query.filter(Binding.queue_name == queue_name) # Filter bindings that are in the database but no longer on RabbitMQ. alive_bindings_names = { Binding.as_string(b['source'], b['routing_key']) for b in rabbit_bindings } deleted_bindings = { b for b in db_bindings if b.name not in alive_bindings_names } # Delete those bindings. for binding in deleted_bindings: logging.info( "Binding '{}' for queue '{}' has been deleted.".format( binding, queue_name)) db_session.delete(binding)
def bindings_listing(queue_name): queue = Queue.query.get(queue_name) bindings = [] if queue: bindings = pulse_management.queue_bindings(vhost='/', queue=queue.name) return jsonify({"queue_name": queue_name, "bindings": bindings})