Automata/Serial.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Automata/Sequence.pm ↑ Summary Automata.pm »
1 =head1 NAME
2  
3 DynGig::Automata::Serial - Process targets in serial batches.
4 Each batch of targets is processed in sequential steps specified by the user.
5  
6 =cut
7 package DynGig::Automata::Serial;
8  
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶use DynGig::Util::Logger;
Subroutine BEGIN: called CC=1 SCAR=2.6
17 use DynGig::Util::LockFile::Time;
18 use DynGig::Automata::EZDB::Alert;
19  
20 use constant { OK => 0, ERROR => 1, GLOBAL => 'global', KILL => 3600 * 240 };
21  
22 sub new
23 {
24 my ( $class, %this ) = @_;
25 my ( %dir, %file );
26 my $name = $this{name};
27 my $queue = $this{queue};
28 my $error = 'invalid config';
29  
30 croak "$error: undefined/invalid name" if ! defined $name || ref $name;
31  
32 croak "$error: undefined/invalid queue" if ! defined $queue
33 || ref $queue ne 'ARRAY' || grep { ! defined $_ } @$queue;
34  
35 for my $job ( @$queue, @this{ qw( target begin end ) } )
36 {
37 next unless $job;
38  
39 croak $error if ref $job ne 'HASH';
40  
41 croak "$error: undefined/invalid name"
42 if ! defined $job->{name} || ref $job->{name};
43  
44 croak "$error: undefined/invalid code"
45 if ! defined $job->{code} || ref $job->{code} ne 'CODE';
46  
47 $job->{param} ||= {};
48 map { $job->{$_} ||= 0 } qw( redo retry timeout );
49 }
50 ## create directories
51 map { croak "invalid directory $_ " unless $dir{$_} = Cwd::abs_path( $_ ) }
52 qw( run param );
53  
54 map { $file{$_} = File::Spec->join( $dir{run}, "$name.$_" ) }
55 qw( log alert pause );
56  
57 bless +{ %this, _dir => \%dir, _file => \%file }, ref $class || $class;
58 }
59  
60 sub run
61 {
62 my ( $this, %param ) = @_;
63 my $error = 'invalid context';
64 ## context
65 croak "$error: not defined" unless my $context = $param{context};
66 croak "$error: not HASH" if ref $context ne 'HASH';
67  
68 my @key = ( 'global', map { $_->{name} } @{ $this->{queue} } );
69  
70 map { push @key, $_ if defined $this->{$_} } qw( target begin end );
71  
72 for my $key ( @key )
73 {
74 unless ( defined $context->{$key} )
75 {
76 $context->{$key} = {};
77 }
78 elsif ( ref $context->{$key} ne 'HASH' )
79 {
80 croak "$error: $key not HASH";
81 }
82 }
83  
84 $context->{transient} = {};
85 my $run = $this->{_run} = { context => $context };
86 ## prepare
87 $this->_prepare( %param );
88 ## alert db
89 $run->{alert} ||= DynGig::Automata::EZDB::Alert->new
90 (
91 $this->{_file}{alert}, table => [ GLOBAL ],
92 );
93  
94 my $name = $this->{name};
95 my $logger = $run->{logger};
96 ## begin
97 $logger->write( 'START: sequence %s', $name );
98 $this->_job( $this->{begin} );
99 ## sequence
100 while ( my @target = $this->_job( $this->{target} ) )
101 {
102 $logger->write( 'TARGET: %s', YAML::XS::Dump \@target );
103  
104 for my $job ( @{ $this->{queue} } )
105 {
106 $logger->write( 'START: %s', $job->{name} );
107 $this->_job( $job, target => \@target );
108 $logger->write( 'DONE: %s', $job->{name} );
109 }
110 }
111 ## end
112 $this->_job( $this->{end} );
113 $logger->write( 'DONE: sequence %s', $name );
114  
115 delete $context->{transient};
116 }
117  
118 sub file
119 {
120 my ( $this, $ext ) = @_;
121 return File::Spec->join( $this->{_dir}{run}, $this->{name} . '.' . $ext );
122 }
123  
124 sub job
125 {
126 my $this = shift @_;
127 my @job = map { $_->{name} } @{ $this->{queue} };
128  
129 return wantarray ? @job : \@job;
130 }
131  
132 sub setup
133 {
134 my $this = shift @_;
135 my $run = $this->{_dir}{run};
136  
137 croak "mkdir $run: $!" unless -d $run || mkdir $run, 0755;
138 return $this;
139 }
140  
141 sub AUTOLOAD
142 {
143 my $this = shift @_;
144 my $run = $this->{_run};
145  
146 return our $AUTOLOAD =~ /::(\w+)$/ && $run ? $run->{$1} : undef;
147 }
148  
149 sub _prepare
150 {
151 my ( $this, %param ) = @_;
152  
153 $this->setup();
154  
155 my $log = $param{log};
156 my $file = $this->{_file};
157 my $run = $this->{_run};
158  
159 map { unlink $_ if -e $_ } values %$file;
160  
161 $run->{param} = {};
162 $run->{logger} = DynGig::Util::Logger->new( %param );
163  
164 symlink $log, $file->{log} if defined $log && -e $log;
165 }
166  
167 sub _eval ## run plugin code
168 {
169 my ( $this, $job, %param ) = @_;
170 my ( $status, @result ) = OK;
171  
172 eval
173 {
174 my $timeout = $job->{timeout};
175 local $SIG{ALRM} = sub { die "timeout after $timeout seconds\n" };
176  
177 alarm $timeout;
178 @result = &{ $job->{code} }( %{ $job->{param} }, %param );
179 alarm 0;
180 };
181  
182 if ( $@ )
183 {
184 $status = ERROR;
185 @result = $@;
186 }
187  
188 return $status, @result;
189 }
190  
191 sub _job
192 {
193 my ( $this, $job, %param ) = @_;
194  
195 return unless $job;
196  
197 my ( $retry, @result );
198 my $name = $job->{name};
199 my $run = $this->{_run};
200 my $alert = $run->{alert};
201 my $logger = $run->{logger};
202 my $context = $run->{context};
203 my %logger = ( logger => sub { $logger->write( @_ ) } );
204 my %context = map { $_ => $context->{$_} } qw( transient global );
205
206 $context{glocal} = $context->{ $name };
207  
208 while ( 1 )
209 {
210 my $stuck;
211  
212 while ( 1 ) ## stuck on alert or pause
213 {
214 my $pause =
215 DynGig::Util::LockFile::Time->check( $this->{_file}{pause} );
216  
217 unless ( $pause )
218 {
219 last unless my %alert = $alert->dump( GLOBAL );
220 $stuck = -1;
221 }
222 elsif ( $pause >= KILL )
223 {
224 $logger->write( 'KILL: %s', $name );
225 $alert->truncate( GLOBAL );
226  
227 return;
228 }
229 elsif ( $stuck <= 0 )
230 {
231 $logger->write( 'PAUSE: %s', $name );
232 $stuck = $pause;
233 }
234  
235 sleep 10;
236 }
237  
238 if ( $stuck )
239 {
240 $logger->write( 'RESUME: %s', $name );
241  
242 if ( $stuck < 0 )
243 {
244 last unless $job->{redo};
245 $retry = 0; ## redo & reset counter
246 }
247 }
248  
249 @result = $this->_eval
250 (
251 $job, %{ $this->_param( $name ) }, %param, %logger,
252 context => \%context,
253 );
254  
255 last if OK == shift @result;
256  
257 if ( $retry ++ < $job->{retry} )
258 {
259 $logger->write( 'RETRY %d: %s << %s', $retry, $name, $result[0] );
260 }
261 else
262 {
263 $alert->set( GLOBAL, $name, $result[0] );
264 $logger->write( 'ALERT: %s << %s', $name, $result[0] );
265 }
266 }
267  
268 return @result;
269 }
270  
271 sub _param ## load param from file
272 {
273 my ( $this, $name ) = @_;
274 my $file = File::Spec->join( $this->{_dir}{param}, $name );
275  
276 return {} unless open my $handle, '<', $file;
277  
278 my $time = ( stat $handle )[9];
279 my $param = $this->{_run}{param}{$name} ||= {};
280  
281 unless ( $param->{$time} )
282 {
283 my $override = eval { YAML::XS::LoadFile $handle };
284  
285 map { delete $param->{$_} } keys %$param;
286 $param->{$time} = $@ && ref $override ne 'HASH' ? {} : $override;
287 }
288  
289 close $handle;
290 return $param->{$time};
291 }
292  
293 =head1 NOTE
294  
295 See DynGig::Automata
296  
297 =cut
298  
299 1;
300  
301 __END__
« Automata/Sequence.pm ↑ Summary Automata.pm »