blib/lib/HPCI/Run.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 134 CC 3 SCAR 16.7
CC
3
Cov
0%
_base_register_status
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 5.3
1 package HPCI::Run;
2  
3 ### INCLUDES ##################################################################
4  
5 # safe Perl
6 use warnings;
7 use strict;
8 use Carp;
9  
10 use Config;
11  
12 use MooseX::Role::Parameterized;
13 use MooseX::Types::Path::Class qw(Dir File);
14  
15 parameter theDriver => (
16 isa => 'Str',
17 required => 1,
18 );
19  
20 die "No sigs?" unless $Config{sig_name} && $Config{sig_num};
21  
22 my %sig_num;
23 my @sig_name;
24  
25 @sig_num{split ' ', $Config{sig_name}} = split ' ', $Config{sig_num};
26 while (my($k,$v) = each %sig_num) {
27 $sig_name[$v] ||= $k;
28 }
29  
30 role {
31 my $p = shift;
32 my $theDriver = $p->theDriver;
33 my $StageClass = "${theDriver}::Stage";
34  
35 # do nothing, but let roles write before/after/around wrappers
36 method BUILD => sub {
37 };
38  
39 has 'stage' => (
40 is => 'ro',
41 required => 1,
42 isa => $StageClass,
43 weak_ref => 1,
44 handles => {
45 debug => 'debug',
46 info => 'info',
47 warn => 'warn',
48 error => 'error',
49 fatal => 'fatal',
50 },
51 );
52  
53 has 'index' => (
54 is => 'ro',
55 isa => 'Int',
56 required => 1,
57 );
58  
59 has 'pid' => (
60 is => 'rw',
61 isa => 'Int',
62 );
63  
64 has 'unique_id' => (
65 is => 'ro',
66 isa => 'Str',
67 lazy => 1,
68 init_arg => undef,
69 default => sub {
70 my $self = shift;
71 my $stage = $self->stage;
72 return $stage->_sub_name( $self->index );
73 },
74 );
75  
76 has 'dir' => (
77 is => 'ro',
78 isa => Dir,
79 init_arg => undef,
80 lazy => 1,
81 default => sub {
82 my $self = shift;
83 my $stage_dir = $self->stage->stage_dir;
84 my $run_dir = $stage_dir->subdir( $self->index );
85 HPCI::_trigger_mkdir( $self, $run_dir );
86 my $sym = $stage_dir->file('final_retry');
87 unlink $sym if -e $sym;
88 symlink $self->index, $sym;
89 return $run_dir;
90 },
91 );
92  
93 has '_stdout' => (
94 is => 'ro',
95 isa => File,
96 init_arg => undef,
97 lazy => 1,
98 default => sub {
99 my $self = shift;
100 $self->dir->file( 'stdout' );
101 },
102 );
103  
104 has '_stderr' => (
105 is => 'ro',
106 isa => File,
107 init_arg => undef,
108 lazy => 1,
109 default => sub {
110 my $self = shift;
111 $self->dir->file( 'stderr' );
112 },
113 );
114  
115 has 'status' => ( # exit code if job exitted normally
116 is => 'rw',
117 isa => 'Str',
118 init_arg => undef,
119 lazy => 1,
120 default => 0,
121 );
122  
123 has 'kill' => ( # true if job was killed
124 is => 'rw',
125 isa => 'Str',
126 init_arg => undef,
127 lazy => 1,
128 default => 0,
129 );
130  
131 has 'killsignal' => ( # kill signal if job was killed
132 is => 'rw',
133 isa => 'Str',
134 init_arg => undef,
135 lazy => 1,
136 default => 0,
137 );
138  
139 has 'abort' => ( # abort info if job was not able to start
140 is => 'rw',
141 isa => 'Str',
142 init_arg => undef,
143 lazy => 1,
144 default => 0,
145 );
146  
147 has 'stats' => (
148 is => 'ro',
149 isa => 'HashRef[Str]',
150 lazy => 1,
151 init_arg => undef,
152 default => sub {
153 my $self = shift;
154 return {
155 map { $_ => 'unknown' } (@{ $self->stats_keys }, 'exit_status')
156 }
157 },
158 );
159  
160 has '_analysis_chose_retry' => (
161 is => 'rw',
162 isa => 'Int',
163 lazy => 1,
164 init_arg => undef,
165 default => 0,
166 );
167  
168 method soft_timeout => sub {
169 my $self = shift;
170 $self->info( "Sending soft timeout: " . $self->unique_id );
171 my $pid = $self->pid;
172 kill 'USR1', $self->pid;
173 };
174  
175 method hard_timeout => sub {
176 my $self = shift;
177 my $pid = $self->pid;
178 $self->info( "Sending hard timeout: " . $self->unique_id );
179 kill 'KILL', $self->pid;
180 };
181  
182 method _submit_command => sub {
183 my $self = shift;
184 my $cmd = shift;
185 my $pid = fork;
186  
187 # normal parent
188 return $pid if $pid;
189  
190 # parent after fork failure
191 $self->_croak( "Fork failed: $!" ) unless defined $pid;
192  
193 # child
194 setpgrp 0, $$;
195 exec $cmd or die "Exec failed ($!) on command: $cmd\n";
196 };
197  
198 before '_collect_job_stats' => sub {
199 my $self = shift;
200 my $status = shift;
201  
202 $self->_base_register_status( $status );
203 };
204  
205 sub _base_register_status {
206 my $self = shift;
207 my $status = shift;
208 my $stath = $self->stats;
209 $stath->{exit_status} = $status;
210 if ($status =~ /\D/) {
211 $self->killsignal( "internal code failed: $status" );
212 $self->status( $stath->{exit_status} = 126 );
213 ;
214 }
215 elsif ($status & 127) {
216 $self->kill( 1 );
217 $self->killsignal( $sig_name[$status & 127] // "Unknown signal #".($status&127) );
218 }
219 else {
220 $self->status( $stath->{exit_status} = $status >> 8 );
221 }
222 };
223  
224 around '_register_status' => sub {
225 my $orig = shift;
226 my $self = shift;
227 my $status = shift || 0;
228 $orig->($self,$status);
229 $self->_base_register_status( $status );
230 };
231  
232 };
233  
234 1;