lib/Mail/Decency/Core/POEForking.pm

filter: Statementstmt 22.0% 17 / 77 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 31.2% 5 / 16 Podpod 90.0% 9 / 10 totaltotal 24.4% 31 / 127 CC 15 SCAR 29.7
CC
15
Cov
17%
forking_fork_child
42.0
forking_catch_sig_int
30.0
forking_catch_sig_child
30.0
CRAP 19.4
« lib/Mail/Decency/Core/SessionItem/Policy.pm ↑ Summary lib/Mail/Decency/LogParser/Core/CacheWrite.pm »
1 package Mail::Decency::Core::POEForking;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use version 0.74; our $VERSION = qv( "v0.1.4" );
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use Scalar::Util qw/ weaken /;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use POE qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
11 Filter::Postfix::Base64
12 Filter::Postfix::Null
13 Filter::Postfix::Plain
14 Wheel::ReadWrite
15 Wheel::SocketFactory
16 20 /;
17  
18 =head1 NAME
19  
20 Mail::Decency::Core::POEForking
21  
22 =head1 DESCRIPTION
23  
24 Base class for Postfix and SMTP server. Implements forking
25  
26  
27 =head1 METHODS
28  
29  
30 =head2 new
31  
32 =cut
33  
34 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
35 0 my ( $class, $decency, $args_ref ) = @_;
36
37 0 weaken( my $decency_weak = $decency );
38 POE::Session->create(
39 inline_states => {
40 _start => \&forking_startup,
41 _stop => \&forking_halt,
42 fork_child => \&forking_fork_child,
43 catch_sig_int => \&forking_catch_sig_int,
44 catch_sig_term => \&forking_catch_sig_term,
45 catch_sig_child => \&forking_catch_sig_child,
46 new_connection => \&forking_new_connection,
47 client_error => \&forking_client_error,
48 0 ▶ new_client_session => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 my ( $heap, $client_session ) = @_[ HEAP, ARG0 ];
50 0 ▶ $heap->{ client_sessions } ||= {};
Condition0%0/2
$heap->{"client_sessions"} ||= {}
l!l
00
MC/DC0%0/1
$heap->{"client_sessions"} ||= {}
$heap->{"client_sessions"}
Truth table0%0/2
A
A= $heap->{"client_sessions"}
Aresult
11
00
51 0 $heap->{ client_sessions }->{ $client_session->ID } = $client_session;
52 },
53 #_child => sub { undef },
54 # _default => sub {
55 # my ( $heap, $event, $args ) = @_[ HEAP, ARG0, ARG1 ];
56 # $heap->{ decency }->logger->error( "** UNKNOWN EVENT $event, $args" );
57 # }
58 },
59 0 heap => {
60 decency => $decency_weak,
61 conf => $decency_weak->config,
62 args => $args_ref,
63 class => $class
64 }
65 );
66 }
67  
68  
69 =head2 forking_startup
70  
71 Server startup event
72  
73 =cut
74  
75 0 ▶sub forking_startup {
Subroutine forking_startup: not called CC=1 SCAR=6.9
Pod documented
76 0 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
77
78 # listen to port and adress
79 $heap->{ server } = POE::Wheel::SocketFactory->new(
80 BindAddress => $heap->{ conf }->{ server }->{ host },
81 BindPort => $heap->{ conf }->{ server }->{ port },
82 0 SuccessEvent => "new_connection",
83 FailureEvent => "client_error",
84 Reuse => "yes"
85 );
86
87 # master does not list
88 #$heap->{ server }->pause_accept();
89
90 # bing sig int to final sig int (bye bye)
91 0 $kernel->sig( INT => "catch_sig_int" );
92 0 $kernel->sig( TERM => "catch_sig_term" );
93
94 # this is the parental process, set list of childs
95 0 $heap->{ childs } = {};
96
97 # mark as parent
98 0 $heap->{ is_child } = 0;
99
100 # startup message
101 0 $heap->{ server_address } = "$heap->{ conf }->{ server }->{ host }:$heap->{ conf }->{ server }->{ port }";
102 0 $heap->{ decency }->logger->debug0( "Start server on $heap->{ server_address } ($$)" );
103
104 # begin create childs
105 0 $kernel->yield( 'fork_child' );
106 }
107  
108  
109 =head2 forking_halt
110  
111 All goes down
112  
113 =cut
114  
115 0 ▶sub forking_halt {
Subroutine forking_halt: not called CC=1 SCAR=6.9
Pod documented
116 0 my $heap = $_[ HEAP ];
117 0 $heap->{ decency }->logger->debug0( "Stop server on $heap->{ server_address } ($$)" );
118 }
119  
120  
121 =head2 forking_fork_child
122  
123 Create a new child
124  
125 =cut
126  
127 0 ▶sub forking_fork_child {
Subroutine forking_fork_child: not called CC=6 SCAR=37.4
Pod documented
128 0 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
129
130 # childs don't fork new childs!
131 0 ▶ return if $heap->{ is_child };
Branch: 1 branch
BranchTrueFalseTotal
if $heap->{"is_child"} 0 0 0
132
133 # main fork loop
134 0 ▶ my $max = $heap->{ conf }->{ server }->{ instances } || 3;
Condition0%0/2
$heap->{'conf'}{'server'}{'instances'} || 3
l!l
00
MC/DC0%0/1
$heap->{'conf'}{'server'}{'instances'} || 3
$heap->{'conf'}{'server'}{'instances'}
Truth table0%0/2
A
A= $heap->{'conf'}{'server'}{'instances'}
Aresult
11
00
135 0 my $check = 0;
136 0 ▶ while( scalar( keys %{ $heap->{ childs } } ) < $max ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %{$$heap{"childs"};} < $max) 0 0 0
137 0 my $pid = fork();
138
139 # oops, could not fork!
140 0 ▶ unless ( defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $pid) { } 0 0 0
if (do { $pid }) { } 0 0 0
141 0 $heap->{ decency }->logger->error( "Failed forking child: $!" );
142
143 # try, try again!
144 0 $kernel->delay( fork_child => 5 );
145 0 return;
146 }
147
148 # we are in parent process:
149 elsif ( $pid ) {
150
151 # add new child
152 0 $heap->{ decency }->logger->debug0( "Add new child $pid to list" );
153 0 $heap->{ childs }->{ $pid } ++;
154
155 # bind sig child to handler (if child dies -> this will be called)
156 0 $kernel->sig_child( $pid, "catch_sig_child" );
157 }
158
159 # we are the child
160 else {
161
162 # child does accept connections
163 #$heap->{ server }->resume_accept();
164
165 # tell everybody we are here, new and forked
166 0 $kernel->has_forked;
167
168 # assure no misidentification
169 0 $heap->{ is_child }++;
170 0 $heap->{ childs } = {};
171 0 return;
172 }
173 }
174
175 }
176  
177  
178 =head2 forking_catch_sig_int
179  
180 Catch the death of the master process
181  
182 =cut
183  
184 0 ▶sub forking_catch_sig_int {
Subroutine forking_catch_sig_int: not called CC=5 SCAR=34.0
Pod documented
185 0 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
186
187 0 $heap->{ decency }->logger->debug0( "Caught SIG Int ($$)" );
188
189 # remove one self
190 0 delete $heap->{ server };
191
192 # close all client session
193 0 ▶ if ( defined $heap->{ client_sessions } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heap->{"client_sessions"}) 0 0 0
194 $kernel->post( $_, 'good_night' )
195 0 for values %{ $heap->{ client_sessions } };
196 0 delete $heap->{ client_sessions };
197 }
198
199 # close all childs
200 0 ▶ if ( ! $heap->{ is_child } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($heap->{"is_child"}) 0 0 0
201 kill "INT", $_
202 0 for keys %{ $heap->{ childs } };
203 }
204
205 # say good night
206 0 $kernel->sig_handled();
207 }
208  
209  
210 =head2 forking_catch_sig_int
211  
212 Catch the death of the master process
213  
214 =cut
215  
216 0 ▶sub forking_catch_sig_term {
Subroutine forking_catch_sig_term: not called CC=1 SCAR=6.9
Pod undocumented
217 0 my ( $kernel, $heap ) = @_[ KERNEL, HEAP ];
218
219 0 $heap->{ decency }->logger->debug0( "Caught SIG Term ($$)" );
220 0 forking_catch_sig_int( @_ );
221 }
222  
223  
224  
225 =head2 forking_catch_sig_child
226  
227 Catch the death of a child .. sad as it might be
228  
229 =cut
230  
231 0 ▶sub forking_catch_sig_child {
Subroutine forking_catch_sig_child: not called CC=5 SCAR=34.0
Pod documented
232 0 my ( $kernel, $heap, $child_pid ) = @_[ KERNEL, HEAP, ARG1 ];
233
234 # if there is NO such child -> return
235 0 ▶ return unless delete $heap->{ childs }->{ $child_pid };
Branch: 1 branch
BranchTrueFalseTotal
unless delete $heap->{"childs"}{$child_pid} 0 0 0
236
237 # close all client session
238 0 ▶ if ( defined $heap->{ client_sessions } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $heap->{"client_sessions"}) 0 0 0
239 0 $kernel->post( $_, 'good_night' ) for values %{ $heap->{ client_sessions } };
240 0 delete $heap->{ client_sessions };
241 }
242
243 # create new child -> if the server is still there!! (not been killed..)
244 0 ▶ $kernel->yield( "fork_child" ) if exists $heap->{ server };
Branch: 1 branch
BranchTrueFalseTotal
if exists $heap->{"server"} 0 0 0
245 }
246  
247  
248 =head2 forking_new_connection
249  
250 New connection etablished
251  
252 =cut
253  
254 0 ▶sub forking_new_connection {
Subroutine forking_new_connection: not called CC=1 SCAR=6.9
Pod documented
255 0 my $heap = $_[ HEAP ];
256 0 $heap->{ class }->create_handler( @_ );
257 }
258  
259  
260 =head2 forking_client_error
261  
262 Error cleint ..
263  
264 =cut
265  
266 0 ▶sub forking_client_error {
Subroutine forking_client_error: not called CC=1 SCAR=6.9
Pod documented
267 0 my ( $kernel, $heap, $session, @args ) = @_[ KERNEL, HEAP, SESSION, ARG0..ARG9 ];
268 }
269  
270  
271 =head2 init_factory_args
272  
273 Can be overwritten by childs
274  
275 Returns additional args for create the factory
276  
277 =cut
278  
279 0 ▶sub init_factory_args { return () }
Subroutine init_factory_args: not called CC=1 SCAR=6.9
Pod documented
280  
281  
282 =head1 AUTHOR
283  
284 Ulrich Kautz <uk@fortrabbit.de>
285  
286 =head1 COPYRIGHT
287  
288 Copyright (c) 2010 the L</AUTHOR> as listed above
289  
290 =head1 LICENCSE
291  
292 This library is free software and may be distributed under the same terms as perl itself.
293  
294 =cut
295  
296  
297 20 1;
« lib/Mail/Decency/Core/SessionItem/Policy.pm ↑ Summary lib/Mail/Decency/LogParser/Core/CacheWrite.pm »