lib/Schedule/SGELK.pm

filter: Statementstmt 49.0% 147 / 300 Branchbran 23.1% 31 / 134 Conditioncond 29.0% 9 / 31 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 66.6% 20 / 30 Podpod 78.9% 15 / 19 totaltotal 42.1% 224 / 531 CC 93 SCAR 41.1
CC
93
Cov
40%
waitOnJobs
223.4
checkJob
90.0
pleaseExecute
74.6
CRAP 61.2
↑ Summary SGELK.log/qsub.435645.pl »
1 #! /usr/bin/env perl
2  
3 =pod
4  
5 =head1 NAME
6  
7 Schedule::SGELK
8  
9 =head1 SYNOPSIS
10  
11 A module for submitting jobs to an SGE queue.
12  
13 use Schedule::SGELK
14 my $sge=Schedule::SGELK->new(verbose=>1,numnodes=>5,numcpus=>8,workingdir=>"SGE/",waitForEachJobToStart=>1);
15 $sge->set("jobname","thisisaname");
16 # run a series of jobs and wait for them all to finish
17 my $job=$sge->pleaseExecute("sleep 60");
18 my $job2=$sge->pleaseExecute("sleep 60");
19 $sge->wrapItUp();
20 # or you can specify which jobs to wait for
21 $sge->waitOnJobs([$job,$job2],1); # 1 means wait for all jobs to finish; 0 to wait for any free node
22 # or in one step
23 $sge->pleaseExecute_andWait("sleep 60");
24  
25 A quick test for this module is the following one-liner
26  
27 perl -MSchedule::SGELK -e '$sge=Schedule::SGELK->new(numnodes=>5); for(1..3){$sge->pleaseExecute("sleep 3");}$sge->wrapItUp();'
28  
29 Another quick test is to use the test() method, if you want to see standardized text output (see test() below)
30  
31 perl -MSchedule::SGELK -e '$sge=Schedule::SGELK->new(-numnodes=>2,-numcpus=>8); $sge->test(\%tmpSettings);'
32
33  
34 =head1 DESCRIPTION
35  
36 A module for submitting jobs to an SGE queue. Monitoring is
37 performed using a combination of monitoring files
38 written to the hard drive and qstat.
39 Submitting is performed internally by making a perl script.
40  
41 =head1 AUTHOR
42  
43 Author: Lee Katz <lkatz@cdc.gov>
44  
45 =cut
46  
47 package Schedule::SGELK;
48 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use File::Basename qw/basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 1 ▶use File::Slurp qw/read_file write_file/;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 1 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 1 ▶use String::Escape qw/escape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 1 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 1 our $VERSION = version->declare("v1.6.0");
59  
60 1 my $has_threads=eval{
61 1 return 0; # this isn't working yet
62 0 require threads;
63 0 return 1;
64 };
65  
66 # some global variables
67 1 my @jobsToClean=();
68 1 my @jobsToMonitor=();
69 1 my $numSlots=0; # number of slots that are being used now
70  
71 4 ▶sub logmsg {local $0=basename $0;my $FH = *STDOUT; print $FH "$0: ".(caller(1))[3].": @_\n";}
Subroutine logmsg: called CC=1 SCAR=0.0
Pod undocumented
72 0 ▶local $SIG{'__DIE__'} = sub { my $e = $_[0]; $e =~ s/(at [^\s]+? line \d+\.$)/\nStopped $1/; die("$0: ".(caller(1))[3].": ".$e); };
Subroutine __ANON__: not called CC=1 SCAR=3.5
73 0 ▶local $SIG{INT} = sub{ cleanAllJobs(); };
Subroutine __ANON__: not called CC=1 SCAR=1.2
74  
75 # to be called when the script exits
76 1 ▶sub cleanAllJobs{
Subroutine cleanAllJobs: called CC=3 SCAR=11.1
Pod undocumented
77 1 ▶ return if(!@jobsToClean);
Branch: 1 branch
BranchTrueFalseTotal
unless @jobsToClean 0 1 1
78 1 logmsg "Cleaning all jobs";
79 1 for (@jobsToClean){
80 2 cleanAJob($_);
81 }
82 }
83 END{
84 cleanAllJobs();
85 }
86  
87 =pod
88  
89 =head2 METHODS
90  
91 =over
92  
93 =item sub new
94  
95 create a new instance of a scheduler.
96 Arguments and their defaults:
97  
98 numnodes=>50 maximum nodes to use
99 numcpus=>128 maximum cpus that will be used per node in a script
100 maxslots=>9999 maximum slots that you can use. Useful if you want to be limited by total slots instead of nodes or CPUs. E.g. {numnodes=>100,numcpus=>1,maxslots=>20}
101 verbose=>0
102 workingdir=>$ENV{PWD} a directory that all nodes can access to read/write jobs and log files
103 waitForEachJobToStart=>0 Allow each job to start as it's run (0), or to wait until the qstat sees the job before continuing (1)
104 jobname=>... This is the name given to the job when you view it with qstat. By default, it will be named after the script that calls this module.
105 warn_on_error=>1 This will make the script give a warning instead of exiting
106 qsubxopts=>... These are extra options to pass to qsub. E.g., {qsubxopts=>"-V"} Options are overwritten by appending them to the within-script options. Therefore this is not the best way to choose a different queue but it is a way to change a job name or the number of processors.
107 noqsub=>1 Force performing a system call instead of using qsub
108 queue=>all.q Choose the queue to use for a new job. Default: all.q
109  
110 Examples:
111 {numnodes=>100,numcpus=>1,maxslots=>50} # for many small jobs
112 {numnodes=>5,numcpus=>8,maxslots=>40} # for a few larger jobs (note: maxslots should be >= numnodes * maxslots
113  
114 =back
115  
116 =cut
117  
118 1 ▶sub new{
Subroutine new: called CC=11 SCAR=25.1
Pod documented
119 1 my($class,%args)=@_;
120 1 my $self=bless{},$class;
121  
122 # just start with verbosity = 0. This avoids an error in the >= checks
123 1 $self->{'settings'}={};
124 1 $self->{'error'}="";
125 1 $self->{'exit_code'}=0;
126  
127 # load up if we know what we have
128 1 foreach my $key (keys %args) {
129 6 my $nodash=$key;
130 6 $nodash =~ s/^\-//;
131 6 $self->set($nodash,$args{$key});
132 }
133  
134 # set defaults if they are not set
135 1 my %default=(numnodes=>50,numcpus=>128,verbose=>0,waitForEachJobToStart=>0,maxslots=>9999,queue=>"all.q",scheduler=>"SGE");
136 1 ▶ while(my($key,$value)=each(%default)){
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %default) 7 1 8
137 7 ▶ $self->settings($key,$value) if(!defined($self->settings($key)));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->settings($key) 3 4 7
138 }
139 1 ▶ if(!$self->get("workingdir")){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get("workingdir")) 0 1 1
140 0 $self->set("workingdir",$self->mktempdir());
141 0 logmsg "Working directory not set. Using ".$self->get("workingdir");
142 }
143  
144 # executables
145 1 for(qw(qsub qstat qdel)){
146 # See if it exists
147 3 my $exec=`which $_ 2>/dev/null`;
148 3 ▶ $exec="" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 3 0 3
149 3 chomp($exec);
150 3 $self->set($_,$exec);
151  
152 3 ▶ $self->set("scheduler","") if(!$exec);
Branch: 1 branch
BranchTrueFalseTotal
unless $exec 3 0 3
153 }
154  
155 # See if SGE is present
156 1 ▶ if($ENV{SGE_ROOT} && -e $ENV{SGE_ROOT}){
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'SGE_ROOT'} and -e $ENV{'SGE_ROOT'}) { } 0 1 1
Condition33%1/3
$ENV{"SGE_ROOT"} and -e $ENV{"SGE_ROOT"}
!ll&&!rl&&r
100
MC/DC0%0/2
$ENV{"SGE_ROOT"} and -e $ENV{"SGE_ROOT"}
$ENV{"SGE_ROOT"} -e $ENV{"SGE_ROOT"}
Truth table33%1/3
A and B
A= $ENV{"SGE_ROOT"}B= -e $ENV{"SGE_ROOT"}
ABresult
0X0
100
111
157 0 $self->set("scheduler","SGE");
158 } else{
159 1 logmsg "Env variable \$SGE_ROOT is not set. I will not use SGE";
160 1 $self->set("scheduler","");
161 1 $self->set("qsub","");
162 }
163  
164 # Remove the scheduler option if the user explicitly
165 # chooses not to use it.
166 1 ▶ $self->set("scheduler","") if($self->get("noqsub"));
Branch: 1 branch
BranchTrueFalseTotal
if $self->get("noqsub") 1 0 1
167  
168 1 return $self;
169 }
170  
171 =pod
172  
173 =over
174  
175 =item sub error($msg,$exit_code) or error()
176  
177 Get or set the error. Can set the error code too, if provided.
178  
179 =back
180  
181 =cut
182  
183 # Sets the error and returns the previous error message.
184 # Or, simply returns the current error message.
185 0 ▶sub error{
Subroutine error: not called CC=3 SCAR=24.8
Pod documented
186 0 my($self,$msg,$exit_code)=@_;
187 0 ▶ return $self->{error} if(!defined($msg));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
188 0 my $oldmsg=$self->{error};
189 0 $self->{error}=$msg;
190 0 ▶ $self->{exit_code}=$exit_code if(defined($exit_code));
Branch: 1 branch
BranchTrueFalseTotal
if defined $exit_code 0 0 0
191 0 return $oldmsg;
192 }
193 =pod
194  
195 =over
196  
197 =item sub set() get() settings()
198  
199 Get or set a setting. All settings are listed under sub new().
200 If a setting is provided without a value, then nothing new will be set,
201 and only the value of the specified setting will be returned.
202  
203 =back
204  
205 =cut
206  
207 37 ▶sub set{
Subroutine set: called CC=5 SCAR=16.2
Pod documented
208 37 my($self,$key,$value)=@_;
209 37 ▶ if(defined($key) && defined($value)){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $key and defined $value) { } 18 19 37
elsif (defined $key) { } 17 2 19
Condition100%3/3
defined $key and defined $value
!ll&&!rl&&r
21718
MC/DC100%2/2
defined $key and defined $value
defined $key defined $value
Truth table100%3/3
A and B
A= defined $keyB= defined $value
ABresult
0X0
100
111
210 18 $self->{'settings'}{$key}=$value;
211 } elsif (defined($key)){
212 17 return $self->{'settings'}{$key};
213 }
214 0 ▶ return %{$self->{'settings'}} if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 20 20
215 20 return $self->{'settings'};
216 }
217 # renaming of sub set()
218 8 ▶sub get{
Subroutine get: called CC=1 SCAR=0.0
Pod documented
219 8 my($self,@args)=@_;
220 8 return $self->set(@args);
221 }
222 # renaming of sub set()
223 14 ▶sub settings{
Subroutine settings: called CC=1 SCAR=0.0
Pod documented
224 14 my($self,@args)=@_;
225 14 return $self->set(@args);
226 }
227  
228 =pod
229  
230 =over
231  
232 =item pleaseExecute()
233  
234 This is the main method. It will submit a command to the cluster.
235  
236 $sge->set("jobname","a_nu_start");
237 $sge->pleaseExecute("someCommand with parameters");
238  
239 If you are already occupying more than numnodes, then it will pause before
240 executing the command. It will also create many files under workingdir, so
241 be sure to specify it. The workingdir is the current directory by default.
242  
243 You can also specify temporary settings for this one command with a referenced hash.
244  
245 $sge->pleaseExecute("someCommand with parameters",{jobname=>"a_nu_start",numcpus=>2});
246  
247 =back
248  
249 =cut
250  
251 2 ▶sub pleaseExecute{
Subroutine pleaseExecute: called CC=25 SCAR=43.1
Pod documented
252 2 my($self,$cmd,$tmpSettings)=@_;
253 2 local $0=basename $0;
254 2 my %settings=%{ $self->settings };
255  
256 # read in any temporary settings for this command
257 2 ▶ $tmpSettings={} if(!defined($tmpSettings));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmpSettings 2 0 2
258 2 $settings{$_}=$$tmpSettings{$_} for(keys(%$tmpSettings));
259  
260 # default settings for undefined settings
261 2 my $jobid=-1; # -1 is an error state
262 2 ▶ $settings{jobname}||="sgelk$0";
Condition50%1/2
$settings{"jobname"} ||= "sgelk$0"
l!l
02
MC/DC0%0/1
$settings{"jobname"} ||= "sgelk$0"
$settings{"jobname"}
Truth table50%1/2
A
A= $settings{"jobname"}
Aresult
11
00
263 2 ▶ $settings{logfile}||="$0.log";
Condition50%1/2
$settings{"logfile"} ||= "$0.log"
l!l
02
MC/DC0%0/1
$settings{"logfile"} ||= "$0.log"
$settings{"logfile"}
Truth table50%1/2
A
A= $settings{"logfile"}
Aresult
11
00
264 2 ▶ $settings{numcpus}||=1;
Condition50%1/2
$settings{"numcpus"} ||= 1
l!l
20
MC/DC0%0/1
$settings{"numcpus"} ||= 1
$settings{"numcpus"}
Truth table50%1/2
A
A= $settings{"numcpus"}
Aresult
11
00
265 2 ▶ $settings{timeout}||=60; # how long we will wait for qsub to start
Condition50%1/2
$settings{"timeout"} ||= 60
l!l
02
MC/DC0%0/1
$settings{"timeout"} ||= 60
$settings{"timeout"}
Truth table50%1/2
A
A= $settings{"timeout"}
Aresult
11
00
266 2 ▶ return 0 if($cmd=~/^\s*$/); # if there's no command, then no worries
Branch: 1 branch
BranchTrueFalseTotal
if $cmd =~ /^\s*$/ 0 2 2
267  
268 2 $self->waitOnJobs(\@jobsToMonitor,0); # wait until the job can be submitted
269  
270 2 my $rand=int(rand(999999));
271 2 my $tempdir=$settings{workingdir};
272 # create a perl script with the literal command in it
273 2 my $script="$tempdir/qsub.$rand.pl";
274  
275 2 ▶ my $prefix=($0 eq '-e')?"STDIN":$0;
Branch: 1 branch
BranchTrueFalseTotal
$0 eq '-e' ? : 0 2 2
276 2 $prefix="$settings{workingdir}/$prefix.$rand";
277 2 my($submitted,$running,$finished,$died,$output)=("$prefix.submitted", "$prefix.running", "$prefix.finished","$prefix.died","$prefix.log");
278
279 2 my $perl=`which perl`; chomp($perl);
280 2 ▶ open(SCRIPT,">",$script) or die "Could not write to temporary script $script: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SCRIPT, ">", $script or die "Could not write to temporary script ${script}: $!" 2 0 2
281 2 print SCRIPT "#! $perl\n\n";
282 # It has SGE params in it.
283 2 print SCRIPT "#\$ -N $settings{jobname}\n";
284 2 print SCRIPT "#\$ -S $perl\n";
285 2 print SCRIPT "#\$ -V\n";
286 2 print SCRIPT "#\$ -wd $ENV{PWD}\n";
287 2 print SCRIPT "#\$ -pe smp $settings{numcpus}\n";
288 2 print SCRIPT "#\$ -o $output\n";
289 2 print SCRIPT "#\$ -e $output\n";
290 2 print SCRIPT "#\$ -q $settings{queue}\n";
291 # qsubxopts get to be in here first but will be overwritten by later qsubopts below
292 2 ▶ if(my $opts=$settings{qsubxopts}){
Branch: 1 branch
BranchTrueFalseTotal
if (my $opts = $settings{"qsubxopts"}) 0 2 2
293 0 print SCRIPT "# options specified by qsubxopts are in the next line:\n";
294 0 print SCRIPT "#\$ $opts\n";
295 }
296 2 print SCRIPT "use strict;\nuse warnings;\n";
297 2 print SCRIPT "use File::Slurp qw/read_file write_file/;\n";
298  
299 # announces that it was submitted
300 2 my $sanitized=escape('qqbackslash',$cmd);
301 2 print SCRIPT "write_file('$submitted',$sanitized);\n";
302 # it runs the command
303 2 print SCRIPT "write_file('$running',$sanitized);\n";
304 2 print SCRIPT "system($sanitized);\n";
305 # let the script try one more time if it fails
306 #print SCRIPT "system($sanitized) if \$?;\n";
307 # print a parsable error if the script dies. This error will be in $output and in file $died
308 2 print SCRIPT <<END;
309 if(\$?){
310 my \$error=\"QSUB ERROR\\n\$?\\n\$!\";
311 write_file('$died',$sanitized);
312 write_file('$died',{append=>1},"\\n\$error\\n");
313 die \$error;
314 }
315 END
316 # announces when it is finished
317 2 print SCRIPT "write_file('$finished',$sanitized);\n";
318 2 close SCRIPT;
319 2 ▶ system("touch $script"); die if $?; # make the system close the script. Why isn't Perl closing it?
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 2 2
320 #system("cat $script");sleep 60;die;
321  
322 # now run the script and get the jobid
323 2 my %return=(submitted=>$submitted,running=>$running,finished=>$finished,died=>$died,tempdir=>$tempdir,output=>$output,cmd=>$cmd,script=>$script,jobname=>$settings{jobname},numcpus=>$settings{numcpus});
324 2 my $qsub=$self->get("qsub");
325 2 ▶ if(!$settings{scheduler}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($settings{"scheduler"}) 2 0 2
326 2 my $job=command("$perl $script",$has_threads,\%settings);
327 2 ▶ $return{thread}=$job if($has_threads);
Branch: 1 branch
BranchTrueFalseTotal
if $has_threads 0 2 2
328 2 ▶ $return{jobid}=$job->tid if($has_threads);
Branch: 1 branch
BranchTrueFalseTotal
if $has_threads 0 2 2
329 2 ▶ push(@jobsToClean,\%return) if(!$self->settings("keep"));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->settings("keep") 2 0 2
330 2 push(@jobsToMonitor,\%return);
331 2 $numSlots+=$settings{numcpus}; # claim these cpus
332 2 ▶ return %return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 2 2
333 2 return \%return;
334 }
335  
336 # At this point, qsub is on this computer. Submit the job.
337 0 my $out=`$qsub $script`; chomp($out);
338 0 ▶ if($out=~/Your job (\d+)/){
Branch: 1 branch
BranchTrueFalseTotal
if ($out =~ /Your job (\d+)/) { } 0 0 0
339 0 $jobid=$1;
340 0 $out.=" from $script";
341 0 ▶ logmsg $out if($settings{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $settings{"verbose"} 0 0 0
342 } else {
343 0 logmsg "WARNING: the last job submitted did not have an obvious jobid. It can't be tracked!";
344 }
345  
346 # monitor for the script to be running before moving on
347 0 my $started=time;
348 0 ▶ while(!-e $submitted){
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $submitted) 0 0 0
349 0 ▶ last if(!$self->settings("waitForEachJobToStart"));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->settings("waitForEachJobToStart") 0 0 0
350 0 sleep 1;
351 0 ▶ die "Command timed out!\n $cmd" if((time-$started)>$settings{timeout});
Branch: 1 branch
BranchTrueFalseTotal
if time - $started > $settings{"timeout"} 0 0 0
352 0 ▶ die "Command resulted in an error. qstat -j $jobid for more info\n $cmd" if($self->jobStatus($jobid) eq 'Eqw');
Branch: 1 branch
BranchTrueFalseTotal
if $self->jobStatus($jobid) eq "Eqw" 0 0 0
353 }
354  
355 # TODO create a link from the jobid to the random id
356
357 0 $return{jobid}=$jobid;
358 0 ▶ push(@jobsToClean,\%return) if(!$self->settings("keep"));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->settings("keep") 0 0 0
359 0 push(@jobsToMonitor,\%return);
360 0 $numSlots+=$settings{numcpus}; # claim these cpus
361 0 ▶ return %return if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
362 0 return \%return;
363 }
364  
365 =pod
366  
367 =over
368  
369 =item pleaseExecute_andWait()
370  
371 Exact same as pleaseExecute(), except it will wait for the command to finish
372 before continuing. Internally calls pleaseExecute() and then waitOnJobs().
373 However one key difference between pleaseExecute() and this sub is that you can
374 give a list of commands.
375  
376 # this will take 100 seconds because all commands have to finish.
377 $sge->pleaseExecute_andWait(["sleep 60","sleep 100","sleep 3"]);
378  
379 =back
380  
381 =cut
382  
383 0 ▶sub pleaseExecute_andWait{
Subroutine pleaseExecute_andWait: not called CC=3 SCAR=24.8
Pod documented
384 0 my($self,$cmd)=@_;
385 0 my %settings=$self->settings;
386 0 my $mustfinish=$self->settings("mustfinish"); # should be restored later
387 0 $self->set("mustfinish",0);
388 0 ▶ $cmd=[$cmd] if(ref($cmd) eq ""); # let cmd be a string but turn it into a list internally
Branch: 1 branch
BranchTrueFalseTotal
if ref $cmd eq "" 0 0 0
389 0 my(@jobid);
390 0 for(@$cmd){
391 0 my $jobid=$self->pleaseExecute($_);
392 0 push(@jobid,$jobid);
393 0 $self->waitOnJobs(\@jobid);
394 }
395 0 $self->waitOnJobs(\@jobid,1);
396 }
397  
398 =pod
399  
400 =over
401  
402 =item checkJob($jobHash)
403  
404 Checks the status of a given job. The job variable is obtained from pleaseExecute().
405 $self->error can be set if there is an error in the job. Return values:
406 1 for finished; 0 for still running or hasn't started; -1 for error.
407  
408 =back
409  
410 =cut
411  
412 0 ▶sub checkJob{
Subroutine checkJob: not called CC=9 SCAR=45.0
Pod documented
413 0 my($self,$job)=@_;
414 # See what the job status is {jobid} for fast checking
415 0 my $status=$self->jobStatus($$job{jobid});
416 0 ▶ if($status eq 'qw'){ # queued but not running
Branch: 3 branches
BranchTrueFalseTotal
if ($status eq 'qw') { } 0 0 0
elsif ($status eq 'Eqw') { } 0 0 0
elsif ($status =~ /[rt]/) { } 0 0 0
417 0 return 0;
418 } elsif($status eq 'Eqw'){ # error
419 0 $self->error("Command resulted in an error. qstat -j $$job{jobid} for more info\n $$job{cmd}");
420 0 return -1;
421 } elsif($status=~/[rt]/){ # running or is delayed
422 0 return 0;
423 }
424  
425 # look at files to check on the job status, for slower checking.
426 # see if the job has even started: {submitted}
427 0 ▶ return 0 if(!-e $$job{submitted});
Branch: 1 branch
BranchTrueFalseTotal
unless -e $job->{"submitted"} 0 0 0
428 # if the job finished, then great! {finished}
429 0 ▶ return 1 if(-e $$job{finished});
Branch: 1 branch
BranchTrueFalseTotal
if -e $job->{"finished"} 0 0 0
430 0 ▶ return 1 if(!keys(%$job)); # sometimes a job is blank... why?
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$job 0 0 0
431 # if the job died
432 0 ▶ if(-e $$job{died}){
Branch: 1 branch
BranchTrueFalseTotal
if (-e $job->{"died"}) 0 0 0
433 0 my @content=read_file($$job{output});
434 0 chomp(@content);
435 0 $self->error(join("\n",@content[-3..-1]));
436 0 return -1;
437 }
438 # It's running if the die-file isn't there and if the running file is there
439 0 ▶ return 0 if(-e $$job{running});
Branch: 1 branch
BranchTrueFalseTotal
if -e $job->{"running"} 0 0 0
440 0 logmsg "ERROR: Could not understand what the status is of job $$job{jobid}!\n".Dumper($job);
441 0 return -1;
442 }
443  
444 =pod
445  
446 =over
447  
448 =item jobStatus(jobid)
449  
450 Given an SGE job id, it returns its qstat status
451  
452 =back
453  
454 =cut
455  
456 0 ▶sub jobStatus{
Subroutine jobStatus: not called CC=4 SCAR=30.0
Pod documented
457 0 my($self,$jobid)=@_;
458 0 my $state=0;
459 0 ▶ $jobid||=0;
Condition0%0/2
$jobid ||= 0
l!l
00
MC/DC0%0/1
$jobid ||= 0
$jobid
Truth table0%0/2
A
A= $jobid
Aresult
11
00
460 0 my $qstat=$self->qstat;
461 0 for(split(/\n/,$qstat)){
462 0 my @F=split /\s+/;
463 0 ▶ if($F[0] eq $jobid){
Branch: 1 branch
BranchTrueFalseTotal
if ($F[0] eq $jobid) 0 0 0
464 0 $state=$F[4];
465 }
466 }
467 0 close QSTAT;
468 0 return $state;
469 }
470  
471 =pod
472  
473 =over
474  
475 =item qstat
476  
477 Runs qstat and caches the result for one second. Or, returns the cached result of qstat
478  
479 =back
480  
481 =cut
482  
483 0 ▶sub qstat{
Subroutine qstat: not called CC=3 SCAR=24.8
Pod documented
484 0 my($self)=@_;
485 # return the cached value if it was just accessed a second ago
486 #return $self->get("qstat") if(defined($self->get("qstat")) && $self->get("qstat_timestamp") <= time - 1);
487  
488 0 my $content="";
489 0 ▶ open(QSTAT,"qstat|") or die "ERROR: could not execute qstat! $!";
Branch: 1 branch
BranchTrueFalseTotal
open QSTAT, "qstat|" or die "ERROR: could not execute qstat! $!" 0 0 0
490 0 ▶ while(my $line=<QSTAT>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline QSTAT)) 0 0 0
491 0 $line=~s/^\s+|\s+$//g;
492 0 $content.="$line\n";
493 }
494 0 close QSTAT;
495 0 $self->set("qstat",$content);
496 0 $self->set("qstat_timestamp",time);
497 0 return $self->get("qstat");
498 }
499  
500 =pod
501  
502 =over
503  
504 =item wrapItUp()
505  
506 Waits on all jobs to finish before pausing the program.
507 Calls waitOnJobs or joinAllThreads internally. Does not take any parameters.
508  
509 =back
510  
511 =cut
512  
513 # Wait on all jobs to finish and clear out the queue.
514 1 ▶sub wrapItUp{
Subroutine wrapItUp: called CC=3 SCAR=13.3
Pod documented
515 1 my($self)=@_;
516 1 ▶ if($self->get("scheduler")){
Branch: 2 branches
BranchTrueFalseTotal
if ($self->get('scheduler')) { } 0 1 1
elsif ($has_threads) { } 0 1 1
517 0 $self->waitOnJobs(\@jobsToMonitor,1);
518 } elsif($has_threads){
519 0 $self->joinAllThreads(\@jobsToMonitor,1);
520 }
521 1 return 1;
522 }
523  
524 =pod
525  
526 =over
527  
528 =item joinAllThreads($jobList)
529  
530 Joins all threads. This is if you have ithreads and if the scheduler is not set.
531 For example, if you specify noqsub or if qsub executable is not found.
532  
533 =back
534  
535 =cut
536  
537 0 ▶sub joinAllThreads{
Subroutine joinAllThreads: not called CC=8 SCAR=42.8
Pod documented
538 0 my($self,$job)=@_;
539  
540 JOINALLTHREADS:
541 0 for my $j(@$job){
542 0 ▶ next if(!$$j{thread});
Branch: 1 branch
BranchTrueFalseTotal
unless $j->{"thread"} 0 0 0
543 0 ▶ next if($$j{thread} && !$$j{thread}->is_joinable);
Branch: 1 branch
BranchTrueFalseTotal
if $j->{"thread"} and not $j->{"thread"}->is_joinable 0 0 0
Condition0%0/3
$j->{"thread"} and not $j->{"thread"}->is_joinable
!ll&&!rl&&r
000
MC/DC0%0/2
$j->{"thread"} and not $j->{"thread"}->is_joinable
$j->{"thread"} not $j->{"thread"}->is_joinable
Truth table0%0/3
A and not(B)
A= $j->{"thread"}B= not $j->{"thread"}->is_joinable
ABresult
0X0
100
111
544 0 logmsg "Joining TID".$$j{jobid};
545 0 $$j{thread}->join;
546 0 $$j{thread}=0;
547 }
548  
549 # clean out the joined jobs
550 0 my @newjob;
551 0 for my $j(@$job){
552 0 ▶ push(@newjob,$j) if($$j{thread});
Branch: 1 branch
BranchTrueFalseTotal
if $j->{"thread"} 0 0 0
553 }
554 0 $job=\@newjob;
555  
556 # if there is still something in @$job, then go for another round
557 0 ▶ if(@$job){
Branch: 1 branch
BranchTrueFalseTotal
if (@$job) 0 0 0
558 0 logmsg "Waiting for ".scalar(@$job)." more jobs to finish...";
559 0 sleep 1;
560 0 goto JOINALLTHREADS;
561 }
562  
563 }
564  
565  
566 =pod
567  
568 =over
569  
570 =item waitOnJobs($jobList,[$mustFinish])
571  
572 Waits on all given jobs to finish. The job list are jobs as given by pleaseExecute().
573 If $mustFinish evaluates to true, then the program will pause until
574 all jobs are finished.
575 Calls on checkJob() internally. Will die with an error message if a job dies.
576  
577 =back
578  
579 =cut
580  
581 # Wait on enough jobs to finish before returning.
582 # If a job finishes, splice it from the job array.
583 3 ▶sub waitOnJobs{
Subroutine waitOnJobs: called CC=16 SCAR=54.1
Pod documented
584 3 my($self,$job,$mustfinish)=@_;
585
586 # if there is no qsub, then every job is only going one at a time
587 3 my $qsub=$self->get("qsub");
588 3 ▶ return @$job if(!$qsub);
Branch: 1 branch
BranchTrueFalseTotal
unless $qsub 3 0 3
589  
590 0 my %settings=$self->settings;
591 0 ▶ $settings{mustfinish}=$mustfinish if(defined($mustfinish));
Branch: 1 branch
BranchTrueFalseTotal
if defined $mustfinish 0 0 0
592 0 ▶ if($settings{verbose}){
Branch: 1 branch
BranchTrueFalseTotal
if ($settings{"verbose"}) 0 0 0
593 0 ▶ logmsg "We have reached node capacity ($settings{numnodes})! Waiting for a job to finish." if(@$job >= $settings{numnodes});
Branch: 1 branch
BranchTrueFalseTotal
if @$job >= $settings{"numnodes"} 0 0 0
594 0 ▶ logmsg "We have reached slot capacity ($settings{maxslots})! Waiting for a job to finish." if($numSlots >= $settings{maxslots});
Branch: 1 branch
BranchTrueFalseTotal
if $numSlots >= $settings{"maxslots"} 0 0 0
595 }
596 0 ▶ while(@$job > 0){
Branch: 1 branch
BranchTrueFalseTotal
if (@$job > 0) 0 0 0
597 0 ▶ for(my $i=0;$i<@$job;$i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$job) 0 0 0
598 0 ▶ $$job[$i]{jobid}||=0;
Condition0%0/2
$job->[$i]{"jobid"} ||= 0
l!l
00
MC/DC0%0/1
$job->[$i]{"jobid"} ||= 0
$job->[$i]{"jobid"}
Truth table0%0/2
A
A= $job->[$i]{"jobid"}
Aresult
11
00
599 0 my $state=$self->checkJob($$job[$i]);
600 0 ▶ if($state==1){
Branch: 2 branches
BranchTrueFalseTotal
if ($state == 1) { } 0 0 0
elsif ($state == -1) { } 0 0 0
601 0 ▶ logmsg "A job finished: $$job[$i]{jobname} ($$job[$i]{jobid})" if($settings{verbose});
Branch: 1 branch
BranchTrueFalseTotal
if $settings{"verbose"} 0 0 0
602 0 $numSlots = $numSlots - $$job[$i]{numcpus}; # not using these slots anymore
603 0 splice(@$job,$i,1);
604 0 last;
605 } elsif($state==-1){
606 0 my $msg="A job failed ($$job[$i]{jobname} [$$job[$i]{jobid}]! Look at $$job[$i]{output} for more details.\nError message was ".$self->error()."\n".Dumper($$job[$i]);
607 0 ▶ die $msg if(!$settings{warn_on_error});
Branch: 1 branch
BranchTrueFalseTotal
unless $settings{"warn_on_error"} 0 0 0
608 # just print the warning if the script didn't die and forget about this dead job
609 0 logmsg $msg;
610 0 $numSlots = $numSlots - $$job[$i]{numcpus}; # not using these slots anymore
611 0 $self->error($msg);
612 0 splice(@$job,$i,1);
613 0 last;
614 }
615 }
616 0 sleep 1;
617 # break out if you don't have to finish yet but you can still add in another job
618 0 ▶ last if(!$settings{mustfinish} && @$job<$settings{numnodes} && $numSlots<$settings{maxslots});
Branch: 1 branch
BranchTrueFalseTotal
if not $settings{"mustfinish"} and @$job < $settings{"numnodes"} and $numSlots < $settings{"maxslots"} 0 0 0
Condition0%0/6
not $settings{"mustfinish"} and @$job < $settings{"numnodes"} and $numSlots < $settings{"maxslots"}
!ll&&!rl&&r
000
not $settings{"mustfinish"} and @$job < $settings{"numnodes"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $settings{"mustfinish"} and @$job < $settings{"numnodes"} and $numSlots < $settings{"maxslots"}
not $settings{"mustfinish"} @$job < $settings{"numnodes"} $numSlots < $settings{"maxslots"}
Truth table0%0/4
not(A) and B and C
A= not $settings{"mustfinish"}B= @$job < $settings{"numnodes"}C= $numSlots < $settings{"maxslots"}
ABCresult
0XX0
10X0
1100
1111
619 }
620 0 return @$job;
621 }
622  
623 =pod
624  
625 =over
626  
627 =item cleanAJob
628  
629 This is internally used for cleaning up files after a job is done.
630 Do not use externally.
631  
632 =back
633  
634 =cut
635  
636 2 ▶sub cleanAJob{
Subroutine cleanAJob: called CC=3 SCAR=17.3
Pod documented
637 2 my($job)=@_;
638 2 ▶ my $jobid=$$job{jobid} || return 0;
Condition50%1/2
$job->{'jobid'} || (return 0)
l!l
02
MC/DC0%0/1
$job->{'jobid'} || (return 0)
$job->{'jobid'}
Truth table50%1/2
A
A= $job->{'jobid'}
Aresult
11
00
639 0 logmsg $jobid;
640 0 for (qw(running submitted finished output script died)){
641 0 unlink $$job{$_};
642 }
643  
644 0 system("qdel $$job{jobid} 2>/dev/null | grep -v 'does not exist'");
645 #die "Internal error" if $?;
646 0 return 1;
647 }
648  
649 0 ▶sub mktempdir{
Subroutine mktempdir: not called CC=4 SCAR=30.0
Pod undocumented
650 0 my ($self,$settings) = @_;
651 0 ▶ $settings||={};
Condition0%0/2
$settings ||= {}
l!l
00
MC/DC0%0/1
$settings ||= {}
$settings
Truth table0%0/2
A
A= $settings
Aresult
11
00
652 # SGELK.22623.XXXXX
653 #my $tempdir_path = File::Spec->join(File::Spec->tmpdir(), (split("::",(caller(1))[3]))[1].".$$.XXXXX");
654 0 ▶ mkdir "./.SGELK" if(!-d "./.SGELK");
Branch: 1 branch
BranchTrueFalseTotal
unless -d "./.SGELK" 0 0 0
655 0 ▶ die if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
656 0 my $tempdir_path = File::Spec->join("./.SGELK",(split("::",(caller(1))[3]))[1].".$$.XXXXX");
657 0 my $tempdir = tempdir($tempdir_path, CLEANUP => !($$settings{keep}));
658 0 return $tempdir;
659 }
660  
661 2 ▶sub command{
Subroutine command: called CC=3 SCAR=11.4
Pod undocumented
662 2 my($cmd,$use_threads,$settings)=@_;
663 2 my $job=0;
664 2 ▶ if($use_threads){
Branch: 1 branch
BranchTrueFalseTotal
if ($use_threads) { } 0 2 2
665 0 $job=threads->new(\&command,$cmd,0,$settings);
666 } else {
667 2 logmsg "Running $cmd";
668 2 system($cmd);
669 2 ▶ die "ERROR with command: $!\n $cmd" if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 2 2
670 }
671  
672 2 return $job;
673 }
674  
675 =pod
676  
677 =over
678  
679 =item test
680  
681 Use this method to perform a test. The test sends
682 ten jobs that print debugging information.
683  
684 You can give an optional hash argument to send other settings as described in new().
685  
686 perl -MSchedule::SGELK -e '$sge=Schedule::SGELK->new(-numnodes=>2,-numcpus=>8); $sge->test(\%tmpSettings);'
687  
688 =back
689  
690 =cut
691  
692 0 ▶sub test{
Subroutine test: not called CC=5 SCAR=34.0
Pod documented
693 0 my($self,$tmpSettings)=@_;
694  
695 # get settings
696 0 my %settings=%{ $self->settings };
697 # read in any temporary settings for this command
698 0 ▶ $tmpSettings={} if(!defined($tmpSettings));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tmpSettings 0 0 0
699 0 $$tmpSettings{verbose}=1; # make sure it's verbose for debugging
700 0 $settings{$_}=$$tmpSettings{$_} for(keys(%$tmpSettings));
701  
702 # execute the jobs
703 0 for(1..$self->get("numnodes")){
704 0 logmsg "Job $_ is being submitted";
705 0 my $text="Job count\t$_\n";
706 0 $text.="$_\t$settings{$_}\n" for(keys(%settings));
707 0 $self->pleaseExecute("echo '$text'|column -t",$tmpSettings);
708 }
709 0 $self->wrapItUp();
710 0 return 1;
711 }
712  
713 1 1;
↑ Summary SGELK.log/qsub.435645.pl »