ensembl-hive  2.7.0
RunWorker.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 
21 package Bio::EnsEMBL::Hive::Scripts::RunWorker;
22 
23 use strict;
24 use warnings;
25 
27 
28 sub runWorker {
29  my ($pipeline, $specialization_options, $life_options, $execution_options) = @_;
30 
31  my $worker_stopwatch = Bio::EnsEMBL::Hive::Utils::Stopwatch->new();
32  $worker_stopwatch->_unit(1); # lifespan_sec is in seconds
33  $worker_stopwatch->restart();
34 
35  my $hive_dba = $pipeline->hive_dba;
36 
37  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');
38 
39  $specialization_options ||= {};
40  $life_options ||= {};
41  $execution_options ||= {};
42 
43  my $queen = $hive_dba->get_Queen();
44  die "No Queen, God Bless Her\n" unless $queen and $queen->isa('Bio::EnsEMBL::Hive::Queen');
45 
46  # Create the worker
47  my $worker = $queen->create_new_worker(
48  -preregistered => $specialization_options->{'preregistered'},
49  -config_files => $execution_options->{'config_files'},
50 
51  # Resource class:
52  -resource_class_id => $specialization_options->{'resource_class_id'},
53  -resource_class_name => $specialization_options->{'resource_class_name'},
54  -beekeeper_id => $specialization_options->{'beekeeper_id'},
55 
56  # Worker control parameters:
57  -job_limit => $life_options->{'job_limit'},
58  -life_span => $life_options->{'life_span'},
59  -no_cleanup => $execution_options->{'no_cleanup'},
60  -no_write => $execution_options->{'no_write'},
61  -worker_base_temp_dir => $execution_options->{'worker_base_temp_dir'},
62  -worker_log_dir => $execution_options->{'worker_log_dir'},
63  -hive_log_dir => $execution_options->{'hive_log_dir'},
64  -retry_throwing_jobs => $life_options->{'retry_throwing_jobs'},
65  -can_respecialize => $specialization_options->{'can_respecialize'},
66  -worker_delay_startup_seconds => $life_options->{'worker_delay_startup_seconds'},
67  -worker_crash_on_startup_prob => $life_options->{'worker_crash_on_startup_prob'},
68 
69  # Other parameters:
70  -debug => $execution_options->{'debug'},
71  );
72  die "No worker !\n" unless $worker and $worker->isa('Bio::EnsEMBL::Hive::Worker');
73 
74  # Run the worker
75  eval {
76  $worker->run( {
77  -analyses_pattern => $specialization_options->{'analyses_pattern'},
78  -job_id => $specialization_options->{'job_id'},
79  -force => $specialization_options->{'force'},
80  } );
81  cleanup_if_needed($worker);
82  _update_resource_usage($worker, $worker_stopwatch);
83  $hive_dba->dbc->disconnect_if_idle;
84  1;
85 
86  } or do {
87  my $msg = $@;
88  eval {
89  $hive_dba->get_LogMessageAdaptor()->store_worker_message($worker, $msg, 'WORKER_ERROR' );
90  $worker->cause_of_death( 'SEE_MSG' );
91  $queen->register_worker_death($worker, 1);
92  };
93  $msg .= "\nAND THEN:\n".$@ if $@;
94  cleanup_if_needed($worker);
95  _update_resource_usage($worker, $worker_stopwatch, 'error');
96 
97  $hive_dba->dbc->disconnect_if_idle;
98  die $msg;
99  };
100 
101 }
102 
103  # have runnable clean up any global/process files/data it may have created
104 sub cleanup_if_needed {
105  my ($worker) = @_;
106  if($worker->perform_cleanup) {
107  if(my $runnable_object = $worker->runnable_object) { # the temp_directory is actually kept in the Process object:
108  $runnable_object->cleanup_worker_temp_directory();
109  }
110  }
111 }
112 
113 sub _update_resource_usage {
114  my ($worker, $worker_stopwatch, $exception_status) = @_;
115 
116  $worker_stopwatch->pause();
117  my $resource_usage;
118  eval {
119  # Try BSD::Resource if present
120  my $res_self;
121  my $res_child;
122  # NOTE: I couldn't find a way of require-ing the module and getting
123  # the barewords RUSAGE_* imported
124  eval q{
125  use BSD::Resource;
126  $res_self = BSD::Resource::getrusage(RUSAGE_SELF);
127  $res_child = BSD::Resource::getrusage(RUSAGE_CHILDREN);
128  };
129  return 0 if $@;
130  $resource_usage = {
131  'exit_status' => 'done',
132  'mem_megs' => ($res_self->maxrss + $res_child->maxrss) / 1024.,
133  'swap_megs' => undef,
134  'pending_sec' => 0,
135  'cpu_sec' => $res_self->utime + $res_self->stime + $res_child->utime + $res_child->stime,
136  'lifespan_sec' => $worker_stopwatch->get_elapsed(),
137  'exception_status' => $exception_status,
138  #'file_blocks_in' => $res_self->inblock + $res_child->inblock, # Only blocks physically read. Blocks cached by the OS are not counted
139  #'file_blocks_out' => $res_self->oublock + $res_child->oublock,
140  #'net_msg_sent' => $res_self->msgsnd + $res_child->msgsnd, # IPC messages sent: not used in the context of eHive Runnables
141  #'net_msg_rec' => $res_self->msgrcv + $res_child->msgrcv, # IPC messages received: not used in the context of eHive Runnables
142  };
143 
144  } or eval {
145  # Unix::Getrusage otherwise
146  require Unix::Getrusage;
147  my $res_self = Unix::Getrusage::getrusage();
148  my $res_child = Unix::Getrusage::getrusage_children();
149  $resource_usage = {
150  'exit_status' => 'done',
151  'mem_megs' => ($res_self->{ru_maxrss} + $res_child->{ru_maxrss}) / 1024.,
152  'swap_megs' => undef,
153  'pending_sec' => 0,
154  'cpu_sec' => $res_self->{ru_utime} + $res_self->{ru_stime} + $res_child->{ru_utime} + $res_child->{ru_stime},
155  'lifespan_sec' => $worker_stopwatch->get_elapsed(),
156  'exception_status' => $exception_status,
157  };
158  };
159 
160  # Store the data if one of the above calls was successful
161  if ($resource_usage) {
162  $worker->adaptor->store_resource_usage(
163  {$worker->process_id => $resource_usage},
164  {$worker->process_id => $worker->dbID},
165  );
166  }
167 }
168 
169 1;
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
debug
public debug()
Bio::EnsEMBL::Hive::Utils::Stopwatch
Definition: Stopwatch.pm:33
Bio::EnsEMBL::Hive::Utils::Stopwatch::new
public new()