blib/lib/XAS/Lib/Batch/Job.pm untested

filter: Statementstmt 0.0% 0 / 39 Branchbran n/a Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 66 CC 1 SCAR 6.9
CC
1
Cov
0%
qsub
2.0
qstat
2.0
qdel
2.0
CRAP 2.0
1 package XAS::Lib::Batch::Job;
2  
3 our $VERSION = '0.01';
4  
5 use XAS::Class
6 debug => 0,
7 version => $VERSION,
8 base => 'XAS::Lib::Batch',
9 filesystem => 'Dir',
10 constants => 'DELIMITER',
11 constant => {
12 TYPES => qr/user|other|system|none|,|\s/,
13 JTYPES => qr/oe|eo|n|,|\s/,
14 }
15 ;
16  
17 # ----------------------------------------------------------------------
18 # Public Methods
19 # ----------------------------------------------------------------------
20  
21 sub qsub {
22 my $self = shift;
23 my $p = $self->validate_params(\@_, {
24 -jobname => 1,
25 -queue => 1,
26 -email => 1,
27 -command => 1,
28 -jobfile => { isa => 'Badger::Filesystem::File' },
29 -logfile => { isa => 'Badger::Filesystem::File' },
30 -rerunable => { optional => 1, default => 'n' },
31 -join_path => { optional => 1, default => 'oe' },
32 -account => { optional => 1, default => undef },
33 -attributes => { optional => 1, default => undef },
34 -environment => { optional => 1, default => undef },
35 -env_export => { optional => 1, default => undef },
36 -exclusive => { optional => 1, default => undef },
37 -hold => { optional => 1, default => undef },
38 -resources => { optional => 1, default => undef },
39 -user => { optional => 1, default => undef },
40 -host => { optional => 1, default => undef },
41 -mail_points => { optional => 1, default => 'bea' },
42 -after => { optional => 1, isa => 'DateTime' },
43 -shell_path => { optional => 1, default => '/bin/sh' },
44 -work_path => { optional => 1, isa => 'Badger::Filesystem::Directory', default => Dir('/', 'tmp') },
45 -priority => { optional => 1, default => 0, callbacks => {
46 'out of priority range' =>
47 sub { $_[0] > -1024 && $_[0] < 1024; },
48 }}
49 });
50  
51 return $self->do_job_sub($p);
52  
53 }
54  
55 sub qstat {
56 my $self = shift;
57 my $p = $self->validate_params(\@_, {
58 -job => 1,
59 -host => { optional => 1, default => undef },
60 });
61  
62 return $self->do_job_stat($p);
63  
64 }
65  
66 sub qdel {
67 my $self = shift;
68 my $p = $self->validate_params(\@_, {
69 -job => 1,
70 -host => { optional => 1, default => undef },
71 -force => { optional => 1, default => undef },
72 -message => { optional => 1, default => undef },
73 });
74  
75 return $self->do_job_del($p);
76  
77 }
78  
79 sub qsig {
80 my $self = shift;
81 my $p = $self->validate_params(\@_, {
82 -job => 1,
83 -signal => 1,
84 -host => { optional => 1, default => undef },
85 });
86  
87 return $self->do_job_sig($p);
88  
89 }
90  
91 sub qhold {
92 my $self = shift;
93 my $p = $self->validate_params(\@_, {
94 -job => 1,
95 -type => { regex => TYPES },
96 -host => { optional => 1, default => undef },
97 });
98  
99 return $self->do_job_hold($p);
100  
101 }
102  
103 sub qrls {
104 my $self = shift;
105 my $p = $self->validate_params(\@_, {
106 -job => 1,
107 -type => { regex => TYPES },
108 -host => { optional => 1, default => undef },
109 });
110  
111 return $self->do_job_rls($p);
112  
113 }
114  
115 sub qmove {
116 my $self = shift;
117 my $p = $self->validate_params(\@_, {
118 -job => 1,
119 -queue => 1,
120 -host => { optional => 1, default => undef },
121 -dhost => { optional => 1, default => undef },
122 });
123  
124 return $self->do_job_move($p);
125  
126 }
127  
128 sub qmsg {
129 my $self = shift;
130 my $p = $self->validate_params(\@_, {
131 -job => 1,
132 -message => 1,
133 -output => { regex => /E|O/ },
134 -host => { optional => 1, default => undef },
135 });
136  
137 return $self->do_job_msg($p);
138  
139 }
140  
141 sub qrerun {
142 my $self = shift;
143 my $p = $self->validate_params(\@_, {
144 -job => 1,
145 -host => { optional => 1, default => undef },
146 });
147  
148 return $self->do_job_rerun($p);
149  
150 }
151  
152 sub qalter {
153 my $self = shift;
154 my $p = $self->validate_params(\@_, {
155 -job => 1,
156 -jobname => { optional => 1, default => undef },
157 -rerunable => { optional => 1, default => undef },
158 -email => { optional => 1, default => undef },
159 -account => { optional => 1, default => undef },
160 -attributes => { optional => 1, default => undef },
161 -exclusive => { optional => 1, default => undef },
162 -resources => { optional => 1, default => undef },
163 -user => { optional => 1, default => undef },
164 -host => { optional => 1, default => undef },
165 -mail_points => { optional => 1, default => undef },
166 -shell_path => { optional => 1, default => undef },
167 -hold => { optional => 1, default => undef, regex => TYPES },
168 -join_path => { optional => 1, default => undef, regex => JTYPES },
169 -after => { optional => 1, default => undef, isa => 'DateTime' },
170 -out_path => { optional => 1, default => undef, isa => 'Badger::Filesystem::File' },
171 -error_path => { optional => 1, default => undef, isa => 'Badger::Filesystem::File' },
172 -priority => { optional => 1, default => 0, callbacks => {
173 'out of priority range' =>
174 sub { $_[0] > -1024 && $_[0] < 1024; },
175 }}
176 });
177  
178 return $self->do_job_alter($p);
179  
180 }
181  
182 # ----------------------------------------------------------------------
183 # Private Methods
184 # ----------------------------------------------------------------------
185  
186 1;
187  
188 __END__
189  
190 =head1 NAME
191  
192 XAS::Lib::Batch::Job - A class for the XAS environment
193  
194 =head1 SYNOPSIS
195  
196 use XAS::Lib::Batch::Job;
197  
198 my $job = XAS::Lib::Batch::Job->new();
199  
200 my $id = $job->qsub(...);
201
202 printf("job %s has started\n", $id);
203  
204 while (my $stat = $job->qstat(-job => $id)) {
205  
206 if ($stat->{job_state} eq 'C') {
207  
208 printf("job %s has finished\n", $id);
209  
210 }
211  
212 sleep 10;
213  
214 }
215  
216 =head1 DESCRIPTION
217  
218 This module provides an interface for manipulating jobs in a Batch System.
219 Each available method is a wrapper around a given command. A command line
220 is built, executed, and the return code is checked. If the return code is
221 non-zero an exception is thrown. The exception will include the return code
222 and the first line from stderr.
223  
224 Since each method is a wrapper, there is a corresponding man page for the
225 actual command. They should also be checked when problems arise.
226  
227 =head1 METHODS
228  
229 =head2 new
230  
231 This method initializes the module and takes these parameters:
232  
233 =over 4
234  
235 =item B<-interface>
236  
237 The command line interface to use. This defaults to 'XAS::Lib::Batch::Interface::Torque'.
238  
239 =back
240  
241 =head2 qsub(...)
242  
243 This method will submit a job to the batch system and returns the jobs ID. It
244 takes the following parameters:
245  
246 =over 4
247  
248 =item B<-jobname>
249  
250 The name of the job.
251  
252 =item B<-queue>
253  
254 The queue to run the job on.
255  
256 =item B<-email>
257  
258 The email address to send status reports too. There can be more then one
259 address or this can be a mailing list.
260  
261 =item B<-command>
262  
263 The command to run.
264  
265 =item B<-jobfile>
266  
267 The name of the job file to use. This needs to be a Badger::Filesystem::File
268 object.
269  
270 =item B<-logfile>
271  
272 The log file to use. This needs to be a Badger::Filesystem::File object.
273  
274 =item B<-rerunable>
275  
276 Wither the job is rerunnable, this is optional and default to no.
277  
278 =item B<-join_path>
279  
280 Wither to join stdout and stderr into one log file, this is optional
281 and defaults to 'oe'.
282  
283 =item B<-account>
284  
285 The optional account to run under.
286  
287 =item B<-attributes>
288  
289 The optinal attributes that may be applied to this job. This should be a
290 comma seperated list of name value pairs.
291  
292 =item B<-environment>
293  
294 The optional environment variables that may be defined for the job. This
295 should be a comma sperated list of name value pairs.
296  
297 =item B<-env_export>
298  
299 Wither to export the users environment.
300  
301 =item B<-exclusive>
302  
303 The option to run this job exclusivily. Default is no.
304  
305 =item B<-hold>
306  
307 The option to submit this job in a hold state.
308  
309 =item B<-resources>
310  
311 Optional resources to associate with this job. This should be a comma
312 seperated list of name value pairs.
313  
314 =item B<-user>
315  
316 The optional user account to run this job under.
317  
318 =item B<-host>
319  
320 The optional host to run this job on.
321  
322 =item B<-mail_points>
323  
324 The optional mail points that the user will be notified at. Defaults to 'bea'.
325  
326 =item B<-after>
327  
328 The optional time to run the job after. This must be a DateTime object.
329  
330 =item B<-shell_path>
331  
332 The optional path to the jobs shell. Defaults to /bin/sh.
333  
334 =item B<-work_path>
335  
336 The optional path to put work files. This must be a Badger::Filesystem::Directory.
337 Defaults to /tmp.
338  
339 =item B<-priority>
340  
341 The optional priority to run the job at. Defaults to 0.
342  
343 =back
344  
345 =head2 qstat(...)
346  
347 This method returns that status of a job. This status will be a hash reference
348 of the parsed output on stdout. It takes the following paramters:
349  
350 =over 4
351  
352 =item B<-job>
353  
354 The ID of the job, as returned from submit().
355  
356 =item B<-host>
357  
358 The optional host that the job may be running on. Defaults to 'localhost'.
359  
360 =back
361  
362 =head2 qdel(...)
363  
364 This method will delete a job. It takes the following parameters:
365  
366 =over 4
367  
368 =item B<-job>
369  
370 The ID of the job, as returned from submit().
371  
372 =item B<-host>
373  
374 The optional host that the job may be running on. Defaults to 'localhost'.
375  
376 =item B<-force>
377  
378 Wither to force the jobs deletion. Defaults to no.
379  
380 =item B<-message>
381  
382 The optional message to be placed into the log file.
383  
384 =back
385  
386 =head2 qsig(...)
387  
388 This method will send a signal to a job. It takes the following parameters:
389  
390 =over 4
391  
392 =item B<-job>
393  
394 The ID of the job, as returned from submit().
395  
396 =item B<-host>
397  
398 The optional host that the job may be running on. Defaults to 'localhost'.
399  
400 =item B<-signal>
401  
402 The signal to send to the job.
403  
404 =back
405  
406 =head2 qhold(...)
407  
408 This method will place a job into a hold status. It takes the following
409 parameters:
410  
411 =over 4
412  
413 =item B<-job>
414  
415 The ID of the job, as returned from submit().
416  
417 =item B<-host>
418  
419 The optional host that the job may be running on. Defaults to 'localhost'.
420  
421 =item B<-type>
422  
423 The type of hold to place on the job. They can be any of the following:
424  
425 user, other, system, none
426
427 If more then one type is used, they need to be comma seperated.
428  
429 =back
430  
431 =head2 qrls(...)
432  
433 This method will release a job that was placed into a hold status. It takes
434 the following parameters:
435  
436 =over 4
437  
438 =item B<-job>
439  
440 The ID of the job, as returned from submit().
441  
442 =item B<-host>
443  
444 The optional host that the job may be running on. Defaults to 'localhost'.
445  
446 =item B<-type>
447  
448 The type of hold to place on the job. They can be any of the following:
449  
450 user, other, system, none
451
452 If more then one type is used, they need to be comma seperated.
453  
454 =back
455  
456 =head2 qmove(...)
457  
458 This method will move a job from one queue to another. That queue may exist on
459 another host. It takes the following parameters:
460  
461 =over 4
462  
463 =item B<-job>
464  
465 The ID of the job, as returned from submit().
466  
467 =item B<-queue>
468  
469 The queue to move the job too.
470  
471 =item B<-host>
472  
473 The optional host that the job may be running on. Defaults to 'localhost'.
474  
475 =item B<-dhost>
476  
477 The optional destination host that the queue is on.
478  
479 =back
480  
481 =head2 qmsg(...)
482  
483 This method will place a message into the log file of a job. It takes
484 the following parameters:
485  
486 =over 4
487  
488 =item B<-job>
489  
490 The ID of the job, as returned from submit().
491  
492 =item B<-host>
493  
494 The optional host that the job may be running on. Defaults to 'localhost'.
495  
496 =item B<-message>
497  
498 The message to be used.
499  
500 =item B<-output>
501  
502 The log to place the message, It can be one of the following:
503  
504 E - stderr
505 O - stdout
506  
507 =back
508  
509 =head2 qrerun(...)
510  
511 This method will attempt to rerun a job. It takes the following parameters:
512  
513 =over 4
514  
515 =item B<-job>
516  
517 The ID of the job, as returned from submit().
518  
519 =item B<-host>
520  
521 The optional host that the job may be running on. Defaults to 'localhost'.
522  
523 =back
524  
525 =head2 qalter(...)
526  
527 This method will alter the parameters of a job. It takes the following
528 parameters:
529  
530 =over 4
531  
532 =item B<-job>
533  
534 The ID of the job, as returned from submit().
535  
536 =item B<-host>
537  
538 The optional host that the job may be running on. Defaults to 'localhost'.
539  
540 =item B<-jobname>
541  
542 This will change the jobs name.
543  
544 =item B<-rerunable>
545  
546 This will change the jobs rerunable status.
547  
548 =item B<-email>
549  
550 This will change the jobs email destinations.
551  
552 =item B<-account>
553  
554 This will change the jobs account.
555  
556 =item B<-attributes>
557  
558 This will change the optional job attributes.
559  
560 =item B<-exclusive>
561  
562 This will change wither the job has exclusive access to the server.
563  
564 =item B<-resources>
565  
566 This will change the jobs optional resources.
567  
568 =item B<-user>
569  
570 This will change the jobs user.
571  
572 =item B<-mail_points>
573  
574 This will change the jobs mail points.
575  
576 =item B<-shell_path>
577  
578 This will change the jobs shell.
579  
580 =item B<-hold>
581  
582 This will hold the job.
583  
584 =item B<-join_path>
585  
586 This will change the jobs join path.
587  
588 =item B<-after>
589  
590 This will change the time that job will run after.
591  
592 =item B<-out_path>
593  
594 This will changes the jobs output path, it must be a Badger::Filesystem::File object.
595  
596 =item B<-error_path>
597  
598 This will changes the jobs error path, it must be a Badger::Filesystem::File object.
599  
600 =item B<-priority>
601  
602 This will change the jobs priority.
603  
604 =back
605  
606 =head1 SEE ALSO
607  
608 =over 4
609  
610 =item L<XAS::Lib::Batch|XAS::Lib::Batch>
611  
612 =item L<XAS|XAS>
613  
614 =back
615  
616 =head1 AUTHOR
617  
618 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
619  
620 =head1 COPYRIGHT AND LICENSE
621  
622 Copyright (c) 2015 Kevin L. Esteb
623  
624 This is free software; you can redistribute it and/or modify it under
625 the terms of the Artistic License 2.0. For details, see the full text
626 of the license at http://www.perlfoundation.org/artistic_license_2_0.
627  
628 =cut