LOCAL.pm 3.06 KB
Newer Older
1 2 3 4 5 6 7
# This is the 'Local' implementation of Meadow

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

use strict;
use Sys::Hostname;

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

10 11 12 13 14 15

sub available {     # always invoked as a class method

    return 1;       # for the moment assume LOCAL meadow is always available
}

16

17 18 19 20 21 22
sub name {

    return hostname();
}


23 24 25 26 27 28
sub total_running_workers_default_max {

    return 2;
}


29 30 31 32 33 34
sub get_current_worker_process_id {
    my ($self) = @_;

    return $$;
}

35

36 37 38
sub count_running_workers {
    my $self = shift @_;

Leo Gordon's avatar
Leo Gordon committed
39
    my $cmd = 'ps x | grep runWorker.pl | grep -v "grep runWorker.pl" | wc -l';
40 41 42 43 44 45 46
    my $run_count = qx/$cmd/;
    chomp($run_count);

    return $run_count;
}


47
sub status_of_all_our_workers { # returns a hashref
48 49
    my ($self) = @_;

Leo Gordon's avatar
Leo Gordon committed
50
    my $cmd = 'ps x -o state,pid,command -w -w | grep runWorker.pl | grep -v "grep runWorker.pl" ';
51 52 53 54 55 56 57 58 59 60 61

        # 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);

62 63 64 65 66 67 68 69 70 71 72
        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
73 74 75 76 77 78 79 80

        # 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;
}

81

82
sub check_worker_is_alive_and_mine {
83 84
    my ($self, $worker) = @_;

85 86 87 88 89
    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;
90 91
}

92

93 94 95 96
sub kill_worker {
    my ($self, $worker) = @_;

    if( $self->responsible_for_worker($worker) ) {
97
        if($self->check_worker_is_alive_and_mine($worker)) {
98 99 100 101 102 103 104 105 106 107
            my $cmd = 'kill -9 '.$worker->process_id();
            system($cmd);
        } else {
            warn 'Cannot kill worker '.$worker->process_id().' because it is not running';
        }
    } else {
        warn 'Cannot kill worker '.$worker->process_id().'@'.$worker->host.' it is probably running on a different host';
    }
}

108

109
sub submit_workers {
Leo Gordon's avatar
Leo Gordon committed
110
    my ($self, $iteration, $worker_cmd, $worker_count, $rc_id, $rc_parameters) = @_;
111

Leo Gordon's avatar
Leo Gordon committed
112
    my $cmd = "$worker_cmd -rc_id $rc_id &";    # $rc_parameters are ignored for the time being
113 114 115 116 117 118 119 120

    foreach (1..$worker_count) {
        print "SUBMITTING_CMD:\t\t$cmd\n";
        system($cmd);
    }
}

1;