def initializeInstance(self, instance_id): """Initializes an instance. :param instance_id: The ID of the instance :return: QueueInformation object describing message queue for this system """ instance = self._get_instance(instance_id) system = self._get_system(instance) self.logger.info( "Initializing instance %s[%s]-%s", system.name, instance.name, system.version, ) routing_words = [system.name, system.version, instance.name] req_name = get_routing_key(*routing_words) req_args = {"durable": True, "arguments": {"x-max-priority": 1}} req_queue = self.clients["pika"].setup_queue(req_name, req_args, [req_name]) routing_words.append("".join( random.choice(string.ascii_lowercase + string.digits) for _ in range(10))) admin_keys = get_routing_keys(*routing_words, is_admin=True) admin_args = {"durable": True} admin_queue = self.clients["pika"].setup_queue(admin_keys[-1], admin_args, admin_keys) connection = { "host": bartender.config.publish_hostname, "port": bartender.config.amq.connections.message.port, "user": bartender.config.amq.connections.message.user, "password": bartender.config.amq.connections.message.password, "virtual_host": bartender.config.amq.virtual_host, "ssl": { "enabled": bartender.config.amq.connections.message.ssl.enabled }, } instance.status = "INITIALIZING" instance.status_info = StatusInfo(heartbeat=datetime.utcnow()) instance.queue_type = "rabbitmq" instance.queue_info = { "admin": admin_queue, "request": req_queue, "connection": connection, "url": self.clients["public"].connection_url, } instance.save() # Send a request to start to the plugin on the plugin's admin queue self.clients["pika"].start(system=system.name, version=system.version, instance=instance.name) return self.parser.serialize_instance(instance, to_string=True)
def publish_request(self, request, **kwargs): if "headers" not in kwargs: kwargs["headers"] = {} kwargs["headers"]["request_id"] = str(request.id) if "routing_key" not in kwargs: kwargs["routing_key"] = get_routing_key(request.system, request.system_version, request.instance_name) return self.publish(SchemaParser.serialize_request(request), **kwargs)
def clearAllQueues(self): """Clears all queues that Bartender knows about. :return: None """ self.logger.debug("Clearing all queues") systems = System.objects.all() for system in systems: for instance in system.instances: routing_key = get_routing_key(system.name, system.version, instance.name) self.clearQueue(routing_key)
def _event_publish_args(self, event, **kwargs): # Main thing we need to do here is figure out the appropriate routing key args = {} if event.metadata and "routing_key" in event.metadata: args["routing_key"] = event.metadata["routing_key"] elif "request" in kwargs: request = kwargs["request"] args["routing_key"] = get_routing_key("request", request.system, request.system_version, request.instance_name) else: args["routing_key"] = "beergarden" return args
def getQueueInfo(self, system_name, system_version, instance_name): """Gets the size of a queue :param system_name: The system name :param system_version: The system version :param instance_name: The instance name :return size of the queue :raises Exception: If queue does not exist """ routing_key = get_routing_key(system_name, system_version, instance_name) self.logger.debug("Get the queue state for %s", routing_key) return bg_utils.bg_thrift.QueueInfo( routing_key, self.clients["pyrabbit"].get_queue_size(routing_key))
def _event_publish_args(self, event, **kwargs): # Main thing we need to do here is figure out the appropriate routing key args = {} if event.metadata and 'routing_key' in event.metadata: args['routing_key'] = event.metadata['routing_key'] elif 'request' in kwargs: request = kwargs['request'] args['routing_key'] = get_routing_key('request', request.system, request.system_version, request.instance_name) else: args['routing_key'] = 'beergarden' return args
def stop(self, system=None, version=None, instance=None, clone_id=None): self.publish_request( Request( system=system, system_version=version, instance_name=instance, command="_stop", command_type="EPHEMERAL", parameters={}, ), routing_key=get_routing_key(system, version, instance, clone_id, is_admin=True), )
def test_get_routing_key(self): assert "system.1-0-0.instance" == get_routing_key("system", "1.0.0", "instance")