| 1 | package HPCD::SGE::DRMAAWrapper; | ||
| 2 | |||
| 3 | ### INCLUDES ################################################################## | ||
| 4 | |||
| 5 | # safe Perl | ||
| 6 | use warnings; | ||
| 7 | use strict; | ||
| 8 | use Carp; | ||
| 9 | use Data::Dumper; | ||
| 10 | use Schedule::DRMAAc qw( :all ); | ||
| 11 | |||
| 12 | use Moose::Role; | ||
| 13 | |||
| 14 | # most drmaa functions return (error,*other vals*, diagnosis) | ||
| 15 | # the error and diagnosis need to be tested to check for failure | ||
| 16 | # we wrap these functions with code that throws an exception on failure | ||
| 17 | # and simpy returns the *other vals* | ||
| 18 | |||
| 19 | BEGIN { | ||
| 20 | # If *other vals* is a single value, we provide scalar context to the result | ||
| 21 | eval "sub O$_ { shift; dt1( $_( \@_ ) ) }" | ||
| 22 | for qw( | ||
| 23 | drmaa_allocate_job_template | ||
| 24 | drmaa_run_job | ||
| 25 | drmaa_get_attribute_names | ||
| 26 | drmaa_get_attribute | ||
| 27 | drmaa_wifexited | ||
| 28 | drmaa_wexitstatus | ||
| 29 | drmaa_wifaborted | ||
| 30 | drmaa_wcoredump | ||
| 31 | drmaa_wifsignaled | ||
| 32 | drmaa_wtermsig | ||
| 33 | ); | ||
| 34 | |||
| 35 | # If *other vals* is multiple values or void, we provide scalar context to the result | ||
| 36 | eval "sub O$_ { shift; dt( $_( \@_ ) ) }" | ||
| 37 | for qw( | ||
| 38 | drmaa_set_attribute | ||
| 39 | drmaa_set_vector_attribute | ||
| 40 | drmaa_control | ||
| 41 | ); | ||
| 42 | }; | ||
| 43 | |||
| 44 | # iterator functions return (error,value) where error is usually a flag to | ||
| 45 | # denote whether a value is being returned or the end of the iterator was | ||
| 46 | # reached (or some real error condition, like an invalid iterator) | ||
| 47 | # These we wrap with code that returns (continue, value) where continue is true | ||
| 48 | # if there is a value, or false if the iterator is empty; or it throws an | ||
| 49 | # exception on other types of error. | ||
| 50 | my @drmaa_iter = qw( | ||
| 51 | drmaa_get_next_attr_name | ||
| 52 | drmaa_get_next_attr_value | ||
| 53 | ); | ||
| 54 | |||
| 55 | use Schedule::DRMAAc qw( :all ); | ||
| 56 | |||
| 57 | ######## DRMAA function wrappers | ||
| 58 | # pretend that they are methods to get the convenience of the around wrapper | ||
| 59 | # mechanism, but since they are actually functions, don't pass the $self arg | ||
| 60 | # | ||
| 61 | # The DRMAA functions all return a list, the first element is an error indicator | ||
| 62 | # which has the value $DRMAA_ERRNO_SUCCESS if all worked well and an error code | ||
| 63 | # value otherwise, and the final list element is a diagnosis string. In between | ||
| 64 | # the real return value (or values) are also returned (but they are only meaningful | ||
| 65 | # if there was no error. | ||
| 66 | # | ||
| 67 | # The around wrappers below cause these to throw an exception if there was an error | ||
| 68 | # and otherwise remove the excess return values. There are two types - depending | ||
| 69 | # upon whether the function, after removing the error baggage, returns one element | ||
| 70 | # or multiple. We make sure that the single element functions return in scalar | ||
| 71 | # context now so that the calling looks sensible. | ||
| 72 | |||
| 73 | sub dt { # drmaa_trap for long - abort on errors or strip error baggage, return list | ||
| 74 | my $error = shift; | ||
| 75 | my $diagnosis = pop; | ||
| 76 | confess "DRMAA error(" . drmaa_strerror($error) . "), diagnosis ($diagnosis)" | ||
| 77 | if $error != $DRMAA_ERRNO_SUCCESS; | ||
| 78 | return @_; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub dt1 { # convert the list return from dt to a scalar | ||
| 82 | (dt(@_))[0]; | ||
| 83 | } | ||
| 84 | |||
| 85 | my $init_count; | ||
| 86 | |||
| 87 | before 'BUILD' => sub { | ||
| 88 | dt( drmaa_init( undef ) ) unless $init_count++; | ||
| 89 | }; | ||
| 90 | |||
| 91 | sub Odrmaa_wait { | ||
| 92 | shift; | ||
| 93 | my @ret = drmaa_wait( @_ ); | ||
| 94 | if ($ret[0] == $DRMAA_ERRNO_EXIT_TIMEOUT) { | ||
| 95 | $ret[0] = $DRMAA_ERRNO_SUCCESS; | ||
| 96 | $ret[1] = undef; | ||
| 97 | } | ||
| 98 | else { | ||
| 99 | $ret[1] .= ': ** THIS IS USUALLY A TIMING ERROR IN THE DRMAA LIBRARY' | ||
| 100 | . ' INTERACTION WITH SGE. TRY RE-RUNNING THE PROGRAM. IT' | ||
| 101 | . ' WILL USUALLY WORK CORRECTLY **' | ||
| 102 | if defined $ret[1] | ||
| 103 | and $ret[1] =~ /no usage information was returned for the completed job$/; | ||
| 104 | } | ||
| 105 | dt( @ret ); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub Odrmaa_get_next_attr_value { | ||
| 109 | shift; | ||
| 110 | my( $continue, $value ) = drmaa_get_next_attr_value( @_ ); | ||
| 111 | return ( $continue == $DRMAA_ERRNO_SUCCESS, $value ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub Odrmaa_get_next_attr_name { | ||
| 115 | shift; | ||
| 116 | my( $continue, $value ) = drmaa_get_next_attr_name( @_ ); | ||
| 117 | return ( $continue == $DRMAA_ERRNO_SUCCESS, $value ); | ||
| 118 | } | ||
| 119 | |||
| 120 | 1; |