def get_all_queue_info(): """Get queue information for all queues :return size of the queue :raises Exception: If queue does not exist """ queues = [] systems = db.query(System) for system in systems: for instance in system.instances: queue_name = get_routing_key(system.namespace, system.name, system.version, instance.name) queue = Queue( name=queue_name, system=system.name, version=system.version, instance=instance.name, system_id=str(system.id), display=system.display_name, size=-1, ) try: queue.size = get_queue_message_count(queue_name) except Exception: logger.error(f"Error getting queue size for {queue_name}") queues.append(queue) return queues
def setup_routing(): """Initialize the routing subsystem This will load the cached child garden definitions and use them to populate the two dictionaries that matter, garden_lookup and garden_connections. It will then query the database for all local systems and add those to the dictionaries as well. """ for system in db.query(System, filter_params={"local": True}): add_routing_system(system) # Don't add the local garden for garden in get_gardens(include_local=False): if garden.name != config.get("garden.name"): for system in garden.systems: add_routing_system(system=system, garden_name=garden.name) if (garden.connection_type is not None and garden.connection_type.casefold() != "local"): with garden_lock: gardens[garden.name] = garden if garden.connection_type.casefold() == "stomp": if garden.name not in stomp_garden_connections: stomp_garden_connections[ garden.name] = create_stomp_connection(garden) else: logger.warning( f"Garden with invalid connection info: {garden!r}")
def check_status(self): """Update instance status if necessary""" for system in db.query(System, filter_params={"local": True}, include_fields=["instances"]): for instance in system.instances: if self.stopped(): break last_heartbeat = instance.status_info["heartbeat"] if last_heartbeat: if (instance.status == "RUNNING" and datetime.utcnow() - last_heartbeat >= self.timeout): update( system=system, instance=instance, new_status="UNRESPONSIVE", update_heartbeat=False, ) elif (instance.status in [ "UNRESPONSIVE", "STARTING", "INITIALIZING", "UNKNOWN" ] and datetime.utcnow() - last_heartbeat < self.timeout): update( system=system, instance=instance, new_status="RUNNING", update_heartbeat=False, )
def get_commands() -> List[Command]: """Retrieve all Commands Returns: The Commands """ commands_list = [system.commands for system in db.query(System)] return list(itertools.chain.from_iterable(commands_list))
def get_requests(**kwargs) -> List[Request]: """Search for Requests Args: kwargs: Parameters to be passed to the DB query Returns: The list of Requests that matched the query """ return db.query(Request, **kwargs)
def get_systems(**kwargs) -> List[System]: """Search for Systems Keyword Args: Parameters to be passed to the DB query Returns: The list of Systems that matched the query """ return db.query(System, **kwargs)
def clear_all_queues(): """Clears all queues that Bartender knows about. :return: None """ systems = db.query(System) for system in systems: for instance in system.instances: routing_key = get_routing_key(system.namespace, system.name, system.version, instance.name) clear_queue(routing_key)
def initialize_counts(): requests = db.query( Request, filter_params={"status__in": ["CREATED", "IN_PROGRESS"]}) for request in requests: label_args = { "system": request.system, "system_version": request.system_version, "instance_name": request.instance_name, } if request.status == "CREATED": queued_request_gauge.labels(**label_args).inc() elif request.status == "IN_PROGRESS": in_progress_request_gauge.labels(**label_args).inc()
def get_request(request_id: str = None, request: Request = None) -> Request: """Retrieve an individual Request Args: request_id: The Request ID request: The Request Returns: The Request """ request = request or db.query_unique( Request, id=request_id, raise_missing=True) request.children = db.query(Request, filter_params={"parent": request}) return request
def get_gardens(include_local: bool = True) -> List[Garden]: """Retrieve list of all Gardens Args: include_local: Also include the local garden Returns: All known gardens """ gardens = db.query(Garden) if include_local: gardens += [local_garden()] return gardens
def get_gardens(include_local: bool = True) -> List[Garden]: """Retrieve list of all Gardens Args: include_local: Also include the local garden Returns: All known gardens """ # This is necessary for as long as local_garden is still needed. See the notes # there for more detail. gardens = db.query(Garden, filter_params={"connection_type__ne": "LOCAL"}) if include_local: gardens += [local_garden()] return gardens
def get_namespaces() -> List[str]: """Get the distinct namespaces in the Garden Returns: List """ namespaces = set( set(db.distinct(Request, "namespace")) | set(db.distinct(System, "namespace"))) for garden in db.query(Garden, include_fields=["namespaces"]): namespaces |= set(garden.namespaces) # Filter out None, empty string namespaces = filter(lambda x: x, namespaces) return list(namespaces)
def get_jobs(filter_params: Dict = None) -> List[Job]: return db.query(Job, filter_params=filter_params)
def initialize_from_db(self): """Initializes the watchdog scheduler from jobs stored in the database""" all_jobs = db.query(Job, filter_params={"trigger_type": "file"}) self._process_watches(all_jobs)
def get_jobs(filter_params: Optional[Dict] = None, **kwargs) -> List[Job]: return db.query(Job, filter_params=filter_params, **kwargs)