LOCAL.pm 4.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
=pod 

=head1 NAME

    Bio::EnsEMBL::Hive::Meadow::LOCAL

=head1 DESCRIPTION

    This is the 'Local' implementation of Meadow

=head1 LICENSE

13
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Matthieu Muffato's avatar
Matthieu Muffato committed
14
    Copyright [2016-2018] EMBL-European Bioinformatics Institute
15 16 17 18 19 20 21 22 23 24 25 26

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

=head1 CONTACT

27
    Please subscribe to the Hive mailing list:  http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users  to discuss Hive-related questions or to be notified of our updates
28 29 30

=cut

31 32 33 34 35 36

package Bio::EnsEMBL::Hive::Meadow::LOCAL;

use strict;
use Sys::Hostname;

Leo Gordon's avatar
Leo Gordon committed
37
use base ('Bio::EnsEMBL::Hive::Meadow');
38

39

40 41 42 43 44
our $VERSION = '1.0';       # Semantic version of the Meadow interface:
                            #   change the Major version whenever an incompatible change is introduced,
                            #   change the Minor version whenever the interface is extended, but compatibility is retained.


45
sub name {  # also called to check for availability; for the moment assume LOCAL meadow is always available
46

47
    return (split(/\./, hostname))[0];     # only take the first name
48 49 50
}


51 52 53 54 55 56
sub get_current_worker_process_id {
    my ($self) = @_;

    return $$;
}

57

58 59 60 61 62 63 64
sub count_pending_workers_by_rc_name {
    my ($self) = @_;

    return ({}, 0);     # LOCAL has no concept of pending workers
}


65 66 67 68
sub _command_line_to_extract_all_running_workers {
    my ($self) = @_;

        # Make sure we have excluded both 'awk' itself and commands like "less runWorker.pl" :
69
    return q{ps x -o state,pid,command -w -w | awk '(/runWorker.pl/ && ($3 ~ /perl[[:digit:].]*$/) )'};
70 71 72
}


73 74 75
sub count_running_workers {
    my $self = shift @_;

76
    my $cmd = $self->_command_line_to_extract_all_running_workers . ' | wc -l';
77 78 79 80 81 82 83
    my $run_count = qx/$cmd/;
    chomp($run_count);

    return $run_count;
}


84
sub status_of_all_our_workers { # returns a hashref
85 86
    my ($self) = @_;

87
    my $cmd = $self->_command_line_to_extract_all_running_workers;
88 89 90 91 92 93 94 95 96 97 98

        # FIXME: if we want to incorporate Meadow->pipeline_name() filtering here,
        #        a dummy parameter to the runWorker.pl should probably be introduced
        #        for 'ps' to be able to externally differentiate between local workers
        #        working for different hives
        #        (but at the moment such a feature is unlikely to be be in demand).

    my %status_hash = ();
    foreach my $line (`$cmd`) {
        my ($pre_status, $worker_pid, $job_name) = split(/\s+/, $line);

99 100 101 102 103 104 105 106 107 108 109
        my $status = {
            'R' => 'RUN',   # running

            'S' => 'RUN',   # sleeping (sleeping for less than 20 sec on a Mac)
            'I' => 'RUN',   # Mac: idle (sleeping for more than 20 sec)

            'D' => 'RUN',   # Linux: uninterruptible sleep, usually IO
            'U' => 'RUN',   # Mac: uninterruptible wait

            'T' => 'SSUSP'  # stopped process
        }->{ substr($pre_status,0,1) }; # only take the first character because of Mac's additional modifiers
110 111 112 113 114 115 116 117

        # Note: you can locally 'kill -19' a worker to suspend it and 'kill -18' a worker to resume it

        $status_hash{$worker_pid} = $status;
    }
    return \%status_hash;
}

118

119
sub check_worker_is_alive_and_mine {
120 121
    my ($self, $worker) = @_;

122 123 124 125 126
    my $wpid = $worker->process_id();
    my $cmd = qq{ps x | grep $wpid | grep -v 'grep $wpid'};
    my $is_alive_and_mine = qx/$cmd/;

    return $is_alive_and_mine;
127 128
}

129

130
sub kill_worker {
131
    my $worker = pop @_;
132

133 134
    my $cmd = 'kill -9 '.$worker->process_id();
    system($cmd);
135 136
}

137

138
sub submit_workers {
139
    my ($self, $worker_cmd, $required_worker_count, $iteration, $rc_name, $rc_specific_submission_cmd_args, $submit_stdout_file, $submit_stderr_file) = @_;
140

Leo Gordon's avatar
Leo Gordon committed
141
    my $cmd = "$worker_cmd &";
142

143 144
    foreach (1..$required_worker_count) {
        print "SUBMITTING_CMD:\t\t$rc_specific_submission_cmd_args $cmd\n";
Leo Gordon's avatar
Leo Gordon committed
145
        system( $cmd );
146 147 148 149
    }
}

1;