Automata/Sequence.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/EZDB/Alert.pm ↑ Summary Automata/Serial.pm »
1 =head1 NAME
2  
3 DynGig::Automata::Sequence - Sequential automation framework.
4  
5 =cut
6 package DynGig::Automata::Sequence;
7  
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use YAML::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶use DynGig::Automata::Serial;
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use DynGig::Automata::Thread;
17  
18 our $THREAD = 0;
19  
20 =head1 SYNOPOSIS
21  
22 use DynGig::Automata::Sequence;
23  
24 $DynGig::Automata::Sequence::THREAD = $thread_count; ## select thread mode
25  
26 my $sequence = DynGig::Automata::Sequence->new( 'blah' );
27  
28 $sequence->run ## see DynGig::Util::Logger for parameter
29 (
30 log => '/home/bob/log/blah',
31 time => 'local',
32 context => \%context,
33 thread => $thread_count,
34 );
35  
36 =head1 DESCRIPTION
37  
38 Execution pauses when an error is encountered in any job.
39 Execution resumes when all errors are removed from the alert database.
40  
41 Execution pauses when B<pause> is active.
42 Execution resumes when B<pause> is remove.
43  
44 Execution terminates when I<stop> is active.
45  
46 =head1 CONFIGURATION FILE
47  
48 A YAML file 'B<conf>' is expected under the application root directory.
49 It should load into a HASH with the following fields.
50  
51 KEY TYPE NOTE
52 === ==== ====
53 target HASH
54 queue AoH
55 begin HASH optional
56 end HASH optional
57  
58 Each sub-HASH represents a code/param HASH, where I<code> is the path to
59 the plugin subroutine, and I<param> is a HASH for the parameter of I<code>.
60 ( see B<PLUGIN API> )
61  
62 =head1 PLUGIN API
63  
64 Each plugin is a file that contains a subroutine. The path of the file
65 corresponds to I<code> in the configuration file. e.g.
66  
67 return sub
68 {
69 my %param = @_;
70 my $logger = $param{logger}; ## CODE
71 my $target = $param{target};
72 my $context = $param{context};
73 ## additional param are passed from the configuration file
74  
75 &$logger( "blah" );
76 &$logger( "%s", 'blah' );
77 ...
78 };
79  
80 =head1 MODE
81  
82 In I<serial> mode, I<target> code must have an exhaustive batching behavior.
83 e.g. n targets T1 .. Tn into k batches in k iterations of I<target> code
84  
85 ITERATION TARGET
86 ========= ======
87 1 T1, T2 .. Tm
88 2 Tm+1 ..
89 ...
90 k .. Tn-1, Tn
91 k+1 undef
92  
93 In I<serial> mode, the I<target> parameter for plugin is ARRAY.
94 Whereas in I<thread> mode, scalar.
95  
96 =head1 ARTIFACT
97  
98 The following directories or symlinks to which, if not already exist
99 are created under the application root directory.
100  
101 DIRECTORY NOTE
102 ========= ====
103 run where alert database and symlink to log are kept
104 param where override parameter files are kept
105  
106 The following files are created.
107  
108 FILE NOTE
109 ==== ====
110 run/$name.alert alert database
111 run/$name.log symlink to the log file
112  
113 The following file is used to control execution
114  
115 FILE NOTE
116 ==== ====
117 run/$name.pause lock file for pause/stop
118  
119 =cut
120 sub new
121 {
122 my ( $class, $name ) = @_;
123  
124 croak 'undefined/invalid name' if ! defined $name || ref $name;
125  
126 my $param = YAML::XS::LoadFile( File::Spec->join( 'conf', $name ) );
127  
128 croak 'invalid config' unless $param && ref $param eq 'HASH';
129  
130 my $queue = $param->{queue} ||= [];
131  
132 croak 'invalid queue config' if ref $queue ne 'ARRAY';
133 ## load code
134 my @global = grep { $param->{$_} } qw( target begin end );
135  
136 for my $plugin ( @$queue, map { $param->{$_} } @global )
137 {
138 my $error = 'invalid plugin ' . YAML::XS::Dump $plugin;
139  
140 croak $error . ( $@ || '' ) if ref $plugin ne 'HASH'
141 || ref ( $plugin->{param} ||= {} ) ne 'HASH'
142 || ref ( $plugin->{code} = do $plugin->{code} ) ne 'CODE';
143  
144 map { $plugin->{$_} ||= 0 } qw( redo retry timeout );
145 }
146  
147 map { $param->{$_}{name} = $_ } @global;
148 ## check job names for collision
149 for ( my %name, my $i = 0; $i < @$queue; $i ++ )
150 {
151 my $name = defined $queue->[$i]{name} ? $queue->[$i]{name} : $i + 1;
152 my $j = $name{$name};
153  
154 croak sprintf "name '%s' collided in job %d and %d\n",
155 $name, $j + 1, $i + 1 if defined $j;
156  
157 $queue->[ $name{$name} = $i ]{name} = "job.$name";
158 }
159  
160 $param->{name} = $name;
161  
162 bless +
163 {
164 sequence => $THREAD
165 ? DynGig::Automata::Thread->new( %$param )
166 : DynGig::Automata::Serial->new( %$param )
167 };
168 }
169  
170 sub AUTOLOAD
171 {
172 my $this = shift @_;
173  
174 return our $AUTOLOAD =~ /::(setup|file)$/
175 ? $this->{sequence}->$1( @_ ) : undef;
176 }
177  
178 =head1 NOTE
179  
180 See DynGig::Automata
181  
182 =cut
183  
184 1;
185  
186 __END__
« Automata/EZDB/Alert.pm ↑ Summary Automata/Serial.pm »