Producer.pm

filter: Statementstmt 22.9% 20 / 87 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 46.1% 6 / 13 Podpod 33.3% 2 / 6 totaltotal 18.0% 28 / 155 CC 21 SCAR 45.6
CC
21
Cov
15%
publish
210.0
fork_consumer
30.0
fork_and_publish
12.0
CRAP 95.4
↑ Summary Consumer.pm »
1 package Amazon::SQS::Producer;
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  
10 1 ▶use constant MAX_RETRIES => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 =head1 NAME
13  
14 Amazon::SQS::Producer - Publish messages to an Amazon Simple Queue Service (SQS) queue
15  
16 =cut
17  
18 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
19 0 ▶$SIG{INT} = sub { say 'caught signal INT'; exit 0; };
Subroutine __ANON__: not called CC=1 SCAR=2.6
20 1 $SIG{CHLD} = 'IGNORE';
21  
22 =head1 SYNOPSIS
23  
24 use Amazon::SQS::Producer;
25  
26 my $out_queue = new Amazon::SQS::Producer
27 AWSAccessKeyId => 'PUBLIC_KEY_HERE',
28 SecretAccessKey => 'SECRET_KEY_HERE',
29 queue => 'YourOutputQueue',
30 consumer => 'ConsumerForOutputQueue';
31  
32 $out_queue->publish(
33 $existingObjectRef,
34 url => $enclosure_URL,
35 pubdate => $pubDate,
36 title => $title,
37 description => $description,
38 rss_guid => $guid,
39 );
40  
41 =head1 METHODS
42  
43 =head2 new(%params)
44  
45 This is the constructor, it will return you an Amazon::SQS::Producer object to work with. It takes these parameters:
46  
47 =over
48  
49 =item AWSAccessKeyId (required)
50  
51 Your AWS access key.
52  
53 =item SecretAccessKey (required)
54  
55 Your secret key, WARNING! don't give this out or someone will be able to use your account and incur charges on your behalf.
56  
57 =item queue (required)
58  
59 The URL of the queue to publish messages to.
60  
61 =item consumer (optional)
62  
63 The name of an executable that will consume messages from the queue we're publishing to. An instance will be launched after the each message is published, up to the maximum set by...
64  
65 =item start_consumers (optional)
66  
67 The maximum number of consumer instance to launch.
68  
69 =item debug (optional)
70  
71 A flag to turn on debugging. It is turned off by default.
72  
73 =back
74  
75 =cut
76  
77 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
78 0 my $class = shift;
79 0 my %args = @_;
80  
81 0 my $me = \%args;
82 0 bless $me, $class;
83 0 $me->initialize;
84 0 return $me;
85 }
86  
87 0 ▶sub initialize {
Subroutine initialize: not called CC=2 SCAR=17.9
Pod undocumented
88 0 my $me = shift;
89  
90 0 ▶ $me->{sleep_after_starting_consumer} = 2 if not exists $me->{sleep_after_starting_consumer};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $me->{"sleep_after_starting_consumer"} 0 0 0
91 0 $me->SUPER::initialize;
92 }
93  
94 =head2 publish(%params)
95  
96 This will publish a message to this Publisher's queue, and start a consumer if this is the first message this Publisher has published. The message body will be a JSON representaton of the method's argument hash. If the first argument is a reference to a hash it will be dereferenced and merged with the other parameters given.
97  
98 =cut
99  
100 0 ▶sub publish {
Subroutine publish: not called CC=14 SCAR=53.5
Pod documented
101 0 ▶ if ( ref $_[0] and ! $_[0]->{queue} ) { goto &fork_consumer }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and not $_[0]{"queue"}) 0 0 0
Condition0%0/3
ref $_[0] and not $_[0]{"queue"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and not $_[0]{"queue"}
ref $_[0] not $_[0]{"queue"}
Truth table0%0/3
A and not(B)
A= ref $_[0]B= not $_[0]{"queue"}
ABresult
0X0
100
111
102 0 ▶ if ( ref $_[1] and $_[1]->{_chain_consumers} ) { goto &fork_consumer }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] and $_[1]{"_chain_consumers"}) 0 0 0
Condition0%0/3
ref $_[1] and $_[1]{"_chain_consumers"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[1] and $_[1]{"_chain_consumers"}
ref $_[1] $_[1]{"_chain_consumers"}
Truth table0%0/3
A and B
A= ref $_[1]B= $_[1]{"_chain_consumers"}
ABresult
0X0
100
111
103  
104 0 my $me = shift;
105 0 ▶ my $old_data = shift if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
106 0 my $data = { %$old_data, @_ };
107 0 my $encoded_data = encode_json $data;
108  
109 0 ▶ say "Queueing message: $encoded_data" if $data->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"_debug"} 0 0 0
110 0 ▶ return if $data->{_test};
Branch: 1 branch
BranchTrueFalseTotal
if $data->{"_test"} 0 0 0
111  
112 0 my $retries;
113 my $message_id;
114 0 ▶ until (
Branch: 1 branch
BranchTrueFalseTotal
unless ($message_id = $me->send_message("Queue", $me->{"queue"}, "MessageBody", $encoded_data)) 0 0 0
115 $message_id = $me->send_message(
116 Queue => $me->{queue},
117 MessageBody => $encoded_data,
118 )
119 ) {
120 0 say "couldn't queue message: ", $me->error;
121 0 ▶ if ( $retries++ < MAX_RETRIES ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retries++ < 3) { } 0 0 0
122 0 say "trying again in $retries seconds";
123 0 sleep $retries;
124 } else {
125 0 say "giving up trying to publish to queue $me->{queue} with message body: $encoded_data",
126 return;
127 }
128 }
129  
130 0 ▶ if ( $me->{consumer} and $me->{started_consumers}++ < $me->{start_consumers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"consumer"} and $me->{"started_consumers"}++ < $me->{"start_consumers"}) 0 0 0
Condition0%0/3
$me->{"consumer"} and $me->{"started_consumers"}++ < $me->{"start_consumers"}
!ll&&!rl&&r
000
MC/DC0%0/2
$me->{"consumer"} and $me->{"started_consumers"}++ < $me->{"start_consumers"}
$me->{"consumer"} $me->{"started_consumers"}++ < $me->{"start_consumers"}
Truth table0%0/3
A and B
A= $me->{"consumer"}B= $me->{"started_consumers"}++ < $me->{"start_consumers"}
ABresult
0X0
100
111
131 0 my $pid = fork;
132 0 ▶ if ( not defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif (not $pid) { } 0 0 0
133 0 say "couldn't fork";
134 } elsif ( not $pid ) {
135 0 close STDIN; open STDIN, '/dev/null';
136 0 close STDOUT; open STDOUT, '/dev/null';
137 0 close STDERR; open STDERR, '>>/tmp/getfeeds.log';
138 0 sleep $me->{sleep_after_starting_consumer};
139 0 exec $me->{consumer};
140 } else {
141 0 say "started consumer $me->{consumer} with PID $pid for queue $me->{queue}";
142 }
143 }
144  
145 0 return $message_id;
146  
147 }
148  
149 0 ▶sub fork_and_publish {
Subroutine fork_and_publish: not called CC=3 SCAR=24.8
Pod undocumented
150 0 my $me = shift;
151  
152 0 my $pid = fork;
153 0 ▶ if ( not defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif (not $pid) { } 0 0 0
154 0 say "couldn't fork";
155 } elsif ( not $pid ) {
156 0 $me->publish( @_ );
157 } else {
158 0 say "forked to publish to queue $me->{queue} with PID $pid";
159 }
160 }
161  
162 0 ▶sub fork_consumer {
Subroutine fork_consumer: not called CC=5 SCAR=34.0
Pod undocumented
163 0 my $me = shift;
164 0 ▶ my $old_data = shift if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 0 0 0
165 0 my %data = @_;
166  
167 0 ▶ if ( $me->{consumer} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me->{"consumer"}) 0 0 0
168 0 my $pid = fork;
169 0 ▶ if ( not defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif (not $pid) { } 0 0 0
170 0 say "couldn't fork";
171 } elsif ( not $pid ) {
172 0 close STDIN; open STDIN, '/dev/null';
173 0 close STDOUT; open STDOUT, '/dev/null';
174 0 close STDERR; open STDERR, '>>/tmp/getfeeds.log';
175 0 $ENV{PATH} .= ':.';
176 0 sleep $me->{sleep_after_starting_consumer};
177 0 exec $me->{consumer}, encode_json { %$old_data, %data };
178 } else {
179 0 say "forked consumer $me->{consumer} with PID $pid";
180 }
181 }
182 }
183  
184 =head1 AUTHOR
185  
186 Nic Wolff, <nic@angel.net>
187  
188 =head1 BUGS
189  
190 Please report any bugs or feature requests to C<bug-amazon-sqs-producerconsumer at rt.cpan.org>, or through
191 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Amazon-SQS-ProducerConsumer>. I will be notified, and then you'll
192 automatically be notified of progress on your bug as I make changes.
193  
194  
195  
196  
197 =head1 SUPPORT
198  
199 You can find documentation for this module with the perldoc command.
200  
201 perldoc Amazon::SQS::ProducerConsumer
202  
203  
204 You can also look for information at:
205  
206 =over 4
207  
208 =item * RT: CPAN's request tracker (report bugs here)
209  
210 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Amazon-SQS-ProducerConsumer>
211  
212 =item * AnnoCPAN: Annotated CPAN documentation
213  
214 L<http://annocpan.org/dist/Amazon-SQS-ProducerConsumer>
215  
216 =item * CPAN Ratings
217  
218 L<http://cpanratings.perl.org/d/Amazon-SQS-ProducerConsumer>
219  
220 =item * Search CPAN
221  
222 L<http://search.cpan.org/dist/Amazon-SQS-ProducerConsumer/>
223  
224 =back
225  
226  
227 =head1 ACKNOWLEDGEMENTS
228  
229  
230 =head1 LICENSE AND COPYRIGHT
231  
232 Copyright 2011 Nic Wolff.
233  
234 This program is free software; you can redistribute it and/or modify it
235 under the terms of either: the GNU General Public License as published
236 by the Free Software Foundation; or the Artistic License.
237  
238 See http://dev.perl.org/licenses/ for more information.
239  
240  
241 =cut
242  
243 1 1; # End of Amazon::SQS::ProducerConsumer
↑ Summary Consumer.pm »