LOCAL.pm 2.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
# This is the 'Local' implementation of Meadow

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

use strict;
use Sys::Hostname;

use base 'Bio::EnsEMBL::Hive::Meadow';

sub count_running_workers {
    my $self = shift @_;

    my $cmd = 'ps -a | grep runWorker.pl | grep -v grep | wc -l';
    my $run_count = qx/$cmd/;
    chomp($run_count);

    return $run_count;
}

sub responsible_for_worker {
    my ($self, $worker) = @_;

    return ($worker->beekeeper() eq $self->type()) and ($worker->host eq hostname());
}

26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
sub status_of_all_my_workers { # returns a hashref
    my ($self) = @_;

    my $cmd = 'ps -o state,pid,cmd -w -w --no-header x | grep runWorker.pl';

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

        my $status = { 'R' => 'RUN', 'S' => 'RUN', 'D' => 'RUN', 'T' => 'SSUSP' }->{$pre_status};

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

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
sub check_worker_is_alive {
    my ($self, $worker) = @_;

    my $cmd = 'ps '. $worker->process_id . ' 2>&1 | grep ' . $worker->process_id;
    my $is_alive = qx/$cmd/;
    return $is_alive;
}

sub kill_worker {
    my ($self, $worker) = @_;

    if( $self->responsible_for_worker($worker) ) {
        if($self->check_worker_is_alive($worker)) {
            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';
    }
}

sub submit_workers {
Leo Gordon's avatar
Leo Gordon committed
74
    my ($self, $worker_cmd, $worker_count, $iteration) = @_;
75 76 77 78 79 80 81 82 83 84

    my $cmd = "$worker_cmd &";

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

1;