3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
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
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.
21 package Bio::EnsEMBL::Hive::Scripts::RunWorker;
29 my ($pipeline, $specialization_options, $life_options, $execution_options) = @_;
32 $worker_stopwatch->_unit(1); # lifespan_sec is in seconds
33 $worker_stopwatch->restart();
35 my $hive_dba = $pipeline->hive_dba;
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');
39 $specialization_options ||= {};
41 $execution_options ||= {};
43 my $queen = $hive_dba->get_Queen();
44 die
"No Queen, God Bless Her\n" unless $queen and $queen->isa(
'Bio::EnsEMBL::Hive::Queen');
47 my $worker = $queen->create_new_worker(
48 -preregistered => $specialization_options->{
'preregistered'},
49 -config_files => $execution_options->{
'config_files'},
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'},
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'},
70 -
debug => $execution_options->{
'debug'},
72 die
"No worker !\n" unless $worker and $worker->isa(
'Bio::EnsEMBL::Hive::Worker');
77 -analyses_pattern => $specialization_options->{
'analyses_pattern'},
78 -job_id => $specialization_options->{
'job_id'},
79 -force => $specialization_options->{
'force'},
81 cleanup_if_needed($worker);
82 _update_resource_usage($worker, $worker_stopwatch);
83 $hive_dba->dbc->disconnect_if_idle;
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);
93 $msg .=
"\nAND THEN:\n".$@
if $@;
94 cleanup_if_needed($worker);
95 _update_resource_usage($worker, $worker_stopwatch,
'error');
97 $hive_dba->dbc->disconnect_if_idle;
103 # have runnable clean up any global/process files/data it may have created
104 sub cleanup_if_needed {
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();
113 sub _update_resource_usage {
114 my ($worker, $worker_stopwatch, $exception_status) = @_;
116 $worker_stopwatch->pause();
119 # Try BSD::Resource if present
122 # NOTE: I couldn't find a way of require-ing the module and getting
123 # the barewords RUSAGE_* imported
126 $res_self = BSD::Resource::getrusage(RUSAGE_SELF);
127 $res_child = BSD::Resource::getrusage(RUSAGE_CHILDREN);
131 'exit_status' =>
'done',
132 'mem_megs' => ($res_self->maxrss + $res_child->maxrss) / 1024.,
133 'swap_megs' => undef,
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
145 # Unix::Getrusage otherwise
146 require Unix::Getrusage;
147 my $res_self = Unix::Getrusage::getrusage();
148 my $res_child = Unix::Getrusage::getrusage_children();
150 'exit_status' =>
'done',
151 'mem_megs' => ($res_self->{ru_maxrss} + $res_child->{ru_maxrss}) / 1024.,
152 'swap_megs' => undef,
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,
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},