<commit_before>=head1 NAME

startServers.pl

=head1 DESCRIPTION

Starts jboss servers group(domain mode).

=head1 COPYRIGHT

Copyright (c) 2014 Electric Cloud, Inc.

=cut
$[/myProject/procedure_helpers/preamble]

my $PROJECT_NAME = '$[/myProject/projectName]';
my $STATUS_STOPPED = 'STOPPED'; # stopped status for servers with auto-start true
my $STATUS_DISABLED = 'DISABLED'; # stopped status for servers with auto-start false
my $SLEEP_TIME = 5;

$|=1;


main();

sub main {
    my $jboss = EC::JBoss->new(
        project_name    =>  $PROJECT_NAME,
    );

    my $params = $jboss->get_params_as_hashref(
        'serversgroup',
        'wait_time'
    );

    my $wait_time = undef;
    $params->{wait_time} = $jboss->trim($params->{wait_time});

    if (defined $params->{wait_time} && $params->{wait_time} ne '') {
        $wait_time = $params->{wait_time};
        if ($wait_time !~ m/^\d+$/s) {
            $jboss->bail_out("Wait time should be a positive integer.");
        }
    }

    my ($servers, $states) = $jboss->get_servergroup_status($params->{serversgroup});

    if (!@$states) {
        $jboss->bail_out("Server group $params->{serversgroup} does not exist or empty.");
    }

    $jboss->{silent} = 1;
    my $servers_with_terminal_status = $jboss->is_servergroup_has_status(
        $params->{serversgroup},
        ['STOPPED']
    );
    $jboss->{silent} = 0;
    if (@$servers_with_terminal_status) {
        for my $server_record (@$servers_with_terminal_status) {
            $jboss->out_warning(
                sprintf(
                    "Server %s on %s is already in %s state",
                    $server_record->{server},
                    $server_record->{host},
                    $server_record->{status}
                )
            );
        }
    }
    my $command = sprintf '/server-group=%s:stop-servers', $params->{serversgroup};
    $jboss->out("Stopping serversgroup: $params->{serversgroup}");
    my %result = $jboss->run_command($command);
    my $done = 0;
    my $time_start = time();

    my $res = {
        error => 0,
        msg => ''
    };

    while (defined $wait_time && !$done) {
        my $time_diff = time() - $time_start;
        if ($wait_time && $time_diff >= $wait_time) {
            $done = 1;
            last;
        }
        my ($servers, $all_states_ref) = $jboss->get_servergroup_status($params->{serversgroup});

        my @stopped_states = grep { $_ eq $STATUS_STOPPED || $_ eq $STATUS_DISABLED } @$all_states_ref;
        my $all_servers_are_stopped = ( scalar @stopped_states == scalar @$all_states_ref ) ? 1 : 0;

        if ( $all_servers_are_stopped ) {
            $res->{error} = 0;
            $res->{msg} = '';
            last;
        }
        $res->{error} = 1;
        my $msg = 'Following servers are not stopped:' . "\n";
        for my $host_name ( keys %$servers ) {
            for my $server_name ( keys %{$servers->{$host_name}}) {
                $jboss->out("$server_name (host: $host_name) is $servers->{$host_name}->{$server_name}->{status}");
                # What should we set in this case?
                $msg .= "\n$server_name (host: $host_name, status: $servers->{$host_name}->{$server_name}->{status})";
            }
        }
        sleep 5;
    }
    if ($res->{error}) {<commit_msg>ECPAPPSERVERJBOSS-310 fix for "StopServers, final check is not performed if wait_time is undefined"<commit_after>=head1 NAME

startServers.pl

=head1 DESCRIPTION

Starts jboss servers group(domain mode).

=head1 COPYRIGHT

Copyright (c) 2014 Electric Cloud, Inc.

=cut
$[/myProject/procedure_helpers/preamble]

my $PROJECT_NAME = '$[/myProject/projectName]';
my $STATUS_STOPPED = 'STOPPED'; # stopped status for servers with auto-start true
my $STATUS_DISABLED = 'DISABLED'; # stopped status for servers with auto-start false
my $SLEEP_TIME = 5;

$|=1;


main();

sub main {
    my $jboss = EC::JBoss->new(
        project_name    =>  $PROJECT_NAME,
    );

    my $params = $jboss->get_params_as_hashref(
        'serversgroup',
        'wait_time'
    );

    my $wait_time = undef;
    $params->{wait_time} = $jboss->trim($params->{wait_time});

    if (defined $params->{wait_time} && $params->{wait_time} ne '') {
        $wait_time = $params->{wait_time};
        if ($wait_time !~ m/^\d+$/s) {
            $jboss->bail_out("Wait time expected to be positive integer (wait time in seconds), 0 (unlimited) or undefined (one time check).");
        }
    }

    my ($servers, $states) = $jboss->get_servergroup_status($params->{serversgroup});

    if (!@$states) {
        $jboss->bail_out("Server group $params->{serversgroup} does not exist or empty.");
    }

    $jboss->{silent} = 1;
    my $servers_with_terminal_status = $jboss->is_servergroup_has_status(
        $params->{serversgroup},
        ['STOPPED']
    );
    $jboss->{silent} = 0;
    if (@$servers_with_terminal_status) {
        for my $server_record (@$servers_with_terminal_status) {
            $jboss->out_warning(
                sprintf(
                    "Server %s on %s is already in %s state",
                    $server_record->{server},
                    $server_record->{host},
                    $server_record->{status}
                )
            );
        }
    }
    my $command = sprintf '/server-group=%s:stop-servers', $params->{serversgroup};
    $jboss->out("Stopping serversgroup: $params->{serversgroup}");
    my %result = $jboss->run_command($command);
    my $done = 0;
    my $time_start = time();

    my $res = {
        error => 0,
        msg => ''
    };

    while (!$done) {
        my $time_diff = time() - $time_start;

        if (!defined $wait_time) {
            # if wait time is undefined then we perform check only once
            $done = 1;
        }
        if ($wait_time && $time_diff >= $wait_time) {
            $done = 1;
            last;
        }
        # otherwise (wait time is 0 = unlimited wait time)

        my ($servers, $all_states_ref) = $jboss->get_servergroup_status($params->{serversgroup});

        my @stopped_states = grep { $_ eq $STATUS_STOPPED || $_ eq $STATUS_DISABLED } @$all_states_ref;
        my $all_servers_are_stopped = ( scalar @stopped_states == scalar @$all_states_ref ) ? 1 : 0;

        if ( $all_servers_are_stopped ) {
            $res->{error} = 0;
            $res->{msg} = '';
            last;
        }
        $res->{error} = 1;
        my $msg = 'Following servers are not stopped:' . "\n";
        for my $host_name ( keys %$servers ) {
            for my $server_name ( keys %{$servers->{$host_name}}) {
                $jboss->out("$server_name (host: $host_name) is $servers->{$host_name}->{$server_name}->{status}");
                # What should we set in this case?
                $msg .= "\n$server_name (host: $host_name, status: $servers->{$host_name}->{$server_name}->{status})";
            }
        }
        sleep 5;
    }
    if ($res->{error}) {
{'fraction_non_alphanumeric': 0.15415588150704632, 'fraction_numerical': 0.006327293643945931, 'mean_word_length': 2.3087535680304474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3427495', 'n_tokens_mistral': 2375, 'n_tokens_neox': 2280, 'n_words': 505}