Repository: microsoft/nni
File: nni/tools/nnictl/nnictl_utils.py (Python)

Code before:
    running, _ = check_rest_server_quick(rest_port)
    if running:
        print_normal('Restful server is running...')
    else:
        print_normal('Restful server is not running...')
    return running

def stop_experiment(args):
    '''Stop the experiment which is running'''
    if args.id and args.id == 'all':
        print_warning('\'nnictl stop all\' is abolished, please use \'nnictl stop --all\' to stop all of experiments!')
        exit(1)
    experiment_id_list = parse_ids(args)
    if experiment_id_list:
        for experiment_id in experiment_id_list:
            print_normal('Stopping experiment %s' % experiment_id)
            # retry up to 10 times to get the experiment metadata
            for i in range(1, 11):
                experiments_dict = Experiments().get_all_experiments()
                experiment_info = experiments_dict.get(experiment_id)
                if experiment_info is None:
                    print_warning('Get experiment {} metadata failed, {} time retry...'.format(experiment_id, i))
                else:
                    break
            if experiment_info is None:
                print_error('Experiment {} metadata getting failed, please manually check its status in `~/nni-experiments/.experiment`.')
                exit(1)
            rest_pid = experiment_info.get('pid')
            if rest_pid:
                kill_command(rest_pid)
            print_normal('Stop experiment success.')

def trial_ls(args):
    '''List trial'''
    def final_metric_data_cmp(lhs, rhs):
        metric_l = json.loads(json.loads(lhs['finalMetricData'][0]['data']))
        metric_r = json.loads(json.loads(rhs['finalMetricData'][0]['data']))
        if isinstance(metric_l, float):
            return metric_l - metric_r
        elif isinstance(metric_l, dict):
            return metric_l['default'] - metric_r['default']
        else:
            print_error('Unexpected data format. Please check your data.')
            raise ValueError

    if args.head and args.tail:
        print_error('Head and tail cannot be set at the same time.')
        return
    experiments_config = Experiments()
    experiments_dict = experiments_config.get_all_experiments()
    experiment_id = get_config_filename(args)

Review comment (bug):
Suggest putting debugging info here so that we can do a diagnosis when similar issues appears on pipeline.

Code after:
    running, _ = check_rest_server_quick(rest_port)
    if running:
        print_normal('Restful server is running...')
    else:
        print_normal('Restful server is not running...')
    return running

def stop_experiment(args):
    '''Stop the experiment which is running'''
    if args.id and args.id == 'all':
        print_warning('\'nnictl stop all\' is abolished, please use \'nnictl stop --all\' to stop all of experiments!')
        exit(1)
    experiment_id_list = parse_ids(args)
    if experiment_id_list:
        for experiment_id in experiment_id_list:
            print_normal('Stopping experiment %s' % experiment_id)
            # FIXME: Retry should be placed to `Experiments`, need review both python and ts code.
            # retry up to 10 times to get the experiment metadata
            for i in range(1, 11):
                experiments_dict = Experiments().get_all_experiments()
                experiment_info = experiments_dict.get(experiment_id)
                if experiment_info is None:
                    print_warning('Get experiment {} metadata failed, {} time retry...'.format(experiment_id, i))
                    time.sleep(0.5)
                else:
                    break
            if experiment_info is None:
                print_error('Experiment {} metadata getting failed.'.format(experiment_id))
                print_error('The experiments metadata in `.experiment` is:')
                print_error(json.dumps(Experiments().get_all_experiments(), indent=4))
                exit(1)
            rest_pid = experiment_info.get('pid')
            if rest_pid:
                kill_command(rest_pid)
            print_normal('Stop experiment success.')

def trial_ls(args):
    '''List trial'''
    def final_metric_data_cmp(lhs, rhs):
        metric_l = json.loads(json.loads(lhs['finalMetricData'][0]['data']))
        metric_r = json.loads(json.loads(rhs['finalMetricData'][0]['data']))
        if isinstance(metric_l, float):
            return metric_l - metric_r
        elif isinstance(metric_l, dict):
            return metric_l['default'] - metric_r['default']
        else:
            print_error('Unexpected data format. Please check your data.')
            raise ValueError

    if args.head and args.tail:
        print_error('Head and tail cannot be set at the same time.')