POEGateway/Mailer.pm

filter: Statementstmt 24.2% 76 / 313 Branchbran 0.8% 1 / 124 Conditioncond 0.0% 0 / 51 MC/DCmcdc 0.0% 0 / 33 Subroutinesub 59.5% 25 / 42 Podpod 14.2% 2 / 14 totaltotal 18.0% 104 / 577 CC 80 SCAR 58.8
CC
80
Cov
16%
spawn
1122.0
Got_STDOUT
272.0
send_report
110.0
CRAP 357.8
↑ Summary POEGateway.pm »
1 # Declare our package
2 package Test::Reporter::POEGateway::Mailer;
3 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # Initialize our version
6 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 $VERSION = '0.04';
8  
9 # Import what we need from the POE namespace
10 1 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use POE::Wheel::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POE::Filter::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POE::Filter::Line;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use base 'POE::Session::AttributeBased';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Set some constants
17 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
18 1 ▶ if ( ! defined &DEBUG ) { *DEBUG = sub () { 0 } }
Branch: 1 branch
BranchTrueFalseTotal
if (not defined &Test::Reporter::POEGateway::Mailer::DEBUG) 1 0 1
19 }
20  
21 # starts the component!
22 0 ▶sub spawn {
Subroutine spawn: not called CC=33 SCAR=70.2
Pod undocumented
23 0 my $class = shift;
24  
25 # The options hash
26 0 my %opt;
27  
28 # Support passing in a hash ref or a regular hash
29 0 ▶ if ( ( @_ & 1 ) and ref $_[0] and ref( $_[0] ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1 and ref $_[0] and ref $_[0] eq 'HASH') { } 0 0 0
Condition0%0/6
@_ & 1 and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
@_ & 1 and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ & 1 and ref $_[0] and ref $_[0] eq "HASH"
@_ & 1 ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/4
A and B and C
A= @_ & 1B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
30 0 %opt = %{ $_[0] };
31 } else {
32 # Sanity checking
33 0 ▶ if ( @_ & 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ & 1) 0 0 0
34 0 warn __PACKAGE__ . ' requires an even number of options passed to spawn()';
35 0 return 0;
36 }
37  
38 0 %opt = @_;
39 }
40  
41 # lowercase keys
42 0 %opt = map { lc($_) => $opt{$_} } keys %opt;
43  
44 0 ▶ if ( exists $opt{'poegateway'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{'poegateway'}) { } 0 0 0
45 0 if ( DEBUG ) {
46 warn "Not using REPORTS, we will receive reports directly from POEGateway";
47 }
48  
49 0 ▶ if ( exists $opt{'reports'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opt{"reports"}) 0 0 0
50 0 warn "You cannot use REPORTS with POEGATEWAY at the same time, preferring POEGATEWAY!";
51 0 delete $opt{'reports'};
52 }
53 } else {
54 # setup the path to read reports from
55 0 ▶ if ( ! exists $opt{'reports'} or ! defined $opt{'reports'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"reports"} and defined $opt{"reports"}) 0 0 0
Condition0%0/3
exists $opt{"reports"} and defined $opt{"reports"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{"reports"} and defined $opt{"reports"}
exists $opt{"reports"} defined $opt{"reports"}
Truth table0%0/3
A and B
A= exists $opt{"reports"}B= defined $opt{"reports"}
ABresult
0X0
100
111
56 0 require File::Spec;
57 0 my $path = File::Spec->catdir( $ENV{HOME}, 'cpan_reports' );
58 0 if ( DEBUG ) {
59 warn "Using default REPORTS = $path";
60 }
61  
62 # Set the default
63 0 $opt{'reports'} = $path;
64 }
65  
66 # validate the report path
67 0 ▶ if ( ! -d $opt{'reports'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $opt{"reports"}) 0 0 0
68 0 warn "The REPORTS path does not exist ($opt{'reports'}), please make sure it is a writable directory!";
69 0 return 0;
70 }
71  
72 # setup the dirwatch alias
73 0 ▶ if ( ! exists $opt{'dirwatch_alias'} or ! defined $opt{'dirwatch_alias'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"dirwatch_alias"} and defined $opt{"dirwatch_alias"}) 0 0 0
Condition0%0/3
exists $opt{"dirwatch_alias"} and defined $opt{"dirwatch_alias"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{"dirwatch_alias"} and defined $opt{"dirwatch_alias"}
exists $opt{"dirwatch_alias"} defined $opt{"dirwatch_alias"}
Truth table0%0/3
A and B
A= exists $opt{"dirwatch_alias"}B= defined $opt{"dirwatch_alias"}
ABresult
0X0
100
111
74 0 if ( DEBUG ) {
75 warn 'Using default DIRWATCH_ALIAS = POEGateway-Mailer-DirWatch';
76 }
77  
78 # Set the default
79 0 $opt{'dirwatch_alias'} = 'POEGateway-Mailer-DirWatch';
80 }
81  
82 # setup the dirwatch interval
83 0 ▶ if ( ! exists $opt{'dirwatch_interval'} or ! defined $opt{'dirwatch_interval'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"dirwatch_interval"} and defined $opt{"dirwatch_interval"}) 0 0 0
Condition0%0/3
exists $opt{"dirwatch_interval"} and defined $opt{"dirwatch_interval"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{"dirwatch_interval"} and defined $opt{"dirwatch_interval"}
exists $opt{"dirwatch_interval"} defined $opt{"dirwatch_interval"}
Truth table0%0/3
A and B
A= exists $opt{"dirwatch_interval"}B= defined $opt{"dirwatch_interval"}
ABresult
0X0
100
111
84 0 if ( DEBUG ) {
85 warn 'Using default DIRWATCH_INTERVAL = 120';
86 }
87  
88 # Set the default
89 0 $opt{'dirwatch_interval'} = 120;
90 }
91 }
92  
93 # setup the alias
94 0 ▶ if ( ! exists $opt{'alias'} or ! defined $opt{'alias'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"alias"} and defined $opt{"alias"}) 0 0 0
Condition0%0/3
exists $opt{"alias"} and defined $opt{"alias"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{"alias"} and defined $opt{"alias"}
exists $opt{"alias"} defined $opt{"alias"}
Truth table0%0/3
A and B
A= exists $opt{"alias"}B= defined $opt{"alias"}
ABresult
0X0
100
111
95 0 if ( DEBUG ) {
96 warn 'Using default ALIAS = POEGateway-Mailer';
97 }
98  
99 # Set the default
100 0 $opt{'alias'} = 'POEGateway-Mailer';
101 }
102  
103 # Setup the session
104 0 ▶ if ( ! exists $opt{'session'} or ! defined $opt{'session'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{'session'} && defined $opt{'session'}) { } 0 0 0
Condition0%0/3
exists $opt{'session'} && defined $opt{'session'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{'session'} && defined $opt{'session'}
exists $opt{'session'} defined $opt{'session'}
Truth table0%0/3
A && B
A= exists $opt{'session'}B= defined $opt{'session'}
ABresult
0X0
100
111
105 0 if ( DEBUG ) {
106 warn 'Using default SESSION = caller';
107 }
108  
109 # set the default
110 0 $opt{'session'} = undef;
111 } else {
112 # Convert it to an ID
113 0 ▶ if ( UNIVERSAL::isa( $opt{'session'}, 'POE::Session' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($opt{"session"}, "POE::Session")) 0 0 0
114 0 $opt{'session'} = $opt{'session'}->ID;
115 }
116 }
117  
118 # setup the "maildone" event
119 0 ▶ if ( ! exists $opt{'maildone'} or ! defined $opt{'maildone'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opt{"maildone"} and defined $opt{"maildone"}) 0 0 0
Condition0%0/3
exists $opt{"maildone"} and defined $opt{"maildone"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{"maildone"} and defined $opt{"maildone"}
exists $opt{"maildone"} defined $opt{"maildone"}
Truth table0%0/3
A and B
A= exists $opt{"maildone"}B= defined $opt{"maildone"}
ABresult
0X0
100
111
120 0 if ( DEBUG ) {
121 warn 'Using default MAILDONE = undef';
122 }
123  
124 # Set the default
125 0 $opt{'maildone'} = undef;
126 }
127  
128 # setup the mailing subprocess
129 0 ▶ if ( ! exists $opt{'mailer'} or ! defined $opt{'mailer'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{'mailer'} && defined $opt{'mailer'}) { } 0 0 0
Condition0%0/3
exists $opt{'mailer'} && defined $opt{'mailer'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{'mailer'} && defined $opt{'mailer'}
exists $opt{'mailer'} defined $opt{'mailer'}
Truth table0%0/3
A && B
A= exists $opt{'mailer'}B= defined $opt{'mailer'}
ABresult
0X0
100
111
130 0 if ( DEBUG ) {
131 warn 'Using default MAILER = SMTP';
132 }
133  
134 # Set the default
135 0 $opt{'mailer'} = 'SMTP';
136 } else {
137 # TODO verify the mailer actually exists?
138 }
139  
140 # setup the mailing subprocess config
141 0 ▶ if ( ! exists $opt{'mailer_conf'} or ! defined $opt{'mailer_conf'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{'mailer_conf'} && defined $opt{'mailer_conf'}) { } 0 0 0
Condition0%0/3
exists $opt{'mailer_conf'} && defined $opt{'mailer_conf'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{'mailer_conf'} && defined $opt{'mailer_conf'}
exists $opt{'mailer_conf'} defined $opt{'mailer_conf'}
Truth table0%0/3
A && B
A= exists $opt{'mailer_conf'}B= defined $opt{'mailer_conf'}
ABresult
0X0
100
111
142 0 if ( DEBUG ) {
143 warn 'Using default MAILER_CONF = {}';
144 }
145  
146 # Set the default
147 0 $opt{'mailer_conf'} = {};
148 } else {
149 0 ▶ if ( ref( $opt{'mailer_conf'} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{"mailer_conf"} ne "HASH") 0 0 0
150 0 warn "The MAILER_CONF argument is not a valid HASH reference!";
151 0 return 0;
152 }
153 }
154  
155 # setup the host aliases
156 0 ▶ if ( ! exists $opt{'host_aliases'} or ! defined $opt{'host_aliases'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{'host_aliases'} && defined $opt{'host_aliases'}) { } 0 0 0
Condition0%0/3
exists $opt{'host_aliases'} && defined $opt{'host_aliases'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{'host_aliases'} && defined $opt{'host_aliases'}
exists $opt{'host_aliases'} defined $opt{'host_aliases'}
Truth table0%0/3
A && B
A= exists $opt{'host_aliases'}B= defined $opt{'host_aliases'}
ABresult
0X0
100
111
157 0 if ( DEBUG ) {
158 warn 'Using default HOST_ALIASES = {}';
159 }
160  
161 # Set the default
162 0 $opt{'host_aliases'} = {};
163 } else {
164 0 ▶ if ( ref( $opt{'host_aliases'} ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opt{"host_aliases"} ne "HASH") 0 0 0
165 0 warn "The HOST_ALIASES argument is not a valid HASH reference!";
166 0 return 0;
167 }
168 }
169  
170 # setup the delay between emails
171 0 ▶ if ( ! exists $opt{'delay'} or ! defined $opt{'delay'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opt{'delay'} && defined $opt{'delay'}) { } 0 0 0
Condition0%0/3
exists $opt{'delay'} && defined $opt{'delay'}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opt{'delay'} && defined $opt{'delay'}
exists $opt{'delay'} defined $opt{'delay'}
Truth table0%0/3
A && B
A= exists $opt{'delay'}B= defined $opt{'delay'}
ABresult
0X0
100
111
172 0 if ( DEBUG ) {
173 warn 'Using default DELAY = 0';
174 }
175  
176 # Set the default
177 0 $opt{'delay'} = 0;
178 } else {
179 0 ▶ if ( $opt{'delay'} !~ /^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt{"delay"} =~ /^\d+$/) 0 0 0
180 0 warn 'The DELAY argument is not a valid integer >= 0!';
181 0 return 0;
182 }
183 }
184  
185 # Create our session
186 POE::Session->create(
187 __PACKAGE__->inline_states(),
188 'heap' => {
189 'ALIAS' => $opt{'alias'},
190 'MAILER' => $opt{'mailer'},
191 'MAILER_CONF' => $opt{'mailer_conf'},
192 'HOST_ALIASES' => $opt{'host_aliases'},
193 'DELAY' => $opt{'delay'},
194  
195 'SESSION' => $opt{'session'},
196 'MAILDONE' => $opt{'maildone'},
197  
198 ( exists $opt{'poegateway'} ? ( 'POEGATEWAY' => 1 ) : (
199 'REPORTS' => $opt{'reports'},
200 'DIRWATCH_ALIAS' => $opt{'dirwatch_alias'},
201 0 ▶ 'DIRWATCH_INTERVAL' => $opt{'dirwatch_interval'},
Branch: 1 branch
BranchTrueFalseTotal
exists $opt{'poegateway'} ? : 0 0 0
202 ) ),
203  
204 'NEWFILES' => [],
205 'WHEEL' => undef,
206 'WHEEL_WORKING' => 0,
207 'WHEEL_RETRIES' => 0,
208 'SHUTDOWN' => 0,
209 },
210 );
211  
212 # return success
213 0 return 1;
214 }
215  
216 # This starts the component
217 0 ▶sub _start : State {
Subroutine _start: not called CC=6 SCAR=37.4
218 0 if ( DEBUG ) {
219 warn 'Starting alias "' . $_[HEAP]->{'ALIAS'} . '"';
220 }
221  
222 # Set up the alias for ourself
223 0 $_[KERNEL]->alias_set( $_[HEAP]->{'ALIAS'} );
224  
225 # spawn the dirwatch
226 0 ▶ if ( ! exists $_[HEAP]->{'POEGATEWAY'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $_[3]{"POEGATEWAY"}) 0 0 0
227 0 require POE::Component::DirWatch;
228 0 POE::Component::DirWatch->import; # needed to set AIO stuff, darn it!
229 $_[HEAP]->{'DIRWATCH'} = POE::Component::DirWatch->new(
230 'alias' => $_[HEAP]->{'DIRWATCH_ALIAS'},
231 'directory' => $_[HEAP]->{'REPORTS'},
232 'file_callback' => $_[SESSION]->postback( 'got_new_file' ),
233 0 'interval' => $_[HEAP]->{'DIRWATCH_INTERVAL'},
234 );
235  
236 # Load the necessary modules
237 0 require YAML::Tiny;
238 0 YAML::Tiny->import( qw( LoadFile ) );
239  
240 0 require File::Copy;
241 0 File::Copy->import( qw( move ) );
242  
243 0 require File::Spec;
244 }
245  
246 # Setup the session
247 0 ▶ if ( ! defined $_[HEAP]->{'SESSION'} and defined $_[HEAP]->{'MAILDONE'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[3]{"SESSION"} and defined $_[3]{"MAILDONE"}) 0 0 0
Condition0%0/3
not defined $_[3]{"SESSION"} and defined $_[3]{"MAILDONE"}
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $_[3]{"SESSION"} and defined $_[3]{"MAILDONE"}
not defined $_[3]{"SESSION"} defined $_[3]{"MAILDONE"}
Truth table0%0/3
not(A) and B
A= not defined $_[3]{"SESSION"}B= defined $_[3]{"MAILDONE"}
ABresult
0X0
100
111
248 # Use the sender
249 0 ▶ if ( $_[KERNEL] == $_[SENDER] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2] == $_[5]) { } 0 0 0
250 0 warn 'Not called from another POE session and SESSION was not set!';
251 0 $_[KERNEL]->yield( 'shutdown' );
252 0 return;
253 } else {
254 0 $_[HEAP]->{'SESSION'} = $_[SENDER]->ID;
255 }
256 }
257  
258 # Give it a refcount
259 0 ▶ if ( defined $_[HEAP]->{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{"SESSION"}) 0 0 0
260 0 $_[KERNEL]->refcount_increment( $_[HEAP]->{'SESSION'}, __PACKAGE__ );
261 }
262  
263 0 return;
264 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
265  
266 # POE Handlers
267 0 ▶sub _stop : State {
Subroutine _stop: not called CC=1 SCAR=6.9
268 0 if ( DEBUG ) {
269 warn 'Stopping alias "' . $_[HEAP]->{'ALIAS'} . '"';
270 }
271  
272 0 return;
273 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 0 ▶sub _child : State {
Subroutine _child: not called CC=1 SCAR=6.9
276 0 return;
277 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
278  
279 0 ▶sub shutdown : State {
Subroutine shutdown: not called CC=5 SCAR=34.0
Pod documented
280 0 if ( DEBUG ) {
281 warn "Shutting down alias '" . $_[HEAP]->{'ALIAS'} . "'";
282 }
283  
284 # cleanup some stuff
285 0 $_[KERNEL]->alias_remove( $_[HEAP]->{'ALIAS'} );
286  
287 # tell dirwatcher to shutdown
288 0 ▶ if ( exists $_[HEAP]->{'DIRWATCH'} and defined $_[HEAP]->{'DIRWATCH'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"DIRWATCH"} and defined $_[3]{"DIRWATCH"}) 0 0 0
Condition0%0/3
exists $_[3]{"DIRWATCH"} and defined $_[3]{"DIRWATCH"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $_[3]{"DIRWATCH"} and defined $_[3]{"DIRWATCH"}
exists $_[3]{"DIRWATCH"} defined $_[3]{"DIRWATCH"}
Truth table0%0/3
A and B
A= exists $_[3]{"DIRWATCH"}B= defined $_[3]{"DIRWATCH"}
ABresult
0X0
100
111
289 0 $_[HEAP]->{'DIRWATCH'}->shutdown;
290 0 undef $_[HEAP]->{'DIRWATCH'};
291 }
292  
293 # decrement the refcount
294 0 ▶ if ( defined $_[HEAP]->{'SESSION'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{"SESSION"}) 0 0 0
295 0 $_[KERNEL]->refcount_decrement( $_[HEAP]->{'SESSION'}, __PACKAGE__ );
296 }
297  
298 0 $_[HEAP]->{'SHUTDOWN'} = 1;
299 0 undef $_[HEAP]->{'WHEEL'};
300  
301 # remove the delay if needed
302 0 ▶ $_[KERNEL]->alarm_remove( delete $_[HEAP]->{'_delay'} ) if exists $_[HEAP]->{'_delay'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[3]{"_delay"} 0 0 0
303  
304 0 return;
305 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
306  
307 # received a postback from DirWatch
308 0 ▶sub got_new_file : State {
Subroutine got_new_file: not called CC=2 SCAR=17.9
Pod undocumented
309 0 my $file = $_[ARG1]->[0]->stringify;
310  
311 # Have we seen this file before?
312 0 ▶ if ( ! grep { $_ eq $file } @{ $_[HEAP]->{'NEWFILES'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $file;} @{$_[3]{"NEWFILES"};}) 0 0 0
313 0 if ( DEBUG ) {
314 warn "Got a new file -> $file";
315 }
316  
317 # Add it to the newfile list
318 0 push( @{ $_[HEAP]->{'NEWFILES'} }, $file );
319  
320 # We're done!
321 0 $_[KERNEL]->yield( 'send_report' );
322 }
323  
324 0 return;
325 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
326  
327 # We got a report directly from the POEGateway httpd!
328 0 ▶sub http_report : State {
Subroutine http_report: not called CC=1 SCAR=6.9
Pod undocumented
329 0 my $report = $_[ARG0];
330  
331 0 if ( DEBUG ) {
332 warn "Got a new report from POEGateway -> $report->{subject}";
333 }
334  
335 # Shove it in the queue
336 0 push( @{ $_[HEAP]->{'NEWFILES'} }, $report );
337 0 $_[KERNEL]->yield( 'send_report' );
338 0 return;
339 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
340  
341 # Returns the length of the queue
342 0 ▶sub queue : State {
Subroutine queue: not called CC=1 SCAR=6.9
Pod documented
343 0 return scalar @{ $_[HEAP]->{'NEWFILES'} };
344 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
345  
346 0 ▶sub send_report_delayed : State {
Subroutine send_report_delayed: not called CC=2 SCAR=17.9
Pod undocumented
347 0 ▶ delete $_[HEAP]->{'_delay'} if exists $_[HEAP]->{'_delay'};
Branch: 1 branch
BranchTrueFalseTotal
if exists $_[3]{"_delay"} 0 0 0
348 0 $_[KERNEL]->yield( 'send_report' );
349 0 return;
350 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
351  
352 0 ▶sub send_report : State {
Subroutine send_report: not called CC=10 SCAR=47.0
Pod undocumented
353 0 ▶ if ( ! defined $_[HEAP]->{'WHEEL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[3]{"WHEEL"}) 0 0 0
354 # Setup the subprocess!
355 0 $_[KERNEL]->yield( 'setup_wheel' );
356 0 return;
357 }
358  
359 0 ▶ if ( $_[HEAP]->{'WHEEL_WORKING'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"WHEEL_WORKING"}) 0 0 0
360 0 return;
361 }
362  
363 0 ▶ if ( exists $_[HEAP]->{'_delay'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"_delay"}) 0 0 0
364 0 return;
365 }
366  
367 # Grab the first file from the array
368 0 my $file = $_[HEAP]->{'NEWFILES'}->[0];
369 0 ▶ if ( ! defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 0 0
370 0 return;
371 }
372  
373 # Is it a filename or hashref?
374 0 my $data;
375 0 ▶ if ( ! ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $file) { } 0 0 0
376 # TODO Sometimes DirWatch gives us a new file notification *AFTER* we delete it... WTF???
377 0 ▶ if ( ! -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $file) 0 0 0
378 0 shift @{ $_[HEAP]->{'NEWFILES'} };
379 0 return;
380 }
381  
382 0 eval {
383 0 $data = LoadFile( $file );
384 };
385 0 ▶ if ( $@ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 0 0
elsif (not defined $data) { } 0 0 0
386 0 if ( DEBUG ) {
387 warn "Failed to load '$file': $@";
388 }
389  
390 0 $_[KERNEL]->yield( 'save_failure', shift @{ $_[HEAP]->{'NEWFILES'} }, 'load' );
391 0 return;
392 } elsif ( ! defined $data ) {
393 0 if ( DEBUG ) {
394 warn "Malformed file: $file";
395 }
396 0 $_[KERNEL]->yield( 'save_failure', shift @{ $_[HEAP]->{'NEWFILES'} }, 'malformed' );
397 0 return;
398 }
399 } else {
400 0 $data = $file;
401 }
402  
403 # do some housekeeping
404 ## no critic ( ProhibitAccessOfPrivateData )
405 0 ▶ if ( exists $_[HEAP]->{'HOST_ALIASES'}->{ $data->{'_sender'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_[3]{"HOST_ALIASES"}{$data->{"_sender"}}) 0 0 0
406 # do some regex tricks...
407 0 $data->{'report'} =~ s/Environment\s+variables\:\n\n/Environment variables:\n\n CPAN_SMOKER = $_[HEAP]->{'HOST_ALIASES'}->{ $data->{'_sender'} } ( $data->{'_sender'} )\n/;
408 0 $data->{'_host'} = $_[HEAP]->{'HOST_ALIASES'}->{ $data->{'_sender'} };
409 }
410  
411 # send it off to the subprocess!
412 0 $data->{'_time'} = time;
413 0 $_[HEAP]->{'WHEEL'}->put( {
414 'ACTION' => 'SEND',
415 'DATA' => $data,
416 } );
417 0 $_[HEAP]->{'WHEEL_WORKING'} = $data;
418  
419 0 return;
420 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
421  
422 0 ▶sub save_failure : State {
Subroutine save_failure: not called CC=5 SCAR=34.0
Pod undocumented
423 0 my( $file, $reason ) = @_[ARG0, ARG1];
424  
425 # Get the filename only
426 0 my $filename = ( File::Spec->splitpath( $file ) )[2];
427  
428 # Create the "fail" subdirectory if it doesn't exist
429 0 my $faildir = File::Spec->catdir( $_[HEAP]->{'REPORTS'}, "fail" );
430 0 ▶ if ( ! -d $faildir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $faildir) 0 0 0
431 0 ▶ mkdir( $faildir ) or die "Unable to mkdir '$faildir': $!";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $faildir or die "Unable to mkdir '${faildir}': $!" 0 0 0
432 }
433  
434 # come up with a new name and move it
435 0 $filename = File::Spec->catfile( $faildir, $filename . '.' . $reason );
436 0 ▶ move( $file, $filename ) or die "Unable to move '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
move($file, $filename) or die "Unable to move '${file}': $!" 0 0 0
437  
438 # done with saving, let's retry the next report
439 # do we need to delay between emails?
440 0 ▶ if ( $_[HEAP]->{'DELAY'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'DELAY'} > 0) { } 0 0 0
441 0 $_[HEAP]->{'_delay'} = $_[KERNEL]->delay_set( 'send_report_delayed' => $_[HEAP]->{'DELAY'} );
442 } else {
443 0 $_[KERNEL]->yield( 'send_report' );
444 }
445  
446 0 return;
447 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
448  
449 0 ▶sub setup_wheel : State {
Subroutine setup_wheel: not called CC=5 SCAR=34.0
Pod undocumented
450 # skip setup if we already have a wheel, eh?
451 0 ▶ if ( defined $_[HEAP]->{'WHEEL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{"WHEEL"}) 0 0 0
452 0 $_[KERNEL]->yield( 'send_report' );
453 0 return;
454 }
455  
456 # Check if we should set up the wheel
457 0 ▶ if ( $_[HEAP]->{'WHEEL_RETRIES'} == 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{"WHEEL_RETRIES"} == 5) 0 0 0
458 0 die 'Tried ' . 5 . ' times to create a subprocess and is giving up...';
459 }
460  
461 # Set up the SubProcess we communicate with
462 0 my $pkg = __PACKAGE__ . '::' . $_[HEAP]->{'MAILER'};
463 0 $_[HEAP]->{'WHEEL'} = POE::Wheel::Run->new(
464 # What we will run in the separate process
465 'Program' => "$^X -M$pkg -e '${pkg}::main()'",
466  
467 # Kill off existing FD's
468 'CloseOnCall' => 1,
469  
470 # Redirect errors to our error routine
471 'ErrorEvent' => 'ChildError',
472  
473 # Send child died to our child routine
474 'CloseEvent' => 'ChildClosed',
475  
476 # Send input from child
477 'StdoutEvent' => 'Got_STDOUT',
478  
479 # Send input from child STDERR
480 'StderrEvent' => 'Got_STDERR',
481  
482 # Set our filters
483 'StdinFilter' => POE::Filter::Reference->new(), # Communicate with child via Storable::nfreeze
484 'StdoutFilter' => POE::Filter::Line->new(), # Receive input via plain lines ( OK/NOK )
485 'StderrFilter' => POE::Filter::Line->new(), # Plain ol' error lines
486 );
487  
488 # Check for errors
489 0 ▶ if ( ! defined $_[HEAP]->{'WHEEL'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[3]{'WHEEL'}) { } 0 0 0
490 0 die 'Unable to create a new wheel!';
491 } else {
492 # smart CHLD handling
493 0 ▶ if ( $_[KERNEL]->can( "sig_child" ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[2]->can('sig_child')) { } 0 0 0
494 0 $_[KERNEL]->sig_child( $_[HEAP]->{'WHEEL'}->PID => 'Got_CHLD' );
495 } else {
496 0 $_[KERNEL]->sig( 'CHLD', 'Got_CHLD' );
497 }
498  
499 # Increment our retry count
500 0 $_[HEAP]->{'WHEEL_RETRIES'}++;
501  
502 # it's obviously not working...
503 0 $_[HEAP]->{'WHEEL_WORKING'} = 0;
504  
505 # Since we created a new wheel, we have to give it the config
506 $_[HEAP]->{'WHEEL'}->put( {
507 'ACTION' => 'CONFIG',
508 0 'DATA' => $_[HEAP]->{'MAILER_CONF'},
509 } );
510  
511 # Do we need to send something?
512 0 $_[KERNEL]->yield( 'send_report' );
513 }
514  
515 0 return;
516 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
517  
518 # Handles child DIE'ing
519 0 ▶sub ChildClosed : State {
Subroutine ChildClosed: not called CC=3 SCAR=24.8
Pod undocumented
520 # Emit debugging information
521 0 if ( DEBUG ) {
522 warn "The subprocess died!";
523 }
524  
525 # Get rid of the wheel
526 0 undef $_[HEAP]->{'WHEEL'};
527  
528 # Should we process the next file?
529 0 ▶ if ( scalar @{ $_[HEAP]->{'NEWFILES'} } and ! $_[HEAP]->{'SHUTDOWN'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$_[3]{"NEWFILES"};} and not $_[3]{"SHUTDOWN"}) 0 0 0
Condition0%0/3
scalar @{$_[3]{"NEWFILES"};} and not $_[3]{"SHUTDOWN"}
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @{$_[3]{"NEWFILES"};} and not $_[3]{"SHUTDOWN"}
scalar @{$_[3]{"NEWFILES"};} not $_[3]{"SHUTDOWN"}
Truth table0%0/3
A and not(B)
A= scalar @{$_[3]{"NEWFILES"};}B= not $_[3]{"SHUTDOWN"}
ABresult
0X0
100
111
530 0 $_[KERNEL]->yield( 'wheel_setup' );
531 }
532  
533 0 return;
534 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
535  
536 # Handles child error
537 0 ▶sub ChildError : State {
Subroutine ChildError: not called CC=1 SCAR=6.9
Pod undocumented
538 # Emit warnings only if debug is on
539 0 if ( DEBUG ) {
540 # Copied from POE::Wheel::Run manpage
541 my ( $operation, $errnum, $errstr ) = @_[ ARG0 .. ARG2 ];
542 warn "Got an $operation error $errnum: $errstr\n";
543 }
544  
545 0 return;
546 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
547  
548 # Got a CHLD event!
549 0 ▶sub Got_CHLD : State {
Subroutine Got_CHLD: not called CC=1 SCAR=6.9
Pod undocumented
550 0 $_[KERNEL]->sig_handled();
551 0 return;
552 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
553  
554 # Handles child STDERR output
555 0 ▶sub Got_STDERR : State {
Subroutine Got_STDERR: not called CC=2 SCAR=17.9
Pod undocumented
556 0 my $input = $_[ARG0];
557  
558 # Skip empty lines as the POE::Filter::Line manpage says...
559 0 ▶ if ( $input eq '' ) { return }
Branch: 1 branch
BranchTrueFalseTotal
if ($input eq "") 0 0 0
560  
561 0 warn "Got STDERR from child, which should never happen ( $input )";
562  
563 0 return;
564 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
565  
566 # Handles child STDOUT output
567 0 ▶sub Got_STDOUT : State {
Subroutine Got_STDOUT: not called CC=16 SCAR=56.1
Pod undocumented
568 # The data!
569 0 my $data = $_[ARG0];
570  
571 0 if ( DEBUG ) {
572 warn "Got stdout ($data)";
573 }
574  
575 # We should get: "OK $msgid" or "NOK $error"
576 0 ▶ if ( $data =~ /^N?OK/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /^N?OK/) { } 0 0 0
elsif ($data =~ /^ERROR\s+(.+)\z/) { } 0 0 0
577 0 my $file = shift( @{ $_[HEAP]->{'NEWFILES'} } );
578 0 my $report = $_[HEAP]->{'WHEEL_WORKING'};
579 0 $_[HEAP]->{'WHEEL_WORKING'} = 0;
580  
581 0 ▶ if ( $data =~ /^OK\s+(.+)\z/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /^OK\s+(.+)\z/) { } 0 0 0
elsif ($data =~ /^NOK\s+(.+)\z/) { } 0 0 0
582 0 my $message_id = $1;
583  
584 0 ▶ if ( ! ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $file) { } 0 0 0
585 0 if ( DEBUG ) {
586 warn "Successfully sent report: $file";
587 }
588  
589 # get rid of the file and move on!
590 0 ▶ unlink( $file ) or die "Unable to delete $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or die "Unable to delete ${file}: $!" 0 0 0
591 } else {
592 0 if ( DEBUG ) {
593 warn "Successfully sent report: $file->{subject}";
594 }
595 }
596  
597 # do we need to delay between emails?
598 0 ▶ if ( $_[HEAP]->{'DELAY'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'DELAY'} > 0) { } 0 0 0
599 0 $_[HEAP]->{'_delay'} = $_[KERNEL]->delay_set( 'send_report_delayed' => $_[HEAP]->{'DELAY'} );
600 } else {
601 0 $_[KERNEL]->yield( 'send_report' );
602 }
603  
604 0 ▶ if ( defined $_[HEAP]->{'MAILDONE'} and ref $report ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{"MAILDONE"} and ref $report) 0 0 0
Condition0%0/3
defined $_[3]{"MAILDONE"} and ref $report
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[3]{"MAILDONE"} and ref $report
defined $_[3]{"MAILDONE"} ref $report
Truth table0%0/3
A and B
A= defined $_[3]{"MAILDONE"}B= ref $report
ABresult
0X0
100
111
605 $_[KERNEL]->post( $_[HEAP]->{'SESSION'} => $_[HEAP]->{'MAILDONE'}, {
606 0 'STARTTIME' => delete $report->{'_time'}, ## no critic ( ProhibitAccessOfPrivateData )
607 'STOPTIME' => time,
608 'DATA' => $report,
609 'STATUS' => 1,
610 'MSGID' => $message_id,
611 } );
612 }
613 } elsif ( $data =~ /^NOK\s+(.+)\z/ ) {
614 0 my $err = $1;
615  
616 # Is this a known error?
617 #
618 # This error happens with my postfix smtpd, because the link was left open too long between emails
619 # Unable to send report for '/home/cpan/cpan_reports/1260750049.58c5ed3e0013517d0f168975795c2bba95f2be79': Unable to set 'from'
620 # address: '4.4.2 mail.0ne.us Error: timeout exceeded' (421) at /usr/local/share/perl/5.10.0/Test/Reporter/POEGateway/Mailer.pm line 576.
621 0 ▶ if ( $err =~ /timeout\s+exceeded/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err =~ /timeout\s+exceeded/) 0 0 0
622 # Retry the email, but push it on the bottom of the queue!
623 0 if ( DEBUG ) {
624 warn "Received timeout for '$file', will give it another shot!";
625 }
626  
627 0 push( @{ $_[HEAP]->{'NEWFILES'} }, $file );
628  
629 # do we need to delay between emails?
630 0 ▶ if ( $_[HEAP]->{'DELAY'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'DELAY'} > 0) { } 0 0 0
631 0 $_[HEAP]->{'_delay'} = $_[KERNEL]->delay_set( 'send_report_delayed' => $_[HEAP]->{'DELAY'} );
632 } else {
633 0 $_[KERNEL]->yield( 'send_report' );
634 }
635  
636 0 return;
637 }
638  
639 # argh!
640 0 ▶ if ( ! ref $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $file) { } 0 0 0
641 0 warn "Unable to send report for '$file': $err";
642 0 $_[KERNEL]->yield( 'save_failure', $file, 'send' );
643 } else {
644 0 warn "Unable to send report for '$file->{subject}': $err";
645  
646 # do we need to delay between emails?
647 0 ▶ if ( $_[HEAP]->{'DELAY'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[3]{'DELAY'} > 0) { } 0 0 0
648 0 $_[HEAP]->{'_delay'} = $_[KERNEL]->delay_set( 'send_report_delayed' => $_[HEAP]->{'DELAY'} );
649 } else {
650 0 $_[KERNEL]->yield( 'send_report' );
651 }
652 }
653  
654 0 ▶ if ( defined $_[HEAP]->{'MAILDONE'} and ref $report ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[3]{"MAILDONE"} and ref $report) 0 0 0
Condition0%0/3
defined $_[3]{"MAILDONE"} and ref $report
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_[3]{"MAILDONE"} and ref $report
defined $_[3]{"MAILDONE"} ref $report
Truth table0%0/3
A and B
A= defined $_[3]{"MAILDONE"}B= ref $report
ABresult
0X0
100
111
655 $_[KERNEL]->post( $_[HEAP]->{'SESSION'} => $_[HEAP]->{'MAILDONE'}, {
656 0 'STARTTIME' => delete $report->{'_time'}, ## no critic ( ProhibitAccessOfPrivateData )
657 'STOPTIME' => time,
658 'DATA' => $report,
659 'STATUS' => 0,
660 'ERROR' => $err,
661 } );
662 }
663 } else {
664 0 die "Malformed line: '$data'";
665 }
666 } elsif ( $data =~ /^ERROR\s+(.+)\z/ ) {
667 # hmpf!
668 0 my $err = $1;
669 0 warn "Unexpected error: $err";
670 } else {
671 0 warn "Unknown line: '$data'";
672 }
673  
674 0 return;
675 1 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
676  
677 1 1;
678 __END__
↑ Summary POEGateway.pm »