Notify/NoticePool.pm

filter: Statementstmt 26.3% 44 / 167 Branchbran 5.1% 3 / 58 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 36.3% 8 / 22 Podpod 0.0% 0 / 16 totaltotal 20.1% 56 / 278 CC 35 SCAR 30.9
CC
35
Cov
21%
updateOutstanding
73.7
addTransport
20.0
addNotice
20.0
CRAP 21.9
↑ Summary Notify/Email.pm »
1  
2 package Notify::NoticePool;
3  
4 1 require 5.00503;
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Notify::Notice;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Tie::Persistent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 require Exporter;
11  
12 1 our @ISA = qw( Exporter );
13 1 our %EXPORT_TAGS = ( 'all' => [ qw( DEFAULT_RESEND_INTERVAL DEFAULT_MAX_RETRIES ) ] );
14 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
15 1 our @EXPORT = qw( );
16 #our $VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
17 1 our $VERSION = '0.0.1';
18  
19 # Set our notification constants
20 1 ▶use constant DEFAULT_RESEND_INTERVAL => 300;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant DEFAULT_MAX_RETRIES => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 our $resend_interval = DEFAULT_RESEND_INTERVAL;
23 1 our $max_retries = DEFAULT_MAX_RETRIES;
24  
25 1 ▶sub new {
Subroutine new: called CC=5 SCAR=17.1
Pod undocumented
26  
27 1 my ($self, $options) = @_;
28 1 ▶ my $class = ref($self) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
29 1 my $this = {};
30 1 bless $this, $class;
31  
32 confess "Error creating Notice Pool: No file store given."
33 1 ▶ unless exists $options->{'file_store'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"file_store"} 0 1 1
34  
35 1 $this->{'__NOTICE_FILE'} = $options->{'file_store'};
36 $this->{'__TRANSPORT'} = (exists $options->{'transport'})
37 1 ▶ ? $options->{'transport'} : { };
Branch: 1 branch
BranchTrueFalseTotal
exists $options->{'transport'} ? : 1 0 1
38  
39 $this->updateOutstanding ()
40 1 ▶ unless exists $options->{'no_implicit_update'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $options->{"no_implicit_update"} 1 0 1
41  
42 1 return $this;
43  
44 } #end sub new
45  
46 0 ▶sub setResendInterval {
Subroutine setResendInterval: not called CC=2 SCAR=17.9
Pod undocumented
47  
48 0 my ($self, $interval) = @_;
49  
50 0 ▶ confess "Error setting resend interval: No interval given."
Branch: 1 branch
BranchTrueFalseTotal
unless $interval 0 0 0
51 unless $interval;
52  
53 0 $resend_interval = $interval;
54  
55 } #end sub setResendInterval
56  
57 0 ▶sub getResendInterval {
Subroutine getResendInterval: not called CC=1 SCAR=6.9
Pod undocumented
58  
59 0 my ($self) = @_;
60 0 return $resend_interval;
61  
62 } #end getResendInterval
63  
64 0 ▶sub setMaxRetries {
Subroutine setMaxRetries: not called CC=2 SCAR=17.9
Pod undocumented
65  
66 0 my ($self, $retries) = @_;
67  
68 0 ▶ confess "Error setting number of max retries: No maximum given."
Branch: 1 branch
BranchTrueFalseTotal
unless $retries 0 0 0
69 unless $retries;
70  
71 0 $max_retries = $retries;
72  
73 } #end sub setMaxRetries
74  
75 0 ▶sub getMaxRetries {
Subroutine getMaxRetries: not called CC=1 SCAR=6.9
Pod undocumented
76  
77 0 my ($self) = @_;
78 0 return $max_retries;
79  
80 } #end sub getMaxRetries
81  
82 0 ▶sub addTransport {
Subroutine addTransport: not called CC=4 SCAR=30.0
Pod undocumented
83  
84 0 my ($self, $entries) = @_;
85  
86 0 foreach (keys %$entries) {
87  
88 confess "Error adding transport entry for type $_. Invalid transport object."
89 0 ▶ unless defined $entries->{$_}->send and defined $entries->{$_}->receive;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $entries->{$_}->send and defined $entries->{$_}->receive 0 0 0
Condition0%0/3
defined $entries->{$_}->send and defined $entries->{$_}->receive
!ll&&!rl&&r
000
MC/DC0%0/2
defined $entries->{$_}->send and defined $entries->{$_}->receive
defined $entries->{$_}->send defined $entries->{$_}->receive
Truth table0%0/3
A and B
A= defined $entries->{$_}->sendB= defined $entries->{$_}->receive
ABresult
0X0
100
111
90  
91 0 $self->{'__TRANSPORT'}->{$_} = $entries->{$_};
92  
93 }
94  
95 } #end sub addTransport
96  
97 0 ▶sub getUniqueID {
Subroutine getUniqueID: not called CC=2 SCAR=17.9
Pod undocumented
98  
99 0 my ($self) = @_;
100 0 my %tied;
101  
102 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
103 0 my $lastid = $tied{'__LAST_ID'} + 1;
104 0 ▶ while (exists $tied{ $lastid }) { $lastid++; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tied{$lastid}) 0 0 0
105 0 untie %tied;
106  
107 0 return $lastid;
108  
109 } #end sub getUniqueID
110  
111 0 ▶sub exists {
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
112  
113 0 my ($self, $id) = @_;
114 0 my %tied;
115  
116 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
117 0 my $result = exists $tied{$id};
118 0 untie %tied;
119  
120 0 return $result;
121  
122 } #end sub exists
123  
124 0 ▶sub addNotice {
Subroutine addNotice: not called CC=4 SCAR=30.0
Pod undocumented
125  
126 0 my ($self, $notice) = @_;
127 0 my %tied;
128  
129 0 ▶ confess "Error adding notice: No notice given."
Branch: 1 branch
BranchTrueFalseTotal
unless $notice 0 0 0
130 unless $notice;
131  
132 0 my $notice_attribs = $notice->getNotice ();
133  
134 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
135  
136 0 ▶ unless (exists $tied{ $notice_attribs->{'id'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tied{$notice_attribs->{'id'}}) { } 0 0 0
137  
138 0 $notice_attribs->{'time_created'} = $notice_attribs->{'time_updated'} = time ();
139 0 $tied{ $notice_attribs->{'id'} } = $notice_attribs;
140  
141 # Update the last used ID
142 0 $tied{'__LAST_ID'} = $notice_attribs->{'id'};
143  
144 }
145 else {
146 0 return undef;
147 }
148  
149 0 untie %tied;
150  
151 0 my $new_notice = new Notify::Notice ($notice_attribs);
152 0 my $updated = $self->sendIfAppropriate ($new_notice);
153  
154 0 ▶ return ($updated) ? $updated : $new_notice;
Branch: 1 branch
BranchTrueFalseTotal
$updated ? : 0 0 0
155  
156 } #end sub addNotice
157  
158 0 ▶sub sendIfAppropriate {
Subroutine sendIfAppropriate: not called CC=3 SCAR=24.8
Pod undocumented
159  
160 0 my ($self, $notice) = @_;
161 0 my $attribs = $notice->getNotice ();
162 0 my %tied;
163  
164 # Try to send the notice immediate with an updated object
165 # and update the persistent object on disk. Note that we
166 # had to untie before trying the send in order to have
167 # guaranteed messaging.
168  
169 0 ▶ if ($attribs->{'status'} == OUTGOING_PENDING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribs->{'status'} == 1) { } 0 0 0
170  
171 0 ▶ if ($self->sendNotice ($notice)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sendNotice($notice)) { } 0 0 0
172 0 $attribs->{'status'} = WAITING_RESPONSE;
173 }
174 else {
175 0 $attribs->{'attempts'}++;
176 }
177  
178 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
179 0 $tied{ $attribs->{'id'} } = $attribs;
180 0 untie %tied;
181  
182 0 return new Notify::Notice ($attribs);
183  
184 }
185 else {
186 0 return undef;
187 }
188  
189 } #end sub sendIfAppropriate
190  
191 0 ▶sub resolveNotice {
Subroutine resolveNotice: not called CC=3 SCAR=24.8
Pod undocumented
192  
193 0 my ($self, $notice) = @_;
194 0 my %tied;
195  
196 0 ▶ confess "Error resolving notice: No notice given."
Branch: 1 branch
BranchTrueFalseTotal
unless $notice 0 0 0
197 unless $notice;
198  
199 0 my $notice_attribs = $notice->getNotice ();
200  
201 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
202 0 ▶ return undef unless exists $tied{ $notice_attribs->{'id'} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tied{$notice_attribs->{"id"}} 0 0 0
203 0 delete $tied{ $notice_attribs->{'id'} };
204 0 untie %tied;
205  
206 0 return 1;
207  
208 } #end sub resolveNotice
209  
210 0 ▶sub retrieveNotice {
Subroutine retrieveNotice: not called CC=4 SCAR=30.0
Pod undocumented
211  
212 0 my ($self, $notice) = @_;
213 0 my %tied;
214 0 my ($notice_attribs, $db_attribs);
215  
216 0 $notice_attribs = $notice->getNotice ();
217  
218 0 ▶ confess "Error retrieving notice: No notice given."
Branch: 1 branch
BranchTrueFalseTotal
unless $notice 0 0 0
219 unless $notice;
220  
221 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'r';
222 0 ▶ if (exists $tied{ $notice_attribs->{'id'} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tied{$notice_attribs->{'id'}}) { } 0 0 0
223 0 $db_attribs = $tied{ $notice_attribs->{'id'} };
224 }
225 else {
226 0 return undef;
227 }
228 0 untie %tied;
229  
230 0 ▶ if ($db_attribs->{'status'} == WAITING_PROCESSING) {
Branch: 1 branch
BranchTrueFalseTotal
if ($db_attribs->{"status"} == 3) 0 0 0
231  
232 # Mark that the notification is transaction is considered
233 # done unless the application updated the notification
234 # back to OUTGOING_PENDING
235 0 $db_attribs->{'status'} = DONE;
236 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
237 0 $tied{ $db_attribs->{'id'} } = $db_attribs;
238 0 untie %tied;
239  
240 }
241  
242 0 return new Notify::Notice ($db_attribs);
243  
244 } #end sub retreiveNotice
245  
246 0 ▶sub updateNotice {
Subroutine updateNotice: not called CC=4 SCAR=30.0
Pod undocumented
247  
248 0 my ($self, $notice) = @_;
249 0 my %tied;
250 0 my ($new_attribs, $old_attribs);
251  
252 0 ▶ confess "Error updating notice: No notice given."
Branch: 1 branch
BranchTrueFalseTotal
unless $notice 0 0 0
253 unless $notice;
254  
255 0 $new_attribs = $notice->getNotice ();
256  
257 0 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
258  
259 0 ▶ return undef unless exists $tied{ $new_attribs->{'id'} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $tied{$new_attribs->{"id"}} 0 0 0
260 0 $old_attribs = $tied{$new_attribs->{'id'}};
261 0 push @{ $new_attribs->{'history'} }, $old_attribs->{'message'};
262 0 $new_attribs->{'time_updated'} = time ();
263 0 $tied{ $new_attribs->{'id'} } = $new_attribs;
264  
265 0 untie %tied;
266  
267 0 my $new_notice = new Notify::Notice ($new_attribs);
268 0 my $updated = $self->sendIfAppropriate ($new_notice);
269  
270 0 ▶ return ($updated) ? $updated : $new_notice;
Branch: 1 branch
BranchTrueFalseTotal
$updated ? : 0 0 0
271  
272 } #end sub updateNotice
273  
274 1 ▶sub updateOutstanding {
Subroutine updateOutstanding: called CC=10 SCAR=43.0
Pod undocumented
275  
276 1 my ($self) = @_;
277 1 my %tied;
278  
279 1 tie %tied, 'Tie::Persistent', $self->{'__NOTICE_FILE'}, 'rw';
280  
281 1 foreach my $key (keys %tied) {
282  
283 # Skip the special LASTID key
284 0 ▶ next if $key =~ /^__LAST_ID$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /^__LAST_ID$/ 0 0 0
285  
286 0 my $notice_attribs = $tied{$key};
287  
288 0 ▶ if ($notice_attribs->{'status'} == OUTGOING_PENDING) {
Branch: 2 branches
BranchTrueFalseTotal
if ($notice_attribs->{'status'} == 1) { } 0 0 0
elsif ($notice_attribs->{'status'} == 2) { } 0 0 0
289  
290 # Check to see if we are within the send interval
291 next unless $notice_attribs->{'attempts'} and
292 0 ▶ (time () - $notice_attribs->{'time_updated'} > $resend_interval);
Branch: 1 branch
BranchTrueFalseTotal
unless $notice_attribs->{"attempts"} and time - $notice_attribs->{"time_updated"} > $Notify::NoticePool::resend_interval 0 0 0
Condition0%0/3
$notice_attribs->{"attempts"} and time - $notice_attribs->{"time_updated"} > $Notify::NoticePool::resend_interval
!ll&&!rl&&r
000
MC/DC0%0/2
$notice_attribs->{"attempts"} and time - $notice_attribs->{"time_updated"} > $Notify::NoticePool::resend_interval
$notice_attribs->{"attempts"} time - $notice_attribs->{"time_updated"} > $Notify::NoticePool::resend_interval
Truth table0%0/3
A and B
A= $notice_attribs->{"attempts"}B= time - $notice_attribs->{"time_updated"} > $Notify::NoticePool::resend_interval
ABresult
0X0
100
111
293  
294 0 my $outgoing = new Notify::Notice ($notice_attribs);
295  
296 0 ▶ if ($self->sendNotice ($outgoing)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sendNotice($outgoing)) { } 0 0 0
297  
298 0 $notice_attribs->{'status'} = WAITING_RESPONSE;
299 0 $notice_attribs->{'time_updated'} = time ();
300  
301 }
302 else {
303  
304 0 $notice_attribs->{'attempts'}++;
305  
306 0 ▶ if ($notice_attribs->{'attempts'} > $max_retries) {
Branch: 1 branch
BranchTrueFalseTotal
if ($notice_attribs->{"attempts"} > $Notify::NoticePool::max_retries) 0 0 0
307 0 $notice_attribs->{'status'} = FAILURE;
308 }
309  
310 }
311  
312 }
313 elsif ($notice_attribs->{'status'} == WAITING_RESPONSE) {
314  
315 0 my $incoming = new Notify::Notice ($notice_attribs);
316 0 my $response = $self->getNoticeResponse ($incoming);
317  
318 0 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 0 0
319  
320 0 push @{ $notice_attribs->{'history'} }, $notice_attribs->{'message'};
321 0 $notice_attribs->{'message'} = $response;
322 0 $notice_attribs->{'status'} = WAITING_PROCESSING;
323 0 $notice_attribs->{'time_updated'} = time ();
324  
325 }
326  
327 }
328  
329 # Assign the copy to the persistent DB
330 0 $tied{$key} = $notice_attribs;
331  
332 }
333  
334 1 untie %tied;
335  
336 1 return 1;
337  
338 } #end sub updateOutstanding
339  
340 0 ▶sub sendNotice {
Subroutine sendNotice: not called CC=2 SCAR=17.9
Pod undocumented
341  
342 0 my ($self, $notice) = @_;
343 0 my $attribs = $notice->getNotice ();
344  
345 confess "Error: Attempted to send notice to undefined transport type."
346 0 ▶ unless exists $self->{'__TRANSPORT'}->{ $attribs->{'transport'} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__TRANSPORT"}{$attribs->{"transport"}} 0 0 0
347  
348 0 return $self->{'__TRANSPORT'}->{ $attribs->{'transport'} }->send ($notice);
349  
350 } #end sub sendNotice
351  
352 0 ▶sub getNoticeResponse {
Subroutine getNoticeResponse: not called CC=2 SCAR=17.9
Pod undocumented
353  
354 0 my ($self, $notice) = @_;
355 0 my $attribs = $notice->getNotice ();
356  
357 confess "Error: Attempted to retrieve notice from undefined transport type."
358 0 ▶ unless exists $self->{'__TRANSPORT'}->{ $attribs->{'transport'} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"__TRANSPORT"}{$attribs->{"transport"}} 0 0 0
359  
360 0 return $self->{'__TRANSPORT'}->{ $attribs->{'transport'} }->receive ($notice);
361  
362 } #end sub getNoticeResponse
363  
364 1 1;
365  
366 __END__
↑ Summary Notify/Email.pm »