blib/lib/POE/Component/ForkManager.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 24 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 233 CC 19 SCAR 22.9
CC
19
Cov
0%
spawn_child
20.0
sigchld
20.0
check_idle
20.0
CRAP 9.9
1 package POE::Component::ForkManager;
2  
3 use 5;
4  
5 use strict;
6 use warnings;
7  
8 use vars '$VERSION';
9  
10 $VERSION = '0.00_01';
11  
12 use POE;
13  
14 use POE::Wheel::ReadWrite;
15 use POE::Filter::Block;
16 use POE::Driver::SysRW;
17 use POE::Pipe::TwoWay;
18  
19 sub DEBUGGING () { 0 }
20  
21 sub new {
22 my $class = shift;
23 my ($min, $max) = @_;
24  
25 $min ||= 0;
26 $max ||= 0;
27  
28 my $self = bless {}, (ref $class || $class);
29  
30 POE::Session->create(
31 package_states => [
32 __PACKAGE__, [qw(
33 _start
34 _stop
35 spawn_child
36 sigchld
37 parent_input
38 child_input
39 child_flushed
40 check_idle
41 )],
42 ],
43 heap => {
44 pids => {},
45 wheels => {},
46 min_idle => $min,
47 max_idle => $max,
48 idle => {},
49 callbacks => {},
50 },
51 args => [
52 $self,
53 ],
54 );
55  
56 return $self;
57 }
58  
59 sub limits {
60 my $self = shift;
61 my ($min, $max) = @_;
62  
63 my $heap = $self->{heap};
64  
65 if (defined( $min )) {
66 $heap->{min_idle} = $min + 0;
67 }
68 if (defined( $max )) {
69 $heap->{max_idle} = $max + 0;
70 }
71  
72 $self->{check_idle}->();
73  
74 return( $heap->{min_idle}, $heap->{max_idle} );
75 }
76  
77 sub callback {
78 my $self = shift;
79 my ($name, $value) = @_;
80 my $callbacks = $self->{heap}->{callbacks};
81  
82 if (defined( $value )) {
83 $callbacks->{$name} = $value;
84 }
85 else {
86 delete( $callbacks->{$name} );
87 }
88 }
89  
90 sub busy {
91 my $self = shift;
92 my $heap = $self->{heap};
93
94 my $write = $heap->{amulet} = $heap->{write};
95 $write->put( 0 );
96 }
97  
98 sub idle {
99 my $self = shift;
100 my $heap = $self->{heap};
101  
102 my $write = $heap->{amulet} = $heap->{write};
103 $write->put( 1 );
104 }
105  
106 sub DESTROY {
107 my $self = shift;
108
109 warn "DESTROY\n" if DEBUGGING;
110
111 my $heap = delete( $self->{heap} );
112  
113 my $write = delete( $heap->{write} );
114
115 $heap->{min_idle} = $heap->{max_idle} = 0;
116 $self->{check_idle}->();
117  
118 delete( $self->{check_idle} );
119 }
120  
121 sub _start {
122 my ($kernel, $session, $heap, $object) = @_[KERNEL, SESSION, HEAP, ARG0];
123  
124 # Set up the first batch of child processes, if any.
125 $kernel->yield( 'check_idle' );
126  
127 # Detach from the parent session so that we don't hold it alive.
128 $kernel->detach_myself();
129  
130 $object->{check_idle} = $session->callback( 'check_idle' );
131 $object->{heap} = $heap;
132 }
133  
134 sub _stop {
135 warn "[$$] Stopping\n" if DEBUGGING;
136 }
137  
138 sub spawn_child {
139 my ($kernel, $heap) = @_[KERNEL, HEAP];
140 my $pids = $heap->{pids};
141 my $wheels = $heap->{wheels};
142  
143 # Our pipe, for IPC
144 my ($parent_read, $parent_write, $child_read, $child_write) = POE::Pipe::TwoWay->new();
145  
146 # Build Filter and Driver before we fork, saves cycles later and reduces linecount
147 my $filter = POE::Filter::Block->new( BlockSize => 1 );
148 my $driver = POE::Driver::SysRW->new();
149  
150 $_[KERNEL]->sig( 'CHLD', 'sigchld' );
151
152 # Where once was one, there are now two.
153 my $fork = fork();
154  
155 if (!defined( $fork )) {
156 warn( "[POE::Component::Forkmanager] Fork Failed: $!\n" );
157 }
158 elsif( $fork ) {
159 # Parent process
160 my $wheel = $pids->{$fork} = POE::Wheel::ReadWrite->new(
161 InputHandle => $parent_read,
162 OutputHandle => $parent_write,
163 Driver => $driver,
164 Filter => $filter,
165 InputEvent => 'parent_input',
166 );
167 $wheels->{$wheel->ID} = $fork;
168  
169 # A new process is counted as idle, because we don't want to
170 # end up forking and flooding the machine.
171 $heap->{idle}->{$fork} = 1;
172  
173 # TODO make child startup tracking more robust. Push startup pids
174 # on an array, add a delay event to forcibly kill them if they haven't
175 # signaled that they are ready soon enough. Possibly refactor this into
176 # a general 'watchdog' design.
177 return 0;
178 }
179 else {
180 # Child process
181 $kernel->sig( 'CHLD' );
182 $kernel->state( 'sigchld' );
183 $kernel->state( 'spawn_child' ); # Prevent children from forking themselves.
184 $kernel->state( 'check_idle' );
185  
186 my $callbacks = $heap->{callbacks};
187
188 %$heap = (
189 callbacks => $callbacks,
190 );
191
192 $heap->{write} = POE::Wheel::ReadWrite->new(
193 InputHandle => $child_read,
194 OutputHandle => $child_write,
195 Driver => $driver,
196 Filter => $filter,
197 InputEvent => 'child_input',
198 FlushedEvent => 'child_flushed',
199 );
200  
201 if (exists( $callbacks->{child_fork} )) {
202 $callbacks->{child_fork}->();
203 }
204
205 return 1;
206 }
207 }
208  
209 sub sigchld {
210 my ($kernel, $session, $heap, $pid) = @_[KERNEL, SESSION, HEAP, ARG1];
211 my $pids = $heap->{pids};
212 my $wheels = $heap->{wheels};
213  
214 if (exists( $pids->{$pid} )) {
215 my $idle = $heap->{idle};
216 if (exists( $idle->{$pid} )) {
217 delete( $idle->{$pid} );
218 $kernel->call( $session, 'check_idle' );
219 }
220 my $wheel = delete( $pids->{$pid} );
221 delete( $wheels->{$wheel->ID} );
222 $kernel->sig_handled();
223 }
224 $kernel->sig( 'CHLD' ) unless (keys( %$pids ));
225 }
226  
227 sub parent_input {
228 my ($kernel, $session, $heap, $input, $wheel_id) = @_[KERNEL, SESSION, HEAP, ARG0, ARG1];
229 my $idle = $heap->{idle};
230
231 my $pid = $heap->{wheels}->{$wheel_id};
232  
233 my $now_idle = $input; # This line defines true to mean idle, false to mean busy
234 my $previously_idle = exists( $idle->{$pid} );
235
236 if ($now_idle xor $previously_idle) { # we're changing state
237 if ($now_idle) {
238 $idle->{$pid} = 1;
239 }
240 else {
241 delete( $idle->{$pid} );
242 }
243 $kernel->call( $session, 'check_idle' );
244 }
245 }
246  
247 sub child_input {
248 my ($kernel, $heap, $input) = @_[KERNEL, HEAP, ARG0];
249 my $callbacks = delete( $heap->{callbacks} );
250 delete( $heap->{write} );
251  
252 if ($input) {
253 if (exists( $callbacks->{child_shutdown} )) {
254 $callbacks->{child_shutdown}->();
255 }
256 }
257 }
258  
259 sub child_flushed {
260 delete $_[HEAP]->{amulet};
261 }
262  
263 sub check_idle {
264 my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
265  
266 my $idle = $heap->{idle};
267  
268 my $max_idle = $heap->{max_idle};
269 my $min_idle = $heap->{min_idle};
270  
271 my $idle_count = keys( %$idle );
272  
273 if ($idle_count > $max_idle) {
274 my $count = $idle_count - $max_idle;
275  
276 while ($count-- > 0) {
277 my ($pid, undef) = each %$idle;
278
279 $heap->{pids}->{$pid}->put( '1' );
280 }
281
282 # TODO Remove from idle count because it should be shutting down.
283 # TODO Push onto an array, add a delay event to forcibly kill off in a few seconds.
284 # TODO cleanup array and delay event in sigchld above
285 }
286 elsif ( $idle_count < $min_idle) {
287 my $count = $min_idle - $idle_count;
288
289 foreach (1..$count) {
290 return if $kernel->call( $session, 'spawn_child' );
291 }
292 }
293 }
294  
295 1;
296 __END__