Valley.pm 3.29 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
=pod 

=head1 NAME

  Bio::EnsEMBL::Hive::Valley

=head1 SYNOPSIS

=head1 DESCRIPTION

    A Valley represents a collection of available Meadows.

    Certain methods fit better with the concept of Valley -
    such as identifying all dead workers, or killing a particular one given worker_id.

=head1 CONTACT

  Please contact ehive-users@ebi.ac.uk mailing list with questions/suggestions.

=cut

package Bio::EnsEMBL::Hive::Valley;

use strict;
use warnings;
26
use Sys::Hostname;
27
28
29
30
31
32
33
34
35
36
use Bio::EnsEMBL::Hive::Utils ('find_submodules');


sub meadow_class_path {

    return 'Bio::EnsEMBL::Hive::Meadow';
}


sub new {
37
    my ($class, $current_meadow_type, $pipeline_name) = @_;
38
39
40

    my $self = bless {}, $class;

41
    my $amch = $self->available_meadow_hash( {} );
42
43
44
45

        # make sure modules are loaded and available ones are checked prior to setting the current one
    foreach my $meadow_class (@{ find_submodules( $self->meadow_class_path ) }) {
        eval "require $meadow_class";
46
        if($meadow_class->name) {
47
48
49
50
51
            my $meadow_object            = $meadow_class->new();

            $meadow_object->pipeline_name( $pipeline_name ) if($pipeline_name);

            $amch->{$meadow_class->type} = $meadow_object;
52
53
54
        }
    }

55
    $self->set_current_meadow_type($current_meadow_type);     # run this method even if $current_meadow_type was not specified
56
57
58
59
60

    return $self;
}


61
sub available_meadow_hash {
62
63
64
    my $self = shift @_;

    if(@_) {
65
        $self->{_available_meadow_hash} = shift @_;
66
    }   
67
    return $self->{_available_meadow_hash};
68
69
70
}


71
sub get_available_meadow_list {     # this beautiful one-liner pushes $local to the bottom of the list
72
73
74
75
    my $self = shift @_;

    my $local = $self->meadow_class_path . '::LOCAL';

76
    return [ sort { (ref($a) eq $local) or -(ref($b) eq $local) } values %{ $self->available_meadow_hash } ];
77
78
79
}


80
81
sub set_current_meadow_type {
    my ($self, $current_meadow_type) = @_;
82

83
84
    if($current_meadow_type) {
        if( my $current_meadow = $self->available_meadow_hash->{$current_meadow_type} ) {   # store if available
85
            $self->{_current_meadow} = $current_meadow;
86
        } else {
87
            die "Meadow '$current_meadow_type' does not seem to be available on this machine, please investigate";
88
        }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    } else {
        $self->{_current_meadow} = $self->get_available_meadow_list->[0];     # take the first from preference list
    }
}


sub get_current_meadow {
    my $self = shift @_;

    return $self->{_current_meadow};
}


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

105
106
    if( my $meadow = $self->available_meadow_hash->{$worker->meadow_type} ) {
        if($meadow->name eq $worker->meadow_name) {
107
108
            return $meadow;
        }
109
    }
110
    return undef;
111
112
113
}


114
sub whereami {
115
116
    my $self = shift @_;

117
    my ($meadow_type, $meadow_name, $pid);
118
    foreach my $meadow (@{ $self->get_available_meadow_list }) {
119
        eval {
120
121
122
            $pid         = $meadow->get_current_worker_process_id();
            $meadow_type = $meadow->type();
            $meadow_name = $meadow->name();
123
124
125
126
127
128
129
130
131
132
133
        };
        unless($@) {
            last;
        }
    }
    unless($pid) {
        die "Could not determine the Meadow, please investigate";
    }

    my $exechost = hostname();

134
    return ($meadow_type, $meadow_name, $pid, $exechost);
135
136
137
}


138
139
1;