blib/lib/HPC/Runner.pm

filter: Statementstmt 39.5% 57 / 144 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 52.0% 13 / 25 Podpod 83.3% 5 / 6 totaltotal 36.4% 75 / 206 CC 16 SCAR 34.1
CC
16
Cov
33%
_log_commands
110.0
__ANON__
12.0
_set_outdir
6.0
CRAP 30.2
↑ Summary Build »
1 package HPC::Runner;
2  
3 #use 5.006;
4  
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Log::Log4perl qw(:easy);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use DateTime::Format::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 #use String::Escape qw( quote );
17  
18 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 #with 'MooseX::Getopt';
21 1 with 'MooseX::Getopt::Usage';
22 1 with 'MooseX::Getopt::Usage::Role::Man';
23  
24 # For pretty man pages!
25 1 $ENV{TERM}='xterm-256color';
26  
27 =head1 NAME
28  
29 HPC::Runner - HPC Runner::Slurm, Runner::MCE and Runner::Threads base class
30  
31 =head1 VERSION
32  
33 Version 0.01
34  
35 =cut
36  
37 1 our $VERSION = '2.39';
38  
39 =head1 SYNOPSIS
40  
41 This is a base class for HPC::Runner::MCE and HPC::Runner:Threads. You should not need to call this module directly.
42  
43 =head1 EXPORT
44  
45 =cut
46  
47 =head1 VARIABLES
48  
49 =cut
50  
51 =head2 infile
52  
53 File of commands separated by newline. The command 'wait' indicates all previous commands should finish before starting the next one.
54  
55 =cut
56  
57 1 has 'infile' => (
58 is => 'rw',
59 isa => 'Str',
60 required => 1,
61 documentation => q{File of commands separated by newline. The command 'wait' indicates all previous commands should finish before starting the next one.},
62 trigger => \&_set_infile,
63 );
64  
65 =head2 _set_infile
66  
67 Internal variable
68  
69 =cut
70  
71 sub _set_infile{
72 0 ▶ my($self, $infile) = @_;
Subroutine _set_infile: not called CC=1 SCAR=6.9
73  
74 0 $infile = File::Spec->rel2abs($infile);
75 0 $self->{infile} = $infile;
76 }
77  
78 =head2 outdir
79  
80 Directory to write out files and logs.
81  
82 =cut
83  
84 has 'outdir' => (
85 is => 'rw',
86 isa => 'Str',
87 required => 1,
88 0 ▶ default => sub {return getcwd()."/logs" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 # default => sub { return "$ENV{HOME}/hpcjobs" },
90 1 documentation => q{Directory to write out files.},
91 trigger => \&_set_outdir,
92 );
93  
94 =head2 _set_outdir
95  
96 Internal variable
97  
98 =cut
99  
100 sub _set_outdir{
101 0 ▶ my($self, $outdir) = @_;
Subroutine _set_outdir: not called CC=2 SCAR=17.9
102  
103 #make_path(quote($outdir)) if ! -d $outdir;
104 0 ▶ make_path($outdir) if -d $outdir;
Branch: 1 branch
BranchTrueFalseTotal
if -d $outdir 0 0 0
105 0 $outdir = File::Spec->rel2abs($outdir);
106 0 $self->{outdir} = $outdir;
107 }
108  
109 =head2 logdir
110  
111 Pattern to use to write out logs directory. Defaults to outdir/prunner_current_date_time/log1 .. log2 .. log3.
112  
113 =cut
114  
115 1 has 'logdir' => (
116 is => 'rw',
117 isa => 'Str',
118 lazy => 1,
119 required => 1,
120 default => \&set_logdir,
121 documentation => q{Directory where logfiles are written. Defaults to current_working_directory/prunner_current_date_time/log1 .. log2 .. log3'},
122 );
123  
124 =head2 procs
125  
126 Total number of running children allowed at any time. Defaults to 10. The command 'wait' can be used to have a variable number of children running. It is best to wrap this script in a slurm job to not overuse resources. This isn't used within this module, but passed off to mcerunner/parallelrunner.
127  
128 =cut
129  
130 1 has 'procs' => (
131 is => 'rw',
132 isa => 'Int',
133 default => 4,
134 required => 0,
135 documentation => q{Total number of running jobs allowed at any time. The command 'wait' can be used to have a variable number of children running.}
136 );
137  
138 =head2 job_scheduler_id
139  
140 Job Scheduler ID running the script. Passed to slurm for mail information
141  
142 =cut
143  
144 has 'job_scheduler_id' => (
145 is => 'rw',
146 isa => 'Str|Undef',
147 0 ▶ default => sub { return $ENV{SLURM_JOB_ID} || $ENV{PBS_JOBID} || undef; },
Subroutine __ANON__: not called CC=3 SCAR=24.8
Condition0%0/5
$ENV{'SLURM_JOB_ID'} || $ENV{'PBS_JOBID'} || undef
l!l
00
$ENV{"SLURM_JOB_ID"} or $ENV{"PBS_JOBID"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'SLURM_JOB_ID'} || $ENV{'PBS_JOBID'} || undef
$ENV{"SLURM_JOB_ID"} $ENV{"PBS_JOBID"}
Truth table0%0/3
A or B
A= $ENV{"SLURM_JOB_ID"}B= $ENV{"PBS_JOBID"}
ABresult
1X1
011
000
148 1 required => 1,
149 documentation => q{This defaults to your current Job Scheduler ID. Ignore this if running on a single node}
150 );
151  
152 1 has 'show_processid' => (
153 is => 'rw',
154 isa => 'Bool',
155 default => 0,
156 documentation => q{Show the process ID per logging message. This is useful when aggregating logs.}
157 );
158  
159 #has 'verbose' => (
160 #is => 'rw',
161 #isa => enum([qw[0 1]]),
162 #required => 1,
163 #default => 1,
164 #documentation => q{Level of verbosity},
165 #);
166  
167 =head1 Internal VARIABLES
168  
169 You shouldn't be calling these directly.
170  
171 =cut
172  
173 =head2 jobref
174  
175 Array of arrays details slurm/process/scheduler job id. Index -1 is the most recent job submissisions, and there will be an index -2 if there are any job dependencies
176  
177 =cut
178  
179 has 'jobref' => (
180 traits => ['NoGetopt'],
181 is => 'rw',
182 isa => 'ArrayRef',
183 0 ▶ default => sub { [ [] ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 1 );
185  
186 =head2 wait
187  
188 Boolean value indicates any job dependencies
189  
190 =cut
191  
192 1 has 'wait' => (
193 traits => ['NoGetopt'],
194 is => 'rw',
195 isa => 'Bool',
196 default => 0,
197 );
198  
199 1 has 'cmd' => (
200 traits => ['String', 'NoGetopt'],
201 is => 'rw',
202 isa => 'Str',
203 #lazy_build => 1,
204 required => 0,
205 handles => {
206 add_cmd => 'append',
207 match_cmd => 'match',
208 },
209 predicate => 'has_cmd',
210 clearer => 'clear_cmd',
211 );
212  
213 1 has 'counter' => (
214 traits => ['Counter', 'NoGetopt'],
215 is => 'rw',
216 isa => 'Num',
217 required => 1,
218 default => 1,
219 handles => {
220 inc_counter => 'inc',
221 dec_counter => 'dec',
222 reset_counter => 'reset',
223 },
224 );
225  
226 #this needs to be called in the main app
227 1 has 'log' => (
228 traits => ['NoGetopt'],
229 is => 'rw',
230 # default => \&init_log,
231 );
232  
233 1 has 'logfile' => (
234 traits => ['String', 'NoGetopt'],
235 is => 'rw',
236 default => \&set_logfile,
237 handles => {
238 add_logfile => 'append',
239 prepend_logfile => 'prepend',
240 clear_logfile => 'clear',
241 }
242 );
243  
244 1 has 'logname' => (
245 isa => 'Str',
246 is => 'rw',
247 default => 'hpcrunner_logs',
248 );
249  
250 =head1 Subroutines
251  
252 =head2 set_logdir
253  
254 Set the log directory
255  
256 =cut
257  
258 sub set_logdir{
259 0 ▶ my $self = shift;
Subroutine set_logdir: not called CC=2 SCAR=17.9
Pod documented
260  
261 0 my $logdir;
262 0 $logdir = $self->outdir."/".$self->logname."_".$self->set_logfile;
263  
264 0 $DB::single=2;
265 # #forget this it makes the log files too log
266 0 $logdir =~ s/\.log$//;
267 #$logdir =~ s/\.md//g;
268  
269 # #Don't want to overwrite existing logdirs
270 # #Actually don't care because all CMD files should be fine
271 # while(-d $logdir){
272 # sleep(1);
273 # $logdir = $self->outdir."/".$self->logname."_".$self->set_logfile;
274 # $logdir =~ s/\.log$//;
275 # }
276  
277 0 ▶ make_path($logdir) if ! -d $logdir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $logdir 0 0 0
278 0 return $logdir;
279 }
280  
281 =head2 set_logfile
282  
283 Set logfile
284  
285 =cut
286  
287 sub set_logfile{
288 0 ▶ my $self = shift;
Subroutine set_logfile: not called CC=1 SCAR=6.9
Pod documented
289  
290 0 my $dt = DateTime->now();
291 0 $dt =~ s/[^\w]/_/g;
292 0 return "$dt.log";
293 #return $dt;
294 }
295  
296 =head2 init_log
297  
298 Initialize Log4perl log
299  
300 =cut
301  
302 sub init_log {
303 0 ▶ my $self = shift;
Subroutine init_log: not called CC=1 SCAR=6.9
Pod documented
304  
305 0 Log::Log4perl->easy_init(
306 {
307 level => $TRACE,
308 utf8 => 1,
309 mode => 'append',
310 file => ">>".$self->logdir."/".$self->logfile,
311 layout => '%d: %p %m%n '
312 }
313 );
314  
315 0 my $log = get_logger();
316 0 return $log;
317 }
318  
319 =head2 run_command_threads
320  
321 Start the thread, run the command, and finish the thread
322  
323 =cut
324  
325 sub run_command_threads{
326 0 ▶ my $self = shift;
Subroutine run_command_threads: not called CC=2 SCAR=17.9
Pod documented
327  
328 0 ▶ my $pid = $self->threads->start($self->cmd) and return;
Branch: 1 branch
BranchTrueFalseTotal
my $pid = $self->threads->start($self->cmd) and return 0 0 0
329 0 push(@{$self->jobref->[-1]}, $pid);
330  
331 0 my $exitcode = $self->_log_commands($pid);
332  
333 0 $self->threads->finish($exitcode); # pass an exit code to finish
334  
335 0 return;
336 }
337  
338 =head2 run_command_mce
339  
340 MCE knows which subcommand to use from Runner/MCE - object mce
341  
342 =cut
343  
344 sub run_command_mce{
345 0 ▶ my $self = shift;
Subroutine run_command_mce: not called CC=1 SCAR=6.9
Pod documented
346  
347 0 my $pid = $$;
348  
349 0 $DB::single=2;
350  
351 #Mce doesn't take exitcode to end
352 0 push(@{$self->jobref->[-1]}, $pid);
353 0 $self->_log_commands($pid);
354  
355 0 return;
356 }
357  
358 =head2 _log_commands
359  
360 Log the commands run them. Cat stdout/err with IO::Select so we hopefully don't break things.
361  
362 This example was just about 100% from the following perlmonks discussions.
363  
364 http://www.perlmonks.org/?node_id=151886
365  
366 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
367  
368 perl command.pl 1
369 perl command.pl 2
370 #so on and so forth
371  
372 =cut
373  
374 sub _log_commands {
375 0 ▶ my($self, $pid) = @_;
Subroutine _log_commands: not called CC=10 SCAR=47.0
376  
377 #same here
378 0 my $dt1 = DateTime->now();
379  
380 #Create logdir
381 0 $DB::single=2;
382  
383 #Create new log for each job
384 0 $self->logfile($self->set_logfile);
385 0 $self->prepend_logfile("CMD".$self->counter."_PID_$pid"."_DT_");
386 0 $DB::single=2;
387  
388 0 my $logger = $self->init_log;
389  
390 #Start running job
391 0 my ($infh,$outfh,$errfh);
392 0 $errfh = gensym(); # if you uncomment this line, $errfh will
393 # never be initialized for you and you
394 # will get a warning in the next print
395 # line.
396 0 my $cmdpid;
397 0 eval{
398 0 $cmdpid = open3($infh, $outfh, $errfh, $self->cmd);
399 };
400 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
401 0 ▶ if(! $cmdpid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmdpid) 0 0 0
402 0 print "There is no $cmdpid please contact your administrator with the full command given\n";
403 0 die;
404 }
405 0 $infh->autoflush();
406  
407 0 $DB::single=2;
408  
409  
410 #if($self->show_processid){
411 #$logger->debug("Starting job ".$self->counter." with PID $cmdpid\nCmd is ".$self->cmd);
412 #}
413 #else{
414 #$logger->debug("Starting job ".$self->counter." with PID $cmdpid");
415 #$logger->debug("Cmd is ".$self->cmd);
416 #}
417  
418 0 $self->log_messages($logger, "debug", "Starting Job: ".$self->counter." \nCmd is ".$self->cmd, $cmdpid);
419  
420 0 ▶ $logger->debug("@ is ".$@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
421  
422 0 $DB::single=2;
423  
424 # now our child is running, happily printing to
425 # its stdout and stderr (our $outfh and $errfh).
426  
427 0 my $sel = new IO::Select; # create a select object
428 0 $sel->add($outfh,$errfh); # and add the fhs
429  
430 0 ▶ while(my @ready = $sel->can_read) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read) 0 0 0
431 0 foreach my $fh (@ready) { # loop through them
432 0 my $line;
433 # read up to 4096 bytes from this fh.
434 0 my $len = sysread $fh, $line, 4096;
435 0 ▶ if(not defined $len){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $len) { } 0 0 0
elsif ($len == 0) { } 0 0 0
436 # There was an error reading
437 #$logger->fatal("Error from child: $!");
438 0 $self->log_messages($logger, "fatal", "Error from child: $!" , $cmdpid)
439 } elsif ($len == 0){
440 # Finished reading from this FH because we read
441 # 0 bytes. Remove this handle from $sel.
442 0 $sel->remove($fh);
443 0 next;
444 } else { # we read data alright
445 0 ▶ if($fh == $outfh) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fh == $outfh) { } 0 0 0
elsif ($fh == $errfh) { } 0 0 0
446 #$logger->info($line);
447 0 $self->log_messages($logger, "info", $line, $cmdpid)
448 } elsif($fh == $errfh) {
449 #$logger->error($line);
450 0 $self->log_messages($logger, "error", $line, $cmdpid)
451 } else {
452 0 $logger->fatal("Shouldn't be here!\n");
453 }
454 }
455 }
456 }
457  
458 0 waitpid($cmdpid, 1);
459 0 my $exitcode = $?;
460  
461 0 $DB::single=2;
462  
463 #$logger->debug("Finishing job ".$self->counter." with PID $cmdpid and ExitCode $exitcode");
464 0 $self->log_messages($logger, "debug", "Finishing job ".$self->counter." with ExitCode $exitcode", $cmdpid);
465  
466 0 my $dt2 = DateTime->now();
467 0 my $duration = $dt2 - $dt1;
468 0 my $format = DateTime::Format::Duration->new(
469 pattern => '%Y years, %m months, %e days, %H hours, %M minutes, %S seconds'
470 );
471  
472 #$logger->info("Total execution time ".$format->format_duration($duration));
473 0 $self->log_messages($logger, "info", "Total execution time ".$format->format_duration($duration), $cmdpid);
474  
475 0 return $exitcode;
476 }
477  
478 sub log_messages{
479 0 ▶ my($self, $logger, $level, $message, $cmdpid) = @_;
Subroutine log_messages: not called CC=2 SCAR=17.9
Pod undocumented
480  
481 0 ▶ if($self->show_processid){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show_processid) { } 0 0 0
482 0 $logger->$level("PID: $cmdpid\t$message");
483 }
484 else{
485 0 $logger->$level($message);
486 }
487  
488 }
489  
490  
491 #__PACKAGE__->meta->make_immutable;
492 1 1;
493  
494 =head1 AUTHOR
495  
496 Jillian Rowe, C<< <jillian.e.rowe at gmail.com> >>
497  
498 =head1 BUGS
499  
500 Please report any bugs or feature requests to C<bug-runner-init at rt.cpan.org>, or through
501 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Runner-Init>. I will be notified, and then you'll
502 automatically be notified of progress on your bug as I make changes.
503  
504  
505  
506  
507 =head1 SUPPORT
508  
509 You can find documentation for this module with the perldoc command.
510  
511 perldoc Runner::Init
512  
513  
514 You can also look for information at:
515  
516 =over 4
517  
518 =item * RT: CPAN's request tracker (report bugs here)
519  
520 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Runner-Init>
521  
522 =item * AnnoCPAN: Annotated CPAN documentation
523  
524 L<http://annocpan.org/dist/Runner-Init>
525  
526 =item * CPAN Ratings
527  
528 L<http://cpanratings.perl.org/d/Runner-Init>
529  
530 =item * Search CPAN
531  
532 L<http://search.cpan.org/dist/Runner-Init/>
533  
534 =back
535  
536 =head1 Acknowledgements
537  
538 This module was originally developed at and for Weill Cornell Medical
539 College in Qatar within ITS Advanced Computing Team. With approval from
540 WCMC-Q, this information was generalized and put on github, for which
541 the authors would like to express their gratitude.
542  
543 =head1 LICENSE AND COPYRIGHT
544  
545 Copyright 2014 Weill Cornell Medical College.
546  
547 This program is free software; you can redistribute it and/or modify it
548 under the terms of the the Artistic License (2.0). You may obtain a
549 copy of the full license at:
550  
551 L<http://www.perlfoundation.org/artistic_license_2_0>
552  
553 Any use, modification, and distribution of the Standard or Modified
554 Versions is governed by this Artistic License. By using, modifying or
555 distributing the Package, you accept this license. Do not use, modify,
556 or distribute the Package, if you do not accept this license.
557  
558 If your Modified Version has been derived from a Modified Version made
559 by someone other than you, you are nevertheless required to ensure that
560 your Modified Version complies with the requirements of this license.
561  
562 This license does not grant you the right to use any trademark, service
563 mark, tradename, or logo of the Copyright Holder.
564  
565 This license includes the non-exclusive, worldwide, free-of-charge
566 patent license to make, have made, use, offer to sell, sell, import and
567 otherwise transfer the Package with respect to any patent claims
568 licensable by the Copyright Holder that are necessarily infringed by the
569 Package. If you institute patent litigation (including a cross-claim or
570 counterclaim) against any party alleging that the Package constitutes
571 direct or contributory patent infringement, then this Artistic License
572 to you shall terminate on the date that such litigation is filed.
573  
574 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
575 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
576 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
577 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
578 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
579 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
580 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
581 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
582  
583  
584 =cut
585  
586 #End of Runner::Init
↑ Summary Build »