blib/lib/HPC/Runner/Command/submit_jobs/Utils/Scheduler/Submit.pm

filter: Statementstmt 17.2% 19 / 110 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.1% 6 / 13 Podpod 66.6% 4 / 6 totaltotal 16.8% 29 / 172 CC 22 SCAR 39.5
CC
22
Cov
13%
submit_to_scheduler
110.0
process_submit_command
72.0
create_version_str
12.0
CRAP 52.1
« blib/lib/HPC/Runner/Command/execute_job/Utils/Log.pm ↑ Summary blib/lib/HPC/Runner/Command/execute_job/Utils/MCE.pm »
1 package HPC::Runner::Command::submit_jobs::Utils::Scheduler::Submit;
2  
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head3 process_submit_command
11  
12 splitting this off from the main command
13  
14 DEPRACATED process_batch_command
15  
16 Command that hpcrunner.pl execute_job/execute_array uses
17  
18 =cut
19  
20 sub process_submit_command {
21 0 ▶ my $self = shift;
Subroutine process_submit_command: not called CC=8 SCAR=42.8
Pod documented
22 0 my $counter = shift;
23  
24 0 my $command = "";
25  
26 ##TODO Discuss changing the log name to just the jobname
27 0 my $logname = $self->create_log_name($counter);
28 0 $self->jobs->{ $self->current_job }->add_lognames($logname);
29  
30 0 $command = "sleep 20\n";
31 0 $command .= "cd " . getcwd() . "\n";
32 0 ▶ if ( $self->has_custom_command ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_custom_command) { } 0 0 0
33 0 $command .= $self->custom_command . " \\\n";
34 }
35 else {
36 0 $command .= "hpcrunner.pl " . $self->subcommand . " \\\n";
37 }
38  
39 0 ▶ $command .= "\t--project " . $self->project . " \\\n" if $self->has_project;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_project 0 0 0
40  
41 0 my $batch_index_start = $self->gen_batch_index_str;
42  
43 0 my $log = "";
44 0 ▶ if ( $self->no_log_json ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->no_log_json) 0 0 0
45 0 $log = "\t--no_log_json \\\n";
46 }
47  
48 $command .=
49 "\t--infile "
50 . $self->cmdfile . " \\\n"
51 . "\t--basedir "
52 . $self->basedir . " \\\n"
53 . "\t--commands "
54 . $self->jobs->{ $self->current_job }->commands_per_node . " \\\n"
55 . "\t--batch_index_start "
56 . $self->gen_batch_index_str . " \\\n"
57 . "\t--procs "
58 0 . $self->jobs->{ $self->current_job }->procs . " \\\n"
59 . "\t--logname "
60 . $logname . " \\\n"
61 . $log
62 . "\t--data_dir "
63 . $self->data_dir . " \\\n"
64 . "\t--process_table "
65 . $self->process_table;
66  
67 #TODO Update metastring to give array index
68 my $metastr =
69 $self->job_stats->create_meta_str( $counter, $self->batch_counter,
70 $self->current_job, $self->use_batches,
71 0 $self->jobs->{ $self->current_job } );
72  
73 0 ▶ $command .= " \\\n\t" if $metastr;
Branch: 1 branch
BranchTrueFalseTotal
if $metastr 0 0 0
74 0 ▶ $command .= $metastr if $metastr;
Branch: 1 branch
BranchTrueFalseTotal
if $metastr 0 0 0
75  
76 0 my $pluginstr = $self->create_plugin_str;
77 0 ▶ $command .= $pluginstr if $pluginstr;
Branch: 1 branch
BranchTrueFalseTotal
if $pluginstr 0 0 0
78  
79 0 my $version_str = $self->create_version_str;
80 0 ▶ $command .= $version_str if $version_str;
Branch: 1 branch
BranchTrueFalseTotal
if $version_str 0 0 0
81  
82 0 $command .= "\n\n";
83 0 return $command;
84 }
85  
86 sub create_log_name {
87 0 ▶ my $self = shift;
Subroutine create_log_name: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my $counter = shift;
89  
90 0 my $logname;
91  
92 0 ▶ if ( $self->has_project ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_project) { } 0 0 0
93 0 $logname = $self->project . "_" . $counter . "_" . $self->current_job;
94 }
95 else {
96 0 $logname = $counter . "_" . $self->current_job;
97 }
98  
99 0 return $logname;
100 }
101  
102 =head3 create_version_str
103  
104 If there is a version add it
105  
106 =cut
107  
108 #TODO Move to git
109  
110 sub create_version_str {
111 0 ▶ my $self = shift;
Subroutine create_version_str: not called CC=3 SCAR=24.8
Pod documented
112  
113 0 my $version_str = "";
114  
115 0 ▶ if ( $self->has_git && $self->has_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_git and $self->has_version) 0 0 0
Condition0%0/3
$self->has_git and $self->has_version
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_git and $self->has_version
$self->has_git $self->has_version
Truth table0%0/3
A and B
A= $self->has_gitB= $self->has_version
ABresult
0X0
100
111
116 0 $version_str .= " \\\n\t";
117 0 $version_str .= "--version " . $self->version;
118 }
119  
120 0 return $version_str;
121 }
122  
123 =head3 process_template
124  
125 =cut
126  
127 sub process_template {
128 0 ▶ my $self = shift;
Subroutine process_template: not called CC=3 SCAR=24.8
Pod documented
129 0 my $counter = shift;
130 0 my $command = shift;
131 0 my $ok = shift;
132 0 my $array_str = shift;
133  
134 0 my $jobname = $self->resolve_project($counter);
135  
136 $self->template->process(
137 $self->jobs->{$self->current_job}->template_file,
138 {
139 JOBNAME => $jobname,
140 USER => $self->user,
141 COMMAND => $command,
142 ARRAY_STR => $array_str,
143 AFTEROK => $ok,
144 MODULES => $self->jobs->{ $self->current_job }->join_modules(' '),
145 OUT => $self->logdir
146 . "/$counter" . "_"
147 . $self->current_job . ".log",
148 0 ▶ job => $self->jobs->{ $self->current_job },
Branch: 1 branch
BranchTrueFalseTotal
$self->template->process($self->jobs->{$self->current_job}->template_file, {"JOBNAME", $jobname, "USER", $self->user, "COMMAND", $command, "ARRAY_STR", $array_str, "AFTEROK", $ok, "MODULES", $self->jobs->{$self->current_job}->join_modules(" "), "OUT", $self->logdir . "/$counter" . '_' . $self->current_job . '.log', "job", $self->jobs->{$self->current_job}}, $self->slurmfile) or die $self->template->error 0 0 0
149 },
150 $self->slurmfile
151 ) || die $self->template->error;
152  
153 0 chmod 0777, $self->slurmfile;
154  
155 0 my $scheduler_id;
156 try {
157 0 ▶ $scheduler_id = $self->submit_jobs;
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 };
159  
160 0 ▶ if ( defined $scheduler_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scheduler_id) { } 0 0 0
161 0 $self->jobs->{ $self->current_job }->add_scheduler_ids($scheduler_id);
162 }
163 else {
164 0 $self->jobs->{ $self->current_job }->add_scheduler_ids('000xxx');
165 }
166 }
167  
168 =head3 submit_to_scheduler
169  
170 Submit the job to the scheduler.
171  
172 Inputs: self, submit_command (sbatch, qsub, etc)
173  
174 Returns: exitcode, stdout, stderr
175  
176 This subroutine was just about 100% from the following perlmonks discussions. All that I did was add in some logging.
177  
178 http://www.perlmonks.org/?node_id=151886
179  
180 This is probably overkill - but occasionally the scheduler takes longer than we think to exit
181  
182 =cut
183  
184 sub submit_to_scheduler {
185 0 ▶ my $self = shift;
Subroutine submit_to_scheduler: not called CC=10 SCAR=47.0
Pod documented
186 0 my $submit_command = shift;
187  
188 0 my ( $infh, $outfh, $errfh, $exitcode, $cmdpid, $stdout, $stderr );
189 0 $errfh = gensym();
190 0 eval {
191  
192 0 $cmdpid = open3( $infh, $outfh, $errfh, $submit_command );
193 };
194 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
195 0 $exitcode = $?;
196 0 $stderr = $@;
197 0 $cmdpid = 0;
198 0 $self->app_log->fatal( 'Cmd failed : ' . $submit_command );
199 0 $self->app_log->fatal($@);
200 }
201  
202 0 $infh->autoflush();
203 0 ▶ return [ $exitcode, '', $stderr, ] if $exitcode;
Branch: 1 branch
BranchTrueFalseTotal
if $exitcode 0 0 0
204  
205 0 my $sel = new IO::Select; # create a select object
206 0 $sel->add( $outfh, $errfh ); # and add the fhs
207  
208 0 ▶ while ( my @ready = $sel->can_read ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@ready) = $sel->can_read) 0 0 0
209 0 foreach my $fh (@ready) { # loop through them
210 0 my $line;
211 0 my $len = sysread $fh, $line, 4096;
212 0 ▶ next unless defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
213 0 ▶ if ( $len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) { } 0 0 0
214 0 $sel->remove($fh);
215 0 close($fh);
216 }
217 else { # we read data alright
218 0 ▶ if ( $fh == $outfh ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fh == $outfh) { } 0 0 0
elsif ($fh == $errfh) { } 0 0 0
219 0 $stdout .= $line;
220 }
221 elsif ( $fh == $errfh ) {
222 0 $stderr .= $line;
223 }
224 }
225 }
226 }
227  
228 0 ▶ waitpid( $cmdpid, 1 ) if $cmdpid;
Branch: 1 branch
BranchTrueFalseTotal
if $cmdpid 0 0 0
229 0 $exitcode = $?;
230  
231 0 $sel->remove($outfh);
232 0 $sel->remove($infh);
233  
234 0 return ( $exitcode, $stdout, $stderr );
235 }
236  
237 sub job_failure {
238 0 ▶ my $self = shift;
Subroutine job_failure: not called CC=1 SCAR=6.9
Pod undocumented
239  
240 0 $self->log->warn( "Submit scripts will be written, "
241 . "but will not be submitted to the queue." );
242 0 $self->log->warn(
243 "Any pending jobs that depend upon this job will NOT be submitted to the queue."
244 );
245 0 $self->log->warn(
246 "Please look at your submission scripts in " . $self->outdir );
247 0 $self->log->warn(
248 "And your logs in " . $self->logdir . "\nfor more information" );
249 0 $self->log->warn(
250 "Task dependencies are not calculated until the end of submission ... please to do not exit unless you are sure!"
251 );
252 0 $self->jobs->{ $self->current_job }->submission_failure(1);
253 }
254  
255 1 1;
« blib/lib/HPC/Runner/Command/execute_job/Utils/Log.pm ↑ Summary blib/lib/HPC/Runner/Command/execute_job/Utils/MCE.pm »