def _label_from_queued_job()

in services/jenkins-autoscaling/lambda_mxnet_ci/autoscaling/handler.py [0:0]


def _label_from_queued_job(nodes, queue_item) -> Optional[str]:
    """
    Extract the node type label from a queue item. The queue item contains a reason that states why it's currently
    hanging and exposes the name of the label it's waiting for. This label is extracted by this method.
    :param queue_item: Queue item dict
    :return: Label
    """
    # Check if there are no running nodes in general. This is a special case since jenkins does not tell which
    # nodes are actually required. In that case, just assume we need a ubuntu-cpu executor.
    regex_result = re.search(RE_NO_AVAILABLE_NODES, queue_item['why'])
    if regex_result:
        logging.debug('There are no nodes on the Jenkins master, creating mxnetlinux-cpu nodes to start'
                      ' label propagation')
        label = 'mxnetlinux-cpu'
    else:
        for re_available_node in RE_NO_AVAILABLE_NODE:
            regex_result = re.search(re_available_node, queue_item['why'])
            if regex_result:
                label = regex_result.group('label')
                break
        else:
            return None

    # Clean up label of any other characters
    label = label.strip(' ‘’\'"')

    # Sometimes, Jenkins does not put the required label into the message but a node-name instead.
    # In this case, we have to extract the label from the node
    if label not in _managed_jenkins_node_labels():
        node = _find_node_by_name(nodes=nodes, name=label)
        if not node:
            logging.error("Queue reason '%s' contains unresolvable label '%s'", queue_item['why'], label)
            return None
        label = _managed_node_label(node=node)
        if not label:
            logging.error('Could not extract type label for node %s as part of queue reason "%s"',
                          node['displayName'], queue_item['why'])
            return None
    return label