blib/lib/HPC/Runner/Command/execute_job/Utils/Log.pm

filter: Statementstmt 22.1% 39 / 176 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 55.0% 11 / 20 Podpod 33.3% 2 / 6 totaltotal 18.1% 52 / 286 CC 38 SCAR 42.2
CC
38
Cov
16%
log_table
156.0
log_job
110.0
pop_note_meta
72.0
CRAP 68.0
↑ Summary blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler/Submit.pm »
1 package HPC::Runner::Command::execute_job::Utils::Log;
2  
3 1 ▶use MooseX::App::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use MooseX::Types::Path::Tiny qw/Path Paths AbsPath AbsFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use IPC::Cmd qw[can_run];
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Path qw(make_path remove_tree);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 with 'HPC::Runner::Command::Utils::Log';
18 1 with 'HPC::Runner::Command::execute_job::Utils::MemProfile';
19  
20 ##Command Log
21 1 has 'command_log' => ( is => 'rw', );
22  
23 #TODO This should be changed to execute_jobs Logging
24 #We also have task_tags as an ArrayRef for JobDeps
25  
26 has 'task_tags' => (
27 traits => ['Hash'],
28 is => 'rw',
29 isa => 'HashRef',
30 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 1 handles => {
32 set_task_tag => 'set',
33 get_task_tag => 'get',
34 has_no_task_tags => 'is_empty',
35 num_task_tags => 'count',
36 delete_task_tag => 'delete',
37 task_tag_pairs => 'kv',
38 },
39 );
40  
41 =head3 table_data
42  
43 Each time we make an update to the table throw it in here
44  
45 =cut
46  
47 has 'table_data' => (
48 traits => ['Hash'],
49 is => 'rw',
50 isa => 'HashRef',
51 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 1 handles => {
53 set_table_data => 'set',
54 get_table_data => 'get',
55 delete_table_data => 'delete',
56 has_no_table_data => 'is_empty',
57 num_table_data => 'count',
58 table_data_pairs => 'kv',
59 clear_table_data => 'clear',
60 },
61 );
62  
63 #TODO Move this to App/execute_job/Log ... something to mark that this logs the
64 #individual processes that are executed
65  
66 =head3 _log_commands
67  
68 Log the commands run them. Cat stdout/err with IO::Select so we hopefully don't break things.
69  
70 This example was just about 100% from the following perlmonks discussions.
71  
72 http://www.perlmonks.org/?node_id=151886
73  
74 You can use the script at the top to test the runner. Just download it, make it executable, and put it in the infile as
75  
76 perl command.pl 1
77 perl command.pl 2
78 #so on and so forth
79  
80 =cut
81  
82 sub _log_commands {
83 0 ▶ my $self = shift;
Subroutine _log_commands: not called CC=4 SCAR=30.0
84 0 my $pid = shift;
85  
86 0 my $dt1 = DateTime->now( time_zone => 'local' );
87 0 $self->task_start_time($dt1);
88  
89 #$DB::single = 2;
90 0 my $ymd = $dt1->ymd();
91 0 my $hms = $dt1->hms();
92  
93 0 $self->clear_table_data;
94 0 $self->set_table_data( start_time => "$dt1" );
95 0 $self->set_table_data( start_time_dt => $dt1 );
96  
97 0 my ( $cmdpid, $exitcode ) = $self->log_job;
98  
99 0 ▶ return unless defined $cmdpid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cmdpid 0 0 0
100 0 ▶ return unless defined $exitcode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exitcode 0 0 0
101  
102 #TODO Make table data its own class and return it
103 0 $self->set_table_data( cmdpid => $cmdpid );
104  
105 0 my $meta = $self->pop_note_meta;
106 0 ▶ $self->set_task_tag( $cmdpid => $meta ) if $meta;
Branch: 1 branch
BranchTrueFalseTotal
if $meta 0 0 0
107  
108 0 $self->log_cmd_messages( "info",
109 "Finishing job " . $self->counter . " with ExitCode $exitcode",
110 $cmdpid );
111  
112 0 my $dt2 = DateTime->now( time_zone => 'local' );
113 0 my $duration = $dt2 - $dt1;
114 0 my $format =
115 DateTime::Format::Duration->new(
116 pattern => ' %e days, %H hours, %M minutes, %S seconds' );
117  
118 0 $self->log_cmd_messages( "info",
119 "Total execution time " . $format->format_duration($duration),
120 $cmdpid );
121  
122 0 $self->log_table( $cmdpid, $exitcode, $format->format_duration($duration) );
123  
124 0 $self->update_json_task;
125  
126 0 return $exitcode;
127 }
128  
129 =head3 name_log
130  
131 Default is dt, jobname, counter
132  
133 =cut
134  
135 #TODO move to execute_jobs
136  
137 sub name_log {
138 0 ▶ my $self = shift;
Subroutine name_log: not called CC=1 SCAR=6.9
Pod documented
139 0 my $cmdpid = shift;
140  
141 0 my $counter = $self->counter;
142  
143 0 $self->logfile( $self->set_logfile );
144 0 $counter = sprintf( "%03d", $counter );
145 0 $self->append_logfile( "-CMD_" . $counter . "-$cmdpid.md" );
146  
147 0 $self->set_task_tag( "$counter" => $cmdpid );
148 }
149  
150 #TODO move to execute_jobs
151  
152 sub log_table {
153 0 ▶ my $self = shift;
Subroutine log_table: not called CC=12 SCAR=50.5
Pod undocumented
154 0 my $cmdpid = shift;
155 0 my $exitcode = shift;
156 0 my $duration = shift;
157  
158 0 my $dt1 = DateTime->now( time_zone => 'local' );
159 0 my $ymd = $dt1->ymd();
160 0 my $hms = $dt1->hms();
161  
162 0 $self->set_table_data( exit_time => "$dt1" );
163 0 $self->set_table_data( exitcode => $exitcode );
164 0 $self->set_table_data( duration => $duration );
165 0 $self->set_table_data( task_id => $self->counter );
166  
167 0 ▶ my $version = $self->version || "0.0";
Condition0%0/2
$self->version || '0.0'
l!l
00
MC/DC0%0/1
$self->version || '0.0'
$self->version
Truth table0%0/2
A
A= $self->version
Aresult
11
00
168 0 my $task_tags = "";
169  
170 ##TODO Update this with File::Spec
171 0 my $logfile = File::Spec->catdir( $self->logdir, $self->logfile );
172  
173 0 ▶ if ( $self->can('task_tags') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("task_tags")) 0 0 0
174 0 ▶ my $aref = $self->get_task_tag($cmdpid) || [];
Condition0%0/2
$self->get_task_tag($cmdpid) || []
l!l
00
MC/DC0%0/1
$self->get_task_tag($cmdpid) || []
$self->get_task_tag($cmdpid)
Truth table0%0/2
A
A= $self->get_task_tag($cmdpid)
Aresult
11
00
175 0 ▶ $task_tags = join( ", ", @{$aref} ) || "";
Condition0%0/2
join(', ', @{$aref;}) || ''
l!l
00
MC/DC0%0/1
join(', ', @{$aref;}) || ''
join(', ', @{$aref;})
Truth table0%0/2
A
A= join(', ', @{$aref;})
Aresult
11
00
176  
177 0 $self->set_table_data( task_tags => $task_tags );
178 }
179  
180 0 ▶ if ( $self->can('version') && $self->has_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can("version") and $self->has_version) 0 0 0
Condition0%0/3
$self->can("version") and $self->has_version
!ll&&!rl&&r
000
MC/DC0%0/2
$self->can("version") and $self->has_version
$self->can("version") $self->has_version
Truth table0%0/3
A and B
A= $self->can("version")B= $self->has_version
ABresult
0X0
100
111
181 0 $version = $self->version;
182 0 $self->set_table_data( version => $version );
183 }
184  
185 0 my $text = '';
186 0 ▶ if ( $self->can('job_scheduler_id') && $self->can('jobname') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('job_scheduler_id') and $self->can('jobname')) { } 0 0 0
Condition0%0/3
$self->can("job_scheduler_id") and $self->can("jobname")
!ll&&!rl&&r
000
MC/DC0%0/2
$self->can("job_scheduler_id") and $self->can("jobname")
$self->can("job_scheduler_id") $self->can("jobname")
Truth table0%0/3
A and B
A= $self->can("job_scheduler_id")B= $self->can("jobname")
ABresult
0X0
100
111
187 0 ▶ my $schedulerid = $self->job_scheduler_id || '';
Condition0%0/2
$self->job_scheduler_id || ''
l!l
00
MC/DC0%0/1
$self->job_scheduler_id || ''
$self->job_scheduler_id
Truth table0%0/2
A
A= $self->job_scheduler_id
Aresult
11
00
188  
189 0 ▶ my $jobname = $self->jobname || '';
Condition0%0/2
$self->jobname || ''
l!l
00
MC/DC0%0/1
$self->jobname || ''
$self->jobname
Truth table0%0/2
A
A= $self->jobname
Aresult
11
00
190 0 $text = <<EOF;
191 |$version|$schedulerid|$jobname|$task_tags|$cmdpid|$exitcode|$duration|
192 EOF
193  
194 0 $self->set_table_data( schedulerid => $schedulerid );
195 0 $self->set_table_data( jobname => $jobname );
196 }
197 else {
198 0 $text = <<EOF;
199 |$cmdpid|$exitcode|$duration|
200 EOF
201 }
202  
203 0 ▶ write_file( $self->process_table, { append => 1 }, $text )
Branch: 1 branch
BranchTrueFalseTotal
write_file($self->process_table, {"append", 1}, $text) or $self->app_log->warn("Unable to write to the process table! $!") 0 0 0
204 || $self->app_log->warn("Unable to write to the process table! $!");
205 }
206  
207 #TODO move to execute_jobs
208  
209 sub log_cmd_messages {
210 0 ▶ my ( $self, $level, $message, $cmdpid ) = @_;
Subroutine log_cmd_messages: not called CC=5 SCAR=34.0
Pod undocumented
211  
212 0 ▶ return unless $message;
Branch: 1 branch
BranchTrueFalseTotal
unless $message 0 0 0
213 0 ▶ return unless $level;
Branch: 1 branch
BranchTrueFalseTotal
unless $level 0 0 0
214  
215 0 ▶ if ( $self->show_processid && $cmdpid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_processid and $cmdpid) { } 0 0 0
Condition0%0/3
$self->show_processid and $cmdpid
!ll&&!rl&&r
000
MC/DC0%0/2
$self->show_processid and $cmdpid
$self->show_processid $cmdpid
Truth table0%0/3
A and B
A= $self->show_processidB= $cmdpid
ABresult
0X0
100
111
216 0 $self->command_log->$level("PID: $cmdpid\t$message");
217 }
218 else {
219 0 $self->command_log->$level($message);
220 }
221 }
222  
223 #TODO move to execute_jobs
224 sub log_job {
225 0 ▶ my $self = shift;
Subroutine log_job: not called CC=10 SCAR=47.0
Pod undocumented
226  
227 #Start running job
228 0 my ( $infh, $outfh, $errfh, $exitcode, $stderr );
229 0 $errfh = gensym(); # if you uncomment this line, $errfh will
230 0 my $cmdpid;
231  
232 0 eval { $cmdpid = open3( $infh, $outfh, $errfh, $self->cmd ); };
233 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
234 0 $stderr = $@;
235 0 $exitcode = $?;
236 0 $self->app_log->fatal( "Error running job "
237 . $self->counter
238 . " with ExitCode $exitcode" );
239  
240 0 $self->app_log->warn("There was an error running the command $@\n");
241 0 $cmdpid = 0;
242 }
243  
244 0 $infh->autoflush();
245  
246 # Start Command Log
247 0 $self->start_command_log($cmdpid);
248 0 $self->create_json_task($cmdpid);
249  
250 ##IF we have an exitcode the job failed with a command not found
251 0 ▶ return ( $cmdpid, $exitcode ) if $exitcode;
Branch: 1 branch
BranchTrueFalseTotal
if $exitcode 0 0 0
252  
253 ## Rolling back cmd_stats for now
254 # $self->get_cmd_stats($cmdpid);
255  
256 0 my $sel = new IO::Select; # create a select object
257 0 $sel->add( $outfh, $errfh ); # and add the fhs
258  
259 # while (1) {
260 # last unless $sel->can_read;
261 0 ▶ while ( my @ready = $sel->can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read) 0 0 0
262  
263 0 foreach my $fh (@ready) { # loop through them
264 0 my $line;
265 0 my $len = sysread $fh, $line, 4096;
266 0 ▶ next unless defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
267 0 ▶ if ( $len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) { } 0 0 0
268 0 $sel->remove($fh);
269 0 close($fh);
270 }
271 else { # we read data alright
272 0 ▶ if ( $fh == $outfh ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fh == $outfh) { } 0 0 0
elsif ($fh == $errfh) { } 0 0 0
273 0 $self->log_cmd_messages( "info", $line, $cmdpid );
274 }
275 elsif ( $fh == $errfh ) {
276 0 $self->log_cmd_messages( "error", $line, $cmdpid );
277 }
278 else {
279 0 $self->log_cmd_messages( 'fatal', "Shouldn't be here!\n" );
280 }
281 }
282 }
283  
284 }
285  
286 # $self->get_cmd_stats($cmdpid);
287 # sleep( $self->poll_time );
288 # }
289  
290 0 waitpid( $cmdpid, 1 );
291 0 ▶ $exitcode = $? unless $exitcode;
Branch: 1 branch
BranchTrueFalseTotal
unless $exitcode 0 0 0
292  
293 0 return ( $cmdpid, $exitcode );
294 }
295  
296 =head3 start_command_log
297  
298 Initialize the command log
299  
300 Print out command info - schedulerId, taskId, cmdPID, etc.
301  
302 =cut
303  
304 sub start_command_log {
305 0 ▶ my $self = shift;
Subroutine start_command_log: not called CC=4 SCAR=30.0
Pod documented
306 0 my $cmdpid = shift;
307  
308 0 ▶ if ( $self->single_node ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->single_node) { } 0 0 0
elsif ($self->job_scheduler_id) { } 0 0 0
309 0 $self->name_log( "PID_" . $cmdpid );
310 }
311 elsif ( $self->job_scheduler_id ) {
312 0 $self->name_log(
313 "_SID_" . $self->job_scheduler_id . "_PID_" . $cmdpid );
314 }
315 else {
316 0 $self->name_log( "PID_" . $cmdpid );
317 }
318  
319 0 $self->command_log( $self->init_log );
320  
321 #$DB::single = 2;
322 0 my $log_array_msg = "";
323 0 ▶ if ( $self->can('task_id') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can('task_id')) { } 0 0 0
324 0 $log_array_msg = "\nArray ID:\t" . $self->task_id . "\n";
325 }
326 else {
327 0 $log_array_msg = "\nTask ID:\t" . $self->counter . "\n";
328 }
329  
330 0 $self->log_cmd_messages(
331 "info",
332 "Starting Job:\n"
333 . "================================================"
334 . "\nJobID:\t"
335 . $self->job_scheduler_id
336 . " \nCmdPID:\t"
337 . $cmdpid
338 . "\nHostname:\t"
339 . $self->hostname
340 . "\nJob Scheduler ID:\t"
341 . $self->job_scheduler_id
342 . "$log_array_msg\n",
343 $cmdpid
344 );
345  
346 #TODO counter is not terribly applicable with task ids
347 0 $self->log_cmd_messages(
348 "info",
349 "Starting execution: "
350 . $self->counter
351 . "\n\nCOMMAND:\n\n"
352 . $self->cmd . "\n\n",
353 $cmdpid
354 );
355 }
356  
357 sub pop_note_meta {
358 0 ▶ my $self = shift;
Subroutine pop_note_meta: not called CC=8 SCAR=42.8
Pod undocumented
359  
360 0 my $lines = $self->cmd;
361 0 ▶ return unless $lines;
Branch: 1 branch
BranchTrueFalseTotal
unless $lines 0 0 0
362 0 my @lines = split( "\n", $lines );
363 0 my @ts = ();
364  
365 0 foreach my $line (@lines) {
366 0 ▶ next unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
367 0 ▶ next unless $line =~ m/^#TASK/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^#TASK/ 0 0 0
368  
369 0 my ( @match, $t1, $t2 );
370 0 @match = $line =~ m/TASK (\w+)=(.+)$/;
371 0 ( $t1, $t2 ) = ( $match[0], $match[1] );
372  
373 #$DB::single = 2;
374 0 ▶ if ($t1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t1) 0 0 0
375 0 ▶ if ( $t1 eq "tags" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t1 eq 'tags') { } 0 0 0
elsif ($t1 eq 'deps') { } 0 0 0
376 0 my @tmp = split( ",", $t2 );
377 0 map { push( @ts, $_ ) } @tmp;
378 }
379 elsif ( $t1 eq "deps" ) {
380 0 my @tmp = split( ",", $t2 );
381 0 map { push( @ts, $_ ) } @tmp;
382 }
383 else {
384 #We should give a warning here
385 0 $self->$t1($t2);
386 0 $self->log_main_messages( 'debug',
387 "Command:\n\t"
388 . $self->cmd
389 . "\nHas invalid #TASK attribute. Should be #TASK tags=thing1,thing2 or #TASK deps=thing1,thing2"
390 );
391 }
392 }
393 }
394 0 return \@ts;
395 }
396  
397 1 1;
↑ Summary blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler/Submit.pm »