Consumer.pm

filter: Statementstmt 33.8% 23 / 68 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 50.0% 7 / 14 Podpod 33.3% 2 / 6 totaltotal 24.6% 32 / 130 CC 17 SCAR 44.0
CC
17
Cov
22%
next
182.0
initialize
12.0
delete_previous
12.0
CRAP 81.7
« Producer.pm ↑ Summary ProducerConsumer/Base.pm »
1 package Amazon::SQS::Consumer;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use base 'Amazon::SQS::ProducerConsumer::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use JSON::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode qw( encode_utf8 is_utf8 );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 DEFAULT_N_MESSAGES => 10,
13 DEFAULT_WAIT_SECONDS => 30,
14 SECONDS_BETWEEN_TRIES => 10
15 1 };
16  
17 =head1 NAME
18  
19 Amazon::SQS::Consumer - Receive messages from an Amazon Simple Queue Service (SQS) queue
20  
21 =cut
22  
23 0 ▶sub say (@) { warn join ' ', (split ' ', scalar localtime)[2,1,4,3], "[$$]", (split '/', $0)[-1], @_, "\n"; return @_; }
Subroutine say: not called CC=1 SCAR=6.9
Pod undocumented
24 0 ▶$SIG{INT} = sub { say 'caught signal INT'; exit 0; };
Subroutine __ANON__: not called CC=1 SCAR=2.6
25 1 $SIG{CHLD} = 'IGNORE';
26  
27 =head1 SYNOPSIS
28  
29 use Amazon::SQS::Consumer;
30  
31 my $in_queue = new Amazon::SQS::Consumer
32 AWSAccessKeyId => 'PUBLIC_KEY_HERE',
33 SecretAccessKey => 'SECRET_KEY_HERE',
34 queue => 'YourInputQueue';
35  
36 while ( my $item = $in_queue->next ) {
37 # Do stuff with the item
38 }
39  
40 =head1 METHODS
41  
42 =head2 new(%params)
43  
44 This is the constructor, it will return you an Amazon::SQS::Consumer object to work with. It takes these parameters:
45  
46 =over
47  
48 =item AWSAccessKeyId (required)
49  
50 Your AWS access key.
51  
52 =item SecretAccessKey (required)
53  
54 Your secret key, WARNING! don't give this out or someone will be able to use your account and incur charges on your behalf.
55  
56 =item queue (required)
57  
58 The URL of the queue to receive messages from.
59  
60 =item wait_seconds (optional)
61  
62 The number of seconds to wait for a new message when the queue is empty.
63  
64 =item debug (optional)
65  
66 A flag to turn on debugging. It is turned off by default.
67  
68 =back
69  
70 =cut
71  
72 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
73 0 my $class = shift;
74 0 my %args = @_;
75  
76 0 my $me = \%args;
77 0 bless $me, $class;
78 0 $me->initialize;
79 0 return $me;
80 }
81  
82 0 ▶sub initialize {
Subroutine initialize: not called CC=3 SCAR=24.8
Pod undocumented
83 0 my $me = shift;
84  
85 0 ▶ $me->{n_messages} ||= DEFAULT_N_MESSAGES;
Condition0%0/2
$me->{"n_messages"} ||= 10
l!l
00
MC/DC0%0/1
$me->{"n_messages"} ||= 10
$me->{"n_messages"}
Truth table0%0/2
A
A= $me->{"n_messages"}
Aresult
11
00
86 0 ▶ $me->{wait_seconds} ||= DEFAULT_WAIT_SECONDS;
Condition0%0/2
$me->{"wait_seconds"} ||= 30
l!l
00
MC/DC0%0/1
$me->{"wait_seconds"} ||= 30
$me->{"wait_seconds"}
Truth table0%0/2
A
A= $me->{"wait_seconds"}
Aresult
11
00
87 0 $me->SUPER::initialize;
88 }
89  
90 =head2 next()
91  
92 This will receive a message from this Publisher's queue. When the queue is empty it will wait a new message for wait_seconds seconds.
93  
94 =cut
95  
96 0 ▶sub next {
Subroutine next: not called CC=13 SCAR=52.0
Pod documented
97 0 my $me = shift;
98  
99 # If we're done with the previous message, delete it
100 0 $me->delete_previous();
101  
102 0 ▶ if ( @ARGV ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ARGV) 0 0 0
103 0 $me->{messages} = [ map { MessageId => undef, Body => $_ }, @ARGV ];
104 0 undef @ARGV;
105 0 $me->{no_loop} = 't';
106 }
107  
108 0 my $seconds_to_wait = $me->{wait_seconds};
109 do {
110  
111 # If there no messages in the cache, get some from the queue
112 $me->{messages} = $me->receive_messages(
113 Queue => $me->{queue},
114 MaxNumberOfMessages => $me->{n_messages},
115 defined $me->{timeout} ? ( VisibilityTimeout => $me->{timeout} ) : ()
116 0 ▶ ) unless defined $me->{messages} && @{$me->{messages}} or $me->{no_loop};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $me->{"messages"} and @{$me->{"messages"};} or $me->{"no_loop"} 0 0 0
defined $me->{'timeout'} ? : 0 0 0
Condition0%0/6
defined $me->{"messages"} and @{$me->{"messages"};} or $me->{"no_loop"}
l!l&&r!l&&!r
000
defined $me->{"messages"} and @{$me->{"messages"};}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $me->{"messages"} and @{$me->{"messages"};} or $me->{"no_loop"}
defined $me->{"messages"} @{$me->{"messages"};} $me->{"no_loop"}
Truth table0%0/5
A and B or C
A= defined $me->{"messages"}B= @{$me->{"messages"};}C= $me->{"no_loop"}
ABCresult
11X1
0X11
1011
0X00
1000
117  
118 # If there's a message in the cache, return it
119 0 ▶ if ( my $message = shift @{$me->{messages}} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $message = shift @{$$me{'messages'};}) { } 0 0 0
elsif ($me->{'no_loop'}) { } 0 0 0
120 0 $me->{DeleteMessageHandle} = $message->{ReceiptHandle};
121 0 my $object;
122 0 eval {
123 0 my $body = $message->{Body};
124 0 ▶ $body = encode_utf8( $body ) if is_utf8( $body );
Branch: 1 branch
BranchTrueFalseTotal
if &is_utf8($body) 0 0 0
125 0 $object = decode_json $body;
126 };
127 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
128 0 say "left bad message in queue; could not decode JSON from $message->{Body}: $@";
129 } else {
130 0 return $object;
131 }
132 } elsif ( $me->{no_loop} ) {
133 0 $seconds_to_wait = 0;
134 } else {
135 # Otherwise, wait a few seconds and try again
136 say "waiting $seconds_to_wait seconds for new messages"
137 0 ▶ if $seconds_to_wait == $me->{wait_seconds};
Branch: 1 branch
BranchTrueFalseTotal
if $seconds_to_wait == $me->{"wait_seconds"} 0 0 0
138 0 sleep SECONDS_BETWEEN_TRIES;
139 0 $seconds_to_wait -= SECONDS_BETWEEN_TRIES;
140 }
141  
142 0 ▶ } while ( $me->{forever} or $seconds_to_wait > 0 );
Branch: 1 branch
BranchTrueFalseTotal
$me->{"forever"} || $seconds_to_wait > 0 and do { do { $me->{"messages"} = $me->receive_messages("Queue", $me->{"queue"}, "MaxNumberOfMessages", $me->{"n_messages"}, defined $me->{"timeout"} ? ("VisibilityTimeout", $me->{"timeout"}) : ()) unless defined $me->{"messages"} and @{$me->{"messages"};} or $me->{"no_loop"}; if (my $message = shift @{$$me{"messages"};}) { $me->{"DeleteMessageHandle"} = $message->{"ReceiptHandle"}; my $object; eval { do { my $body = $message->{"Body"}; $body = &encode_utf8($body) if &is_utf8($body); $object = &decode_json($body) } }; if ($@) { &say(qq[left bad message in queue; could not decode JSON from $message->{"Body"}: $@]); } else { return $object; } ; } elsif ($me->{"no_loop"}) { $seconds_to_wait = 0; } else { &say("waiting $seconds_to_wait seconds for new messages") if $seconds_to_wait == $me->{"wait_seconds"}; sleep 10; $seconds_to_wait -= 10; } } } 0 0 0
Condition0%0/3
$me->{"forever"} or $seconds_to_wait > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$me->{"forever"} or $seconds_to_wait > 0
$me->{"forever"} $seconds_to_wait > 0
Truth table0%0/3
A or B
A= $me->{"forever"}B= $seconds_to_wait > 0
ABresult
1X1
011
000
143  
144 # If we've retried for a while and gotten no messages, give up
145 0 return undef;
146  
147 }
148  
149 0 ▶sub delete_previous {
Subroutine delete_previous: not called CC=3 SCAR=24.8
Pod undocumented
150 0 my $me = shift;
151  
152 0 ▶ if ( $me->{DeleteMessageHandle} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"DeleteMessageHandle"}) 0 0 0
153 0 ▶ say "deleting message $me->{DeleteMessageHandle}" if $me->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $me->{"debug"} 0 0 0
154 0 $me->delete_message( Queue => $me->{queue}, ReceiptHandle => $me->{DeleteMessageHandle} );
155 }
156 }
157  
158 0 ▶sub defer { delete $_[0]->{DeleteMessageHandle} }
Subroutine defer: not called CC=1 SCAR=6.9
Pod undocumented
159  
160  
161 =head1 AUTHOR
162  
163 Nic Wolff, <nic@angel.net>
164  
165 =head1 BUGS
166  
167 Please report any bugs or feature requests to C<bug-amazon-sqs-producerconsumer at rt.cpan.org>, or through
168 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Amazon-SQS-ProducerConsumer>. I will be notified, and then you'll
169 automatically be notified of progress on your bug as I make changes.
170  
171  
172  
173  
174 =head1 SUPPORT
175  
176 You can find documentation for this module with the perldoc command.
177  
178 perldoc Amazon::SQS::ProducerConsumer
179  
180  
181 You can also look for information at:
182  
183 =over 4
184  
185 =item * RT: CPAN's request tracker (report bugs here)
186  
187 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Amazon-SQS-ProducerConsumer>
188  
189 =item * AnnoCPAN: Annotated CPAN documentation
190  
191 L<http://annocpan.org/dist/Amazon-SQS-ProducerConsumer>
192  
193 =item * CPAN Ratings
194  
195 L<http://cpanratings.perl.org/d/Amazon-SQS-ProducerConsumer>
196  
197 =item * Search CPAN
198  
199 L<http://search.cpan.org/dist/Amazon-SQS-ProducerConsumer/>
200  
201 =back
202  
203  
204 =head1 ACKNOWLEDGEMENTS
205  
206  
207 =head1 LICENSE AND COPYRIGHT
208  
209 Copyright 2011 Nic Wolff.
210  
211 This program is free software; you can redistribute it and/or modify it
212 under the terms of either: the GNU General Public License as published
213 by the Free Software Foundation; or the Artistic License.
214  
215 See http://dev.perl.org/licenses/ for more information.
216  
217  
218 =cut
219  
220 1 1; # End of Amazon::SQS::ProducerConsumer
« Producer.pm ↑ Summary ProducerConsumer/Base.pm »