RunWorker.pm 6.3 KB
Newer Older
1 2
=head1 LICENSE

3
Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
4
Copyright [2016-2017] EMBL-European Bioinformatics Institute
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

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.

=cut


package Bio::EnsEMBL::Hive::Scripts::RunWorker;

use strict;
use warnings;

Matthieu Muffato's avatar
Matthieu Muffato committed
26
use Bio::EnsEMBL::Hive::Valley;
27
use Bio::EnsEMBL::Hive::Utils::Stopwatch;
28 29

sub runWorker {
30 31
    my ($pipeline, $specialization_options, $life_options, $execution_options) = @_;

32 33 34 35
    my $worker_stopwatch = Bio::EnsEMBL::Hive::Utils::Stopwatch->new();
    $worker_stopwatch->_unit(1); # lifespan_sec is in seconds
    $worker_stopwatch->restart();

36
    my $hive_dba = $pipeline->hive_dba;
37 38 39 40 41 42 43 44 45 46

    die "Hive's DBAdaptor is not a defined Bio::EnsEMBL::Hive::DBSQL::DBAdaptor\n" unless $hive_dba and $hive_dba->isa('Bio::EnsEMBL::Hive::DBSQL::DBAdaptor');

    $specialization_options ||= {};
    $life_options ||= {};
    $execution_options ||= {};

    my $queen = $hive_dba->get_Queen();
    die "No Queen, God Bless Her\n" unless $queen and $queen->isa('Bio::EnsEMBL::Hive::Queen');

47
    if( $specialization_options->{'force_sync'} ) {       # sync the Hive in Test mode:
48
        my $list_of_analyses = $pipeline->collection_of('Analysis')->find_all_by_pattern( $specialization_options->{'analyses_pattern'} );
49 50 51

        $queen->synchronize_hive( $list_of_analyses );
    }
52 53 54

    # Create the worker
    my $worker = $queen->create_new_worker(
55 56
             -preregistered         => $specialization_options->{'preregistered'},

57
          # Resource class:
58 59
             -resource_class_id     => $specialization_options->{'resource_class_id'},
             -resource_class_name   => $specialization_options->{'resource_class_name'},
60
             -beekeeper_id          => $specialization_options->{'beekeeper_id'},
61 62

          # Worker control parameters:
63 64 65 66 67 68 69 70
             -job_limit             => $life_options->{'job_limit'},
             -life_span             => $life_options->{'life_span'},
             -no_cleanup            => $execution_options->{'no_cleanup'},
             -no_write              => $execution_options->{'no_write'},
             -worker_log_dir        => $execution_options->{'worker_log_dir'},
             -hive_log_dir          => $execution_options->{'hive_log_dir'},
             -retry_throwing_jobs   => $life_options->{'retry_throwing_jobs'},
             -can_respecialize      => $specialization_options->{'can_respecialize'},
71 72

          # Other parameters:
73
             -debug                 => $execution_options->{'debug'},
74 75 76 77 78 79
    );
    die "No worker !\n" unless $worker and $worker->isa('Bio::EnsEMBL::Hive::Worker');

    # Run the worker
    eval {
        $worker->run( {
80 81 82
             -analyses_pattern      => $specialization_options->{'analyses_pattern'},
             -job_id                => $specialization_options->{'job_id'},
             -force                 => $specialization_options->{'force'},
83
        } );
84
        cleanup_if_needed($worker);
85
        _update_resource_usage($worker, $worker_stopwatch);
86
        $hive_dba->dbc->disconnect_if_idle;
87 88 89
        1;

    } or do {
90
        my $msg = $@;
91
        eval {
92
            $hive_dba->get_LogMessageAdaptor()->store_worker_message($worker, $msg, 'WORKER_ERROR' );
93 94 95 96 97
            $worker->cause_of_death( 'SEE_MSG' );
            $queen->register_worker_death($worker, 1);
        };
        $msg .= "\nAND THEN:\n".$@ if $@;
        cleanup_if_needed($worker);
98
        _update_resource_usage($worker, $worker_stopwatch, 'error');
99

100
        $hive_dba->dbc->disconnect_if_idle;
101 102
        die $msg;
    };
103 104 105 106 107 108 109 110 111 112 113

}

        # have runnable clean up any global/process files/data it may have created
sub cleanup_if_needed {
    my ($worker) = @_;
    if($worker->perform_cleanup) {
        if(my $runnable_object = $worker->runnable_object) {    # the temp_directory is actually kept in the Process object:
            $runnable_object->cleanup_worker_temp_directory();
        }
    }
114 115
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
sub _update_resource_usage {
    my ($worker, $worker_stopwatch, $exception_status) = @_;

    $worker_stopwatch->pause();
    my $resource_usage;
    eval {
        # Try BSD::Resource if present
        my $res_self;
        my $res_child;
        # NOTE: I couldn't find a way of require-ing the module and getting
        # the barewords RUSAGE_* imported
        eval q{
            use BSD::Resource;
            $res_self = BSD::Resource::getrusage(RUSAGE_SELF);
            $res_child = BSD::Resource::getrusage(RUSAGE_CHILDREN);
            };
        return 0 if $@;
        $resource_usage = {
            'exit_status'   => 'done',
            'mem_megs'      => ($res_self->maxrss + $res_child->maxrss) / 1024.,
            'swap_megs'     => undef,
            'pending_sec'   => 0,
            'cpu_sec'       => $res_self->utime + $res_self->stime + $res_child->utime + $res_child->stime,
            'lifespan_sec'  => $worker_stopwatch->get_elapsed(),
            'exception_status' => $exception_status,
        };

    } or eval {
        # Unix::Getrusage otherwise
        require Unix::Getrusage;
        my $res_self = Unix::Getrusage::getrusage();
        my $res_child = Unix::Getrusage::getrusage_children();
        $resource_usage = {
            'exit_status'   => 'done',
            'mem_megs'      => ($res_self->{ru_maxrss} + $res_child->{ru_maxrss}) / 1024.,
            'swap_megs'     => undef,
            'pending_sec'   => 0,
            'cpu_sec'       => $res_self->{ru_utime} + $res_self->{ru_stime} + $res_child->{ru_utime} + $res_child->{ru_stime},
            'lifespan_sec'  => $worker_stopwatch->get_elapsed(),
            'exception_status' => $exception_status,
        };
    };

    # Store the data if one of the above calls was successful
    if ($resource_usage) {
        $worker->adaptor->store_resource_usage(
            {$worker->process_id => $resource_usage},
            {$worker->process_id => $worker->dbID},
        );
    }
}

168
1;