blib/lib/XAS/Lib/Batch/Interface/Torque.pm untested

filter: Statementstmt 0.0% 0 / 254 Branchbran 0.0% 0 / 84 Conditioncond 0.0% 0 / 11 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 22 totaltotal 0.0% 0 / 395 CC 43 SCAR 45.1
CC
43
Cov
0%
do_job_alter
272.0
_create_jobfile
110.0
_parse_output
42.0
CRAP 90.9
1 package XAS::Lib::Batch::Interface::Torque;
2  
3 our $VERSION = '0.01';
4  
5 use Params::Validate qw/HASHREF ARRAYREF/;
6  
7 use XAS::Class
8 debug => 0,
9 version => $VERSION,
10 base => 'XAS::Base',
11 utils => 'trim',
12 mixins => 'do_job_sub do_job_stat do_job_del do_job_sig do_job_hold
13 do_job_rls do_job_move do_job_msg do_job_rerun do_queue_stat
14 do_queue_stop do_queue_start do_server_stat do_server_enable
15 do_server_disable',
16 constant => {
17 QSUB => '/usr/bin/qsub',
18 QSTAT => '/usr/bin/qstat',
19 QDEL => '/usr/bin/qdel',
20 QSIG => '/usr/bin/qsig',
21 QHOLD => '/usr/bin/qhold',
22 QRLS => '/usr/bin/qrls',
23 QMSG => '/usr/bin/qmsg',
24 QMOVE => '/usr/bin/qmove',
25 QRERUN => '/usr/bin/qrerun',
26 QALTER => '/usr/bin/qalter',
27 QSTOP => '/usr/bin/qstop',
28 QSTART => '/usr/bin/qstart',
29 QENABLE => '/usr/bin/qenable',
30 QDISABLE => '/usr/bin/qdisable',
31 },
32 ;
33  
34 #use Data::Dumper;
35  
36 # ----------------------------------------------------------------------
37 # Public Methods
38 # ----------------------------------------------------------------------
39  
40 sub do_job_sub {
41 my $self = shift;
42 my ($p) = $self->validate_params(\@_, [
43 { type => HASHREF }
44 ]);
45
46 my $queue = _create_queue($self, $p->{'queue'}, $p->{'host'});
47 my $cmd = sprintf('%s -q %s %s', QSUB, $queue, $p->{'jobfile'});
48  
49 _create_jobfile($self, $p);
50  
51 my $output = $self->do_cmd($cmd, 'qsub');
52  
53 return trim($output->[0]);
54  
55 }
56  
57 sub do_job_stat {
58 my $self = shift;
59 my ($p) = $self->validate_params(\@_, [
60 { type => HASHREF }
61 ]);
62  
63 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
64 my $cmd = sprintf('%s -f1 %s', QSTAT, $jobid);
65 my $output = $self->do_cmd($cmd, 'qstat');
66 my $stat = _parse_output($self, $output);
67  
68 return $stat->{$p->{'job'}};
69  
70 }
71  
72 sub do_job_del {
73 my $self = shift;
74 my ($p) = $self->validate_params(\@_, [
75 { type => HASHREF }
76 ]);
77  
78 my $cmd;
79 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
80  
81 $cmd = sprintf('%s', QDEL);
82 $cmd .= sprintf(' -p') if (defined($p->{'force'}));
83 $cmd .= sprintf(' -m "%s"', $p->{'message'}) if (defined($p->{'message'}));
84 $cmd .= sprintf(' %s', $jobid);
85  
86 my $output = $self->do_cmd($cmd, 'qdel');
87  
88 return 1;
89  
90 }
91  
92 sub do_job_sig {
93 my $self = shift;
94 my ($p) = $self->validate_params(\@_, [
95 { type => HASHREF }
96 ]);
97  
98 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
99 my $cmd = sprintf('%s -s %s %s', QSIG, $p->{'signal'}, $jobid);
100 my $output = $self->do_cmd($cmd, 'qsig');
101  
102 return 1;
103  
104 }
105  
106 sub do_job_hold {
107 my $self = shift;
108 my ($p) = $self->validate_params(\@_, [
109 { type => HASHREF }
110 ]);
111  
112 my $cmd;
113 my $hold;
114 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
115  
116 foreach my $x (split(DELIMITER, $p->{'type'})) {
117  
118 $hold .= 'u' if ($x eq 'user');
119 $hold .= 'o' if ($x eq 'other');
120 $hold .= 's' if ($x eq 'system');
121  
122 }
123  
124 $cmd = sprintf('%s -h %s %s', QHOLD, $hold, $jobid);
125  
126 my $output = $self->do_cmd($cmd, 'qhold');
127  
128 return 1;
129  
130 }
131  
132 sub do_job_rls {
133 my $self = shift;
134 my ($p) = $self->validate_params(\@_, [
135 { type => HASHREF }
136 ]);
137  
138 my $cmd;
139 my $hold;
140 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
141  
142 foreach my $x (split(DELIMITER, $p->{'type'})) {
143  
144 $hold .= 'u' if ($x eq 'user');
145 $hold .= 'o' if ($x eq 'other');
146 $hold .= 's' if ($x eq 'system');
147  
148 }
149  
150 $cmd = sprintf('%s -h %s %s', QRLS, $hold, $jobid);
151  
152 my $output = $self->do_cmd($cmd, 'qrls');
153  
154 return 1;
155  
156 }
157  
158 sub do_job_move {
159 my $self = shift;
160 my ($p) = $self->validate_params(\@_, [
161 { type => HASHREF }
162 ]);
163  
164 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
165 my $queue = _create_queue($self, $p->{'queue'}, $p->{'dhost'});
166 my $cmd = sprintf('%s %s %s', QMOVE, $queue, $jobid);
167 my $output = $self->do_cmd($cmd, 'qmove');
168  
169 return 1;
170  
171 }
172  
173 sub do_job_msg {
174 my $self = shift;
175 my ($p) = $self->validate_params(\@_, [
176 { type => HASHREF }
177 ]);
178  
179 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
180 my $cmd = sprintf('%s -%s "%s" %s', QMSG, $p->{'output'}, $p->{'message'}, $jobid);
181 my $output = $self->do_cmd($cmd, 'qmsg');
182  
183 return 1;
184  
185 }
186  
187 sub do_job_rerun {
188 my $self = shift;
189 my ($p) = $self->validate_params(\@_, [
190 { type => HASHREF }
191 ]);
192  
193 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
194 my $cmd = sprintf('%s %s', QRERUN, $jobid);
195 my $output = $self->do_cmd($cmd, 'qrerun');
196  
197 return 1;
198  
199 }
200  
201 sub do_job_alter {
202 my $self = shift;
203 my ($p) = $self->validate_params(\@_, [
204 { type => HASHREF }
205 ]);
206  
207 my $cmd;
208 my $after = _create_after($self, $p->{'after'});
209 my $jobid = _create_jobid($self, $p->{'job'}, $p->{'host'});
210 my $outpath = _create_path($self, $p->{'out_path'}, $p->{'host'});
211 my $errpath = _create_path($self, $p->{'error_path'}, $p->{'host'});
212  
213 $cmd .= sprintf('%s ', QALTER);
214 $cmd .= sprintf('-a %s ', $after) if (defined($p->{'after'}));
215 $cmd .= sprintf('-A %s ', $p->{'account'}) if (defined($p->{'account'}));
216 $cmd .= sprintf('-e "%s" ', $errpath) if (defined($p->{'error_path'}));
217 $cmd .= sprintf('-h ') if (defined($p->{'hold'}));
218 $cmd .= sprintf('-l "%s" ', $p->{'resources'}) if (defined($p->{'resources'}));
219 $cmd .= sprintf('-m %s ', $p->{'mail_points'}) if (defined($p->{'mail_points'}));
220 $cmd .= sprintf('-M "%s" ', $p->{'email'}) if (defined($p->{'email'}));
221 $cmd .= sprintf('-n ') if (defined($p->{'exclusive'}));
222 $cmd .= sprintf('-N %s ', $p->{'jobname'}) if (defined($p->{'jobname'}));
223 $cmd .= sprintf('-o "%s" ', $outpath) if (defined($p->{'out_path'}));
224 $cmd .= sprintf('-p %s ', $p->{'priority'}) if (defined($p->{'priority'}));
225 $cmd .= sprintf('-r %s ', $p->{'rerunable'}) if (defined($p->{'rerunnable'}));
226 $cmd .= sprintf('-S "%s" ', $p->{'shell_path'}) if (defined($p->{'shell_path'}));
227 $cmd .= sprintf('-u "%s" ', $p->{'user'}) if (defined($p->{'user'}));
228 $cmd .= sprintf('-W "%s" ', $p->{'attributes'}) if (defined($p->{'attributes'}));
229 $cmd .= sprintf('%s', $jobid);
230  
231 my $output = $self->do_cmd($cmd, 'qalter');
232  
233 return 1;
234  
235 }
236  
237 sub do_queue_stat {
238 my $self = shift;
239 my ($p) = $self->validate_params(\@_, [
240 { type => HASHREF }
241 ]);
242  
243 my $queue = _create_queue($self, $p->{'queue'}, $p->{'host'});
244 my $cmd = sprintf('%s -Q -f1 %s', QSTAT, $queue);
245 my $output = $self->do_cmd($cmd, 'qstat');
246 my $stat = _parse_output($self, $output);
247  
248 return $stat;
249  
250 }
251  
252 sub do_queue_stop {
253 my $self = shift;
254 my ($p) = $self->validate_params(\@_, [
255 { type => HASHREF }
256 ]);
257  
258 my $queue = _create_queue($self, $p->{'queue'}, $p->{'host'});
259 my $cmd = sprintf('%s -Q -f1 %s', QSTOP, $queue);
260 my $output = $self->do_cmd($cmd, 'qstop');
261 my $stat = _parse_output($self, $output);
262  
263 return $stat;
264  
265 }
266  
267 sub do_queue_start {
268 my $self = shift;
269 my ($p) = $self->validate_params(\@_, [
270 { type => HASHREF }
271 ]);
272  
273 my $queue = _create_queue($self, $p->{'queue'}, $p->{'host'});
274 my $cmd = sprintf('%s -Q -f1 %s', QSTART, $queue);
275 my $output = $self->do_cmd($cmd, 'qstart');
276 my $stat = _parse_output($self, $output);
277  
278 return $stat;
279  
280 }
281  
282 sub do_server_stat {
283 my $self = shift;
284 my ($p) = $self->validate_params(\@_, [
285 { type => HASHREF }
286 ]);
287  
288 my $cmd = sprintf('%s -B -f1 %s', QSTAT, $p->{'host'});
289 my $output = $self->do_cmd($cmd, 'qstat');
290 my $stat = _parse_output($self, $output);
291  
292 return $stat;
293  
294 }
295  
296 sub do_server_disable {
297 my $self = shift;
298 my ($p) = $self->validate_params(\@_, [
299 { type => HASHREF }
300 ]);
301  
302 my $queue = _create_queue($self, $p->{'queue'}, $p->{'host'});
303 my $cmd = sprintf('%s %s', QDISABLE, $queue);
304 my $output = $self->do_cmd($cmd, 'qdisable');
305 my $stat = _parse_output($self, $output);
306  
307 return $stat;
308  
309 }
310  
311 sub do_server_enable {
312 my $self = shift;
313 my ($p) = $self->validate_params(\@_, [
314 { type => HASHREF }
315 ]);
316  
317 my $queue = _create_queue($self, $p->{'queue'}, $p->{'host'});
318 my $cmd = sprintf('%s %s', QENABLE, $queue);
319 my $output = $self->do_cmd($cmd, 'qenable');
320 my $stat = _parse_output($self, $output);
321  
322 return $stat;
323  
324 }
325  
326 # ----------------------------------------------------------------------
327 # Private Methods
328 # ----------------------------------------------------------------------
329  
330 sub _create_jobid {
331 my $self = shift;
332 my ($id, $host) = $self->validate_params(\@_, [
333 1,
334 { optional => 1, default => undef }
335 ]);
336  
337 my $jobid;
338  
339 if (defined($host)) {
340  
341 $jobid = sprintf("%s\@%s", $id, $host);
342  
343 } else {
344  
345 $jobid = $id;
346  
347 }
348  
349 return $jobid;
350  
351 }
352  
353 sub _create_queue {
354 my $self = shift;
355 my ($queue, $host) = $self->validate_params(\@_, [
356 { optional => 1, default => undef },
357 { optional => 1, default => undef },
358 ]);
359  
360 my $que;
361  
362 if (defined($host) && defined($queue)) {
363  
364 $que = sprintf("%s\@%s", $queue, $host);
365  
366 } elsif (defined($host)) {
367  
368 $que = sprintf("@%s", $host);
369  
370 } else {
371  
372 $que = $queue || '';
373  
374 }
375  
376 return $que;
377  
378 }
379  
380 sub _create_path {
381 my $self = shift;
382 my $path = shift || undef;
383 my $host = shift || undef;
384  
385 my $fqpath;
386  
387 if (defined($host)) {
388  
389 $fqpath = sprintf("%s:%s", $host, $path);
390  
391 } else {
392  
393 $fqpath = $path;
394  
395 }
396  
397 return $fqpath;
398  
399 }
400  
401 sub _create_after {
402 my $self = shift;
403 my $after = shift || undef;
404  
405 if (defined($after)) {
406  
407 $after = $after->strftime('%Y%m%d%H%M');
408  
409 }
410  
411 return $after;
412  
413 }
414  
415 sub _parse_output {
416 my $self = shift;
417 my ($output) = $self->validate_params(\@_, [
418 { type => ARRAYREF }
419 ]);
420  
421 my $id;
422 my $stat;
423  
424 foreach my $line (@$output) {
425  
426 next if ($line eq '');
427  
428 $line = trim($line);
429  
430 if ($line =~ /^Job Id/) {
431  
432 ($id) = ($line =~ m/^Job Id\:\s(.*)/);
433 $id = trim($id);
434 next;
435  
436 }
437  
438 if ($line =~ /^Queue/) {
439  
440 ($id) = ($line =~ m/^Queue\:\s(.*)/);
441 $id = trim($id);
442 next;
443  
444 }
445  
446 if ($line =~ /^Server/) {
447  
448 ($id) = ($line =~ m/^Server\:\s(.*)/);
449 $id = trim($id);
450 next;
451  
452 }
453  
454 next if (index($line, '=') < 0);
455  
456 my ($key, $value) = split('=', $line, 2);
457  
458 $key = trim(lc($key));
459 $key =~ s/\./_/;
460  
461 $stat->{$id}->{$key} = trim($value);
462  
463 }
464  
465 return $stat;
466  
467 }
468  
469 sub _create_jobfile {
470 my $self = shift;
471 my ($p) = $self->validate_params(\@_, [
472 { type => HASHREF }
473 ]);
474  
475 my $job = $p->{'jobfile'};
476 my $fh = $job->open('w');
477 my $after = _create_after($self, $p->{'after'});
478 my $logfile = _create_path($self, $p->{'logfile'}, $p->{'host'});
479  
480 $fh->printf("#!/bin/sh\n");
481 $fh->printf("#\n");
482 $fh->printf("#PBS -N \"%s\"\n", $p->{'jobname'});
483 $fh->printf("#PBS -j \"%s\"\n", $p->{'join_path'});
484 $fh->printf("#PBS -e \"%s\"\n", $logfile);
485 $fh->printf("#PBS -o \"%s\"\n", $logfile);
486 $fh->printf("#PBS -m \"%s\"\n", $p->{'mail_points'});
487 $fh->printf("#PBS -M \"%s\"\n", $p->{'email'});
488 $fh->printf("#PBS -S \"%s\"\n", $p->{'shell_path'});
489 $fh->printf("#PBS -w %s\n", $p->{'work_path'});
490 $fh->printf("#PBS -d %s\n", $p->{'work_path'});
491 $fh->printf("#PBS -p %s\n", $p->{'priority'});
492 $fh->printf("#PBS -r %s\n", $p->{'rerunable'});
493 $fh->printf("#PBS -u \"%s\"\n", $p->{'user'}) if (defined($p->{'user'}));
494 $fh->printf("#PBS -A %s\n", $p->{'account'}) if (defined($p->{'account'}));
495 $fh->printf("#PBS -a %s\n", $after) if (defined($p->{'after'}));
496 $fh->printf("#PBS -l \"%s\"\n", $p->{'resources'}) if (defined($p->{'resources'}));
497 $fh->printf("#PBS -W \"%s\"\n", $p->{'attributes'}) if (defined($p->{'attributes'}));
498 $fh->printf("#PBS -v \"%s\"\n", $p->{'environment'}) if (defined($p->{'environment'}));
499 $fh->printf("#PBS -n \n") if (defined($p->{'exclusive'}));
500 $fh->printf("#PBS -h \n") if (defined($p->{'hold'}));
501 $fh->printf("#PBS -V \n") if (defined($p->{'env_export'}));
502 $fh->printf("#\n");
503 $fh->printf("%s\n", $p->{'command'});
504 $fh->printf("#\n");
505 $fh->printf("exit \$?\n");
506  
507 $fh->close;
508  
509 }
510  
511 1;
512  
513 __END__
514  
515 =head1 NAME
516  
517 XAS::Lib::Batch::Interface::Torque - A mixin for the XAS environment
518  
519 =head1 SYNOPSIS
520  
521 use XAS::Class
522 debug => 0,
523 version => '0.01',
524 base => 'XAS::Base',
525 mixin => 'XAS::Lib::Batch::Interface::Torque'
526 ;
527  
528 =head1 DESCRIPTION
529  
530 In the Unix world, there is a standardized interface for Batch Systems. In
531 honor of this standard, each implementation has a slightly different command
532 line syntax. This mixin implements the PBS/Torque command line.
533  
534 =head1 SEE ALSO
535  
536 =over 4
537  
538 =item L<XAS::Lib::Batch|XAS::Lib::Batch>
539  
540 =item L<XAS|XAS>
541  
542 =back
543  
544 =head1 AUTHOR
545  
546 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
547  
548 =head1 COPYRIGHT AND LICENSE
549  
550 Copyright (c) 2015 Kevin L. Esteb
551  
552 This is free software; you can redistribute it and/or modify it under
553 the terms of the Artistic License 2.0. For details, see the full text
554 of the license at http://www.perlfoundation.org/artistic_license_2_0.
555  
556 =cut