Channel.pm

filter: Statementstmt 50.0% 52 / 104 Branchbran 25.0% 8 / 32 Conditioncond 23.0% 3 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 57.1% 8 / 14 Podpod 100% 5 / 5 totaltotal 43.4% 76 / 175 CC 23 SCAR 27.4
CC
23
Cov
42%
_do
42.0
publish
30.0
_confirmed_connect
12.7
CRAP 15.4
« Exchange.pm ↑ Summary Connection.pm »
1 package Net::RabbitMQ::Channel;
2  
3 1 ▶use Class::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Net::RabbitMQ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶unless ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "MSWin32") 1 0 1
8 1 ▶ use Sys::SigAction;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 }
10  
11 1 ▶use Net::RabbitMQ::Exchange;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Net::RabbitMQ::Queue;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = '0.05';
15  
16 # has 'mq';
17  
18 1 has exchange_pack => 'Net::RabbitMQ::Exchange';
19 1 has queue_pack => 'Net::RabbitMQ::Queue';
20  
21 1 has 'number';
22  
23 1 our @NO_CHANNEL_CALLS = ('recv');
24  
25 1 ▶sub new {
Subroutine new: called CC=3 SCAR=12.3
Pod documented
26 1 my $class = shift;
27 1 my $number = shift;
28 1 my $config = {@_};
29
30 1 $config->{mq} = Net::RabbitMQ->new;
31 1 $config->{number} = $number;
32
33 1 ▶ $config->{reconnect_timeout} ||= 10;
Condition50%1/2
$config->{"reconnect_timeout"} ||= 10
l!l
01
MC/DC0%0/1
$config->{"reconnect_timeout"} ||= 10
$config->{"reconnect_timeout"}
Truth table50%1/2
A
A= $config->{"reconnect_timeout"}
Aresult
11
00
34
35 1 my $self = bless $config, $class;
36  
37 1 ▶ if ($self->_confirmed_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_confirmed_connect) 0 0 0
38 0 return $self;
39 }
40
41 0 die "can't open connection";
42 # if channel didn't open, then we died before this string
43 }
44  
45 0 ▶sub exchange_declare {
Subroutine exchange_declare: not called CC=2 SCAR=17.9
Pod documented
46 0 my $self = shift;
47 0 my $name = shift;
48 0 my $args = {@_};
49
50 $args->{package} = $self->exchange_pack
51 0 ▶ unless exists $args->{package};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"package"} 0 0 0
52
53 0 $args->{package}->new ($self, $name, %$args);
54 }
55  
56 0 ▶sub queue_declare {
Subroutine queue_declare: not called CC=2 SCAR=17.9
Pod documented
57 0 my $self = shift;
58 0 my $name = shift;
59 0 my $args = {@_};
60
61 $args->{package} = $self->queue_pack
62 0 ▶ unless exists $args->{package};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"package"} 0 0 0
63
64 0 return $args->{package}->new ($self, $name, %$args);
65 }
66  
67 28 ▶sub _failed_host_sort_sub {
Subroutine _failed_host_sort_sub: called CC=3 SCAR=11.0
68 28 my ($hosts, $sort_a, $sort_b) = @_;
69 return !exists $hosts->{$sort_a}->{failed}
70 ? -1
71 : !exists $hosts->{$sort_b}->{failed}
72 ? 1
73 : $hosts->{$sort_b}->{failed} <=> $hosts->{$sort_a}->{failed}
74 28 ▶}
Branch: 2 branches
BranchTrueFalseTotal
!exists $hosts->{$sort_a}{'failed'} ? : 10 18 28
!exists $hosts->{$sort_b}{'failed'} ? : 8 10 18
75  
76  
77 # here we try to connect, if all servers fails, then die
78 1 ▶sub _confirmed_connect {
Subroutine _confirmed_connect: called CC=8 SCAR=25.4
79 1 my $self = shift;
80 1 my $is_reconnect = shift;
81
82 1 my $mq = $self->{mq};
83
84 1 local $@;
85
86 1 my $success = 0;
87
88 1 my $hosts = $self->{hosts};
89
90 0 foreach my $host (sort {_failed_host_sort_sub ($hosts, $a, $b)} keys %$hosts
91 ) {
92 1 my $last_failed = $hosts->{$host}->{failed};
93 1 ▶ if (defined $last_failed and (time - $last_failed < $self->{reconnect_timeout})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_failed and time - $last_failed < $self->{"reconnect_timeout"}) 0 1 1
Condition33%1/3
defined $last_failed and time - $last_failed < $self->{"reconnect_timeout"}
!ll&&!rl&&r
100
MC/DC0%0/2
defined $last_failed and time - $last_failed < $self->{"reconnect_timeout"}
defined $last_failed time - $last_failed < $self->{"reconnect_timeout"}
Truth table33%1/3
A and B
A= defined $last_failedB= time - $last_failed < $self->{"reconnect_timeout"}
ABresult
0X0
100
111
94 0 die "no hosts can be reached within $self->{reconnect_timeout} sec. you must check hosts availability or enlarge your interval";
95 }
96
97 1 eval {
98 1 ▶ if ($^O ne 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O ne "MSWin32") 1 0 1
99 # warn "setting alarm";
100 0 ▶ my $sig_handler = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 # failed disconnect is safe solution
102 # warn "alarm called";
103 0 $self->{hosts}->{$host}->{failed} = time;
104 0 $success = 0;
105 0 die "timeout";
106 1 };
107 1 my $h = Sys::SigAction::set_sig_handler ('ALRM', $sig_handler);
108 1 $h->{ACT}->{HANDLER} = $sig_handler;
109 #use Data::Dumper;
110 #warn Dumper $h;
111 #$SIG{'ALRM'} = sub {die;};
112 1 ▶ alarm ($self->{timeout} || 10);
Condition50%1/2
$self->{'timeout'} || 10
l!l
01
MC/DC0%0/1
$self->{'timeout'} || 10
$self->{'timeout'}
Truth table50%1/2
A
A= $self->{'timeout'}
Aresult
11
00
113 }
114  
115 #warn "before connect";
116  
117 1 $mq->connect ($host, $self->{hosts}->{$host});
118 0 ▶ $self->_do ('channel_open')
Branch: 1 branch
BranchTrueFalseTotal
unless $is_reconnect 0 0 0
119 unless $is_reconnect;
120 0 $success = 1;
121
122 #warn "after connect";
123  
124 0 alarm 0;
125 };
126
127 #warn "after eval, before alarm";
128  
129 1 alarm 0;
130 #warn "after eval";
131  
132 1 ▶ if ($success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($success) { } 0 1 1
133 0 delete $hosts->{$host}->{failed};
134 0 return 1
135 } else {
136 1 $self->{hosts}->{$host}->{failed} = time;
137 }
138 }
139
140 1 die "we can't connect to any provided server: $@";
141 }
142  
143 0 ▶sub _do {
Subroutine _do: not called CC=6 SCAR=37.4
144 0 my $self = shift;
145 0 my $cmd = shift;
146
147 0 my $verify = "_verify_$cmd";
148
149 # parameter verification
150 0 ▶ if ($self->can ($verify)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($verify)) 0 0 0
151 0 ▶ return unless $self->$verify (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$verify(@_) 0 0 0
152 }
153
154 0 local $@;
155
156 0 my $result;
157 0 my $success = 0;
158
159 # warn "cmd: $cmd, num: ", $self->number, ", array: @_";
160
161 # use warnings FATAL => qw(uninitialized);
162  
163 0 my @params = @_;
164
165 unshift @params, $self->number
166 0 ▶ unless scalar grep {$_ eq $cmd} @NO_CHANNEL_CALLS;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar grep({$_ eq $cmd;} @Net::RabbitMQ::Channel::NO_CHANNEL_CALLS) 0 0 0
167
168 # real server work -> we must restart connection after failure
169 0 eval {
170 # warn "$cmd ", $self->number;
171 0 $result = $self->{mq}->$cmd (@params);
172 0 $success = 1;
173 };
174
175 #no warnings qw(uninitialized);
176 #debug "command: $cmd, result: $result, success: $success";
177 #use warnings qw(uninitialized);
178
179 # TODO: check for real connection error, we don't want to run erratical command another time
180 0 ▶ unless ($success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($success) 0 0 0
181
182 # warn $@;
183
184 0 $self->_confirmed_connect (1); # send flag for reconnect
185 0 $self->{mq}->channel_open ($self->number);
186
187 # if we have more than one failure after successful
188 # reconnect, then we must die
189 0 $result = $self->{mq}->$cmd (@params);
190 0 $success = 1;
191 }
192
193 0 ▶ return wantarray ? ($success, $result) : $success;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
194 }
195  
196 0 ▶sub publish {
Subroutine publish: not called CC=5 SCAR=34.0
Pod documented
197 0 my $self = shift;
198 0 my $routing_key = shift;
199 0 my $body = shift;
200 0 my $props = {@_};
201
202 0 my $opts = {};
203
204 0 foreach my $k (keys %$props) {
205 0 ▶ if ($k eq 'exchange' or $k eq 'mandatory' or $k eq 'immediate') {
Branch: 1 branch
BranchTrueFalseTotal
if ($k eq "exchange" or $k eq "mandatory" or $k eq "immediate") 0 0 0
Condition0%0/6
$k eq "exchange" or $k eq "mandatory" or $k eq "immediate"
l!l&&r!l&&!r
000
$k eq "exchange" or $k eq "mandatory"
l!l&&r!l&&!r
000
MC/DC0%0/3
$k eq "exchange" or $k eq "mandatory" or $k eq "immediate"
$k eq "exchange" $k eq "mandatory" $k eq "immediate"
Truth table0%0/4
A or B or C
A= $k eq "exchange"B= $k eq "mandatory"C= $k eq "immediate"
ABCresult
1XX1
01X1
0011
0000
206 0 $opts->{$k} = delete $props->{$k};
207 }
208 }
209
210 0 my ($success, $result) = $self->_do ('publish', $routing_key, $body, $opts, $props);
211 }
212  
213 0 ▶sub close {
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
214 0 my $self = shift;
215
216
217 }
218  
219 1 1;
220  
221 =head1 NAME
222  
223 Net::RabbitMQ::Channel - use rabbitmq, OOP style
224  
225 =head1 SYNOPSIS
226  
227 use Net::RabbitMQ::Channel;
228  
229 my $channel = Net::RabbitMQ::Channel->new (1, {
230 hosts => {
231 rabbit1 => {user => 'guest', pass => 'guest'},
232 rabbit2 => {user => 'guest', pass => 'guest'}
233 }
234 });
235  
236 my $exchange = $channel->exchange_declare (
237 'test.x',
238 exchange_type => "topic",
239 );
240  
241 my $publisher_key = 'test.*';
242  
243 # consumer part
244 my $queue = $channel->queue_declare (
245 'test.q',
246 exclusive => 0,
247 );
248  
249 $queue->bind ($exchange, $publisher_key);
250  
251 # publisher part
252 $exchange->publish ($publisher_key, $message,
253 app_id => 'test',
254 timestamp => time
255 );
256
257 # consumer part
258 my $message = $queue->get;
259  
260 =head1 METHODS
261  
262 =head2 init
263  
264 =over 4
265  
266 =item new
267  
268 my $channel = Net::RabbitMQ::Channel->new (1, {
269 # mandatory
270 hosts => {host_name => {user => 'user_name', pass => 'password'}},
271 # optional
272 });
273  
274  
275 when creating Net::RabbitMQ::Channel you must provide
276 channel number and configuration options.
277  
278 in the current version only 'hosts' key is supported. each key for 'hosts' specifies
279 one rabbitmq broker configuration. if current broker connection fails, module tries
280 to reconnect to another one.
281  
282  
283 =cut
284  
285 =back
286  
287 =head2 working with channel
288  
289 =over 4
290  
291 =item exchange_declare
292  
293 declares exchange
294  
295 my $exchange = $self->exchange_declare (
296 'test.exchange',
297 package => 'My::Exchange', # Net::RabbitMQ::Exchange if omitted
298 passive => 0, # 0
299 durable => 1, # 1
300 auto_delete => 0, # 0
301 exchange_type => "topic"
302 );
303  
304  
305 =item queue_declare
306  
307 declares queue
308  
309  
310 my $queue = $self->queue_declare (
311 'test.queue',
312 package => 'My::Queue', # Net::RabbitMQ::Queue if omitted
313 passive => 0, # 0
314 durable => 1, # 1
315 auto_delete => 0, # 0
316 exclusive => 0
317 );
318  
319  
320 =item publish
321  
322 publish message to routing key
323  
324 a typical workflow for a producer role is: open channel, declare exchange,
325 and publish message via routing key
326  
327 please, note: queue for recieving that message must be declared
328 and binded to exchange using routing key prior to message publishing.
329  
330 $channel->publish ($publisher_key, $message, {exchange => $exchange->name}, {
331 # content_type => $string,
332 # content_encoding => $string,
333 # correlation_id => $string,
334 # reply_to => $string,
335 # expiration => $string,
336 # message_id => $string,
337 # type => $string,
338 # user_id => $string,
339 app_id => 'test',
340 # delivery_mode => $integer,
341 # priority => $integer,
342 timestamp => time
343  
344 });
345
346  
347 =item close
348  
349 stub
350  
351 =cut
352  
353 =back
354  
355 =head1 AUTHOR
356  
357 Ivan Baktsheev, C<< <apla at the-singlers.us> >>
358  
359 =head1 BUGS
360  
361 Please report any bugs or feature requests to my email address,
362 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net::RabbitMQ::Channel>.
363 I will be notified, and then you'll automatically be notified
364 of progress on your bug as I make changes.
365  
366 =head1 SUPPORT
367  
368  
369  
370 =head1 ACKNOWLEDGEMENTS
371  
372  
373  
374 =head1 COPYRIGHT & LICENSE
375  
376 Copyright 2010-2011 Ivan Baktsheev
377  
378 This program is free software; you can redistribute it and/or modify it
379 under the same terms as Perl itself.
380  
381  
382 =cut
« Exchange.pm ↑ Summary Connection.pm »