AMQP/Queue.pm

filter: Statementstmt 19.5% 26 / 133 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 24.2% 8 / 33 Podpod 90.9% 10 / 11 totaltotal 18.1% 44 / 243 CC 30 SCAR 23.3
CC
30
Cov
14%
consumer
30.0
__ANON__
20.0
__ANON__
20.0
CRAP 10.3
« AMQP/RPC/Server.pm ↑ Summary AMQP/ConnectionManager/Channel.pm »
1 package Net::Async::AMQP::Queue;
2 19 $Net::Async::AMQP::Queue::VERSION = '2.000';
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 Net::Async::AMQP::Queue - deal with queue-specific functionality
9  
10 =head1 VERSION
11  
12 version 2.000
13  
14 =cut
15  
16 19 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 19 ▶use Class::ISA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 19 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use Variable::Disposition qw(retain_future);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 19 ▶use Net::Async::AMQP;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head1 METHODS
25  
26 =cut
27  
28 =head2 listen
29  
30 Starts a consumer on this queue.
31  
32 $q->listen(
33 channel => $ch,
34 ack => 1
35 )->then(sub {
36 my ($q, $ctag) = @_;
37 print "Queue $q has ctag $ctag\n";
38 ...
39 })
40  
41 Expects the following named parameters:
42  
43 =over 4
44  
45 =item * channel - which channel to listen on
46  
47 =item * ack (optional) - true to enable ACKs
48  
49 =item * consumer_tag (optional) - specific consumer tag
50  
51 =back
52  
53 Returns a L<Future> which resolves with ($queue, $consumer_tag) on
54 completion. If this is cancelled before we receive the Basic.ConsumeOk
55 acknowledgement from the server, we'll issue an explicit cancel.
56  
57 =cut
58  
59 0 ▶sub listen {
Subroutine listen: not called CC=2 SCAR=17.9
Pod documented
60 0 my $self = shift;
61 0 my %args = @_;
62  
63 0 ▶ my $ch = delete $args{channel} or die "No channel provided";
Branch: 1 branch
BranchTrueFalseTotal
my $ch = delete $args{"channel"} or die "No channel provided" 0 0 0
64 0 $self->{channel} = $ch;
65  
66 # Attempt to bind after we've successfully declared the exchange.
67 0 ▶ retain_future($self->future->then(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
68 0 my $f = $ch->loop->new_future->set_label("listen on " . $self->queue_name);
69 0 $ch->debug_printf("Attempting to listen for events on queue [%s]", $self->queue_name);
70  
71 my $frame = Net::AMQP::Protocol::Basic::Consume->new(
72 queue => Net::AMQP::Value::String->new($self->queue_name),
73 consumer_tag => (exists $args{consumer_tag} ? Net::AMQP::Value::String->new($args{consumer_tag}) : ''),
74 no_local => 0,
75 0 ▶ no_ack => ($args{ack} ? 0 : 1),
Branch: 2 branches
BranchTrueFalseTotal
exists $args{'consumer_tag'} ? : 0 0 0
$args{'ack'} ? : 0 0 0
76 exclusive => 0,
77 ticket => 0,
78 nowait => 0,
79 );
80 $ch->push_pending(
81 0 ▶ 'Basic::ConsumeOk' => (sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
82 0 my ($amqp, $frame) = @_;
83 0 my $ctag = $frame->method_frame->consumer_tag;
84 0 $ch->bus->invoke_event(
85 listener_start => $ctag
86 );
87 0 ▶ $f->done($self => $ctag) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
88  
89 # If we were cancelled before we received the OK response,
90 # that's mildly awkward - we need to cancel the consumer,
91 # note that messages may be delivered in the interim.
92 0 ▶ if($f->is_cancelled) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_cancelled) 0 0 0
93 retain_future(
94 $self->cancel(
95 consumer_tag => $ctag
96 0 ▶ )->on_fail(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 # We should report this, but where to?
98 0 $ch->debug_printf("Failed to cancel listener %s", $ctag);
99 0 })->set_label(
100 "Cancel $ctag"
101 )
102 )
103 }
104 })
105 0 );
106 0 $ch->closure_protection($f);
107 0 $ch->send_frame($frame);
108 0 $f;
109 0 }));
110 }
111  
112 =head2 cancel
113  
114 Cancels the given consumer.
115  
116 $q->cancel(
117 consumer_tag => '...',
118 )->then(sub {
119 my ($q, $ctag) = @_;
120 print "Queue $q ctag $ctag cancelled\n";
121 ...
122 })
123  
124 Expects the following named parameters:
125  
126 =over 4
127  
128 =item * consumer_tag (optional) - specific consumer tag
129  
130 =back
131  
132 Returns a L<Future> which resolves with ($queue, $consumer_tag) on
133 completion.
134  
135 =cut
136  
137 0 ▶sub cancel {
Subroutine cancel: not called CC=3 SCAR=24.8
Pod documented
138 0 my $self = shift;
139 0 my %args = @_;
140 0 ▶ my $ch = delete $self->{channel} or die "No channel";
Branch: 1 branch
BranchTrueFalseTotal
my $ch = delete $self->{"channel"} or die "No channel" 0 0 0
141 0 ▶ my $ctag = delete $args{consumer_tag} or die "No ctag";
Branch: 1 branch
BranchTrueFalseTotal
my $ctag = delete $args{"consumer_tag"} or die "No ctag" 0 0 0
142  
143 # Attempt to bind after we've successfully declared the exchange.
144 0 ▶ retain_future($self->future->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
145 0 my $f = $ch->loop->new_future->set_label("cancel ctag " . $ctag);
146 0 $ch->debug_printf("Attempting to cancel consumer [%s]", $ctag);
147  
148 0 my $frame = Net::AMQP::Protocol::Basic::Cancel->new(
149 consumer_tag => Net::AMQP::Value::String->new($ctag),
150 nowait => 0,
151 );
152 $ch->push_pending(
153 0 ▶ 'Basic::CancelOk' => (sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
154 0 my ($amqp, $frame) = @_;
155 0 my $ctag = $frame->method_frame->consumer_tag;
156 0 $ch->bus->invoke_event(
157 listener_stop => $ctag
158 );
159 0 ▶ $f->done($self => $ctag) unless $f->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_cancelled 0 0 0
160 })
161 0 );
162 0 $ch->closure_protection($f);
163 0 $ch->send_frame($frame);
164 0 $f;
165 0 }));
166 }
167  
168 =head2 consumer
169  
170 Similar to L</listen>, but applies the event handlers so you can just provide an C<on_message> callback.
171  
172 Takes the following extra named parameters:
173  
174 =over 4
175  
176 =item * on_message - callback for message handling
177  
178 =item * on_cancel - will be called if the consumer is cancelled (either by the server or client)
179  
180 =back
181  
182 For server consumer cancellation notification, you'll need consumer_cancel_notifications:
183  
184 $mq->connect(
185 ...
186 client_properties => {
187 capabilities => {
188 'consumer_cancel_notify' => Net::AMQP::Value->true
189 },
190 },
191 )
192  
193 The on_message callback receives the following named parameters:
194  
195 =over 4
196  
197 =item * type
198  
199 =item * payload - scalar containing the raw binary data for this message
200  
201 =item * consumer_tag - which consumer tag received this message
202  
203 =item * delivery_tag - the delivery information for L<Net::Async::AMQP::Channel/ack>
204  
205 =item * routing_key - routing key used for this message
206  
207 =item * properties - any properties for the message
208  
209 =item * headers - custom headers
210  
211 =back
212  
213 Properties include:
214  
215 =over 4
216  
217 =item * correlation_id - user-specified ID that can be used to link related messages, see
218 L<Net::Async::RPC::Client> for details.
219  
220 =item * reply_to - user-specified target queue to which any replies should be sent
221  
222 =item * content_type - payload format
223  
224 =item * content_encoding - any encoding applied to the payload (gzip etc.)
225  
226 =item * delivery_mode - delivery persistence - 1 for default, 2 for permanent
227  
228 =item * priority - message priority, ranges from 0..255
229  
230 =item * expiration - when this message (would have) expired
231  
232 =item * message_id - user-specified ID
233  
234 =item * timestamp - when the message was published, usually the time in seconds
235  
236 =item * user_id - custom user-id info
237  
238 =item * app_id - user-specified application information
239  
240 =back
241  
242 See C<examples/alternative-consumer.pl> for a usage example.
243  
244 =cut
245  
246 0 ▶sub consumer {
Subroutine consumer: not called CC=5 SCAR=34.0
Pod documented
247 0 my ($self, %args) = @_;
248  
249 0 ▶ my $ch = (delete $args{channel}) // die "No channel";
Condition0%0/2
delete $args{"channel"} // die("No channel")
l!l
00
MC/DC0%0/1
delete $args{"channel"} // die("No channel")
delete $args{"channel"}
Truth table0%0/2
A
A= delete $args{"channel"}
Aresult
11
00
250 0 ▶ my $ctag = (delete $args{consumer_tag}) // '';
Condition0%0/2
delete $args{"consumer_tag"} // ""
l!l
00
MC/DC0%0/1
delete $args{"consumer_tag"} // ""
delete $args{"consumer_tag"}
Truth table0%0/2
A
A= delete $args{"consumer_tag"}
Aresult
11
00
251 0 ▶ my $on_message = delete $args{on_message} || sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
delete $args{'on_message'} || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{'on_message'} || sub { }
delete $args{'on_message'} sub { }
Truth table0%0/3
A || B
A= delete $args{'on_message'}B= sub { }
ABresult
1X1
011
000
252 0 ▶ my $on_cancel = delete $args{on_cancel} || sub { };
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
delete $args{'on_cancel'} || sub { }
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $args{'on_cancel'} || sub { }
delete $args{'on_cancel'} sub { }
Truth table0%0/3
A || B
A= delete $args{'on_cancel'}B= sub { }
ABresult
1X1
011
000
253 0 my @ev;
254 $ch->bus->subscribe_to_event(
255 @ev = (
256 # Deliver any matching messages to our callback
257 0 ▶ message => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
258 0 my ($ev, $type, $payload, $incoming_ctag, $dtag, $rkey, $headers, $properties) = @_;
259 0 ▶ return unless $incoming_ctag eq $ctag;
Branch: 1 branch
BranchTrueFalseTotal
unless $incoming_ctag eq $ctag 0 0 0
260 0 $on_message->(
261 type => $type,
262 payload => $payload,
263 consumer_tag => $ctag,
264 delivery_tag => $dtag,
265 routing_key => $rkey,
266 headers => $headers,
267 properties => $properties,
268 );
269 },
270 # Drop event handlers and call cancellation callback on cancel
271 0 ▶ cancel => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
272 0 my ($ev, %args) = @_;
273 0 ▶ return unless $args{ctag} eq $ctag;
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"ctag"} eq $ctag 0 0 0
274 # Avoid potential race between ->cancel and the consumer future being cancelled
275 0 eval {
276 0 $ch->bus->unsubscribe_from_events(@ev);
277 };
278 0 $on_cancel->(
279 consumer_tag => $ctag
280 );
281 }
282 )
283 0 );
284 $self->listen(
285 %args,
286 channel => $ch,
287 consumer_tag => $ctag,
288 0 ▶ )->on_done(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
289 0 $ctag = $_[1];
290 0 ▶ })->on_ready(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
291 0 ▶ return if shift->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if (shift())->is_done 0 0 0
292 0 eval {
293 0 $ch->bus->unsubscribe_from_events(@ev);
294 };
295 0 $on_cancel->(
296 consumer_tag => $ctag
297 )
298 0 });
299 }
300  
301 =head2 bind_exchange
302  
303 Binds this queue to an exchange.
304  
305 $q->bind_exchange(
306 channel => $ch,
307 exchange => '',
308 )->then(sub {
309 my ($q) = @_;
310 print "Queue $q bound to default exchange\n";
311 ...
312 })
313  
314 Expects the following named parameters:
315  
316 =over 4
317  
318 =item * channel - which channel to perform the bind on
319  
320 =item * exchange - the exchange to bind, can be '' for default
321  
322 =item * routing_key (optional) - a routing key for the binding
323  
324 =back
325  
326 Returns a L<Future> which resolves with ($queue) on
327 completion.
328  
329 =cut
330  
331 0 ▶sub bind_exchange {
Subroutine bind_exchange: not called CC=3 SCAR=24.8
Pod documented
332 0 my $self = shift;
333 0 my %args = @_;
334 0 ▶ die "No exchange specified" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
335 0 ▶ my $ch = delete $args{channel} or die "No channel provided";
Branch: 1 branch
BranchTrueFalseTotal
my $ch = delete $args{"channel"} or die "No channel provided" 0 0 0
336  
337 # Attempt to bind after we've successfully declared the exchange.
338 0 ▶ retain_future($self->future->then(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
339 0 ▶ my $f = $ch->loop->new_future->set_label(sprintf "bind exchange [%s] to exchange [%s] with rkey [%s]", $self->queue_name, $args{exchange}, $args{routing_key} // '(none)');
Condition0%0/2
$args{"routing_key"} // "(none)"
l!l
00
MC/DC0%0/1
$args{"routing_key"} // "(none)"
$args{"routing_key"}
Truth table0%0/2
A
A= $args{"routing_key"}
Aresult
11
00
340 0 ▶ $ch->debug_printf("Binding queue [%s] to exchange [%s] with rkey [%s]", $self->queue_name, $args{exchange}, $args{routing_key} // '(none)');
Condition0%0/2
$args{"routing_key"} // "(none)"
l!l
00
MC/DC0%0/1
$args{"routing_key"} // "(none)"
$args{"routing_key"}
Truth table0%0/2
A
A= $args{"routing_key"}
Aresult
11
00
341  
342 my $frame = Net::AMQP::Frame::Method->new(
343 method_frame => Net::AMQP::Protocol::Queue::Bind->new(
344 queue => Net::AMQP::Value::String->new($self->queue_name),
345 exchange => Net::AMQP::Value::String->new($args{exchange}),
346 0 ▶ (exists($args{routing_key}) ? ('routing_key' => Net::AMQP::Value::String->new($args{routing_key})) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'routing_key'} ? : 0 0 0
347 ticket => 0,
348 nowait => 0,
349 )
350 );
351 0 $ch->push_pending(
352 'Queue::BindOk' => [ $f, $self ],
353 );
354 0 $ch->closure_protection($f);
355 0 $ch->send_frame($frame);
356 0 $f
357 0 }));
358 }
359  
360 =head2 unbind_exchange
361  
362 Unbinds this queue from an exchange.
363  
364 $q->unbind_exchange(
365 channel => $ch,
366 exchange => '',
367 )->then(sub {
368 my ($q) = @_;
369 print "Queue $q unbound from default exchange\n";
370 ...
371 })
372  
373 Expects the following named parameters:
374  
375 =over 4
376  
377 =item * channel - which channel to perform the bind on
378  
379 =item * exchange - the exchange to bind, can be '' for default
380  
381 =item * routing_key (optional) - a routing key for the binding
382  
383 =back
384  
385 Returns a L<Future> which resolves with ($queue) on
386 completion.
387  
388 =cut
389  
390 0 ▶sub unbind_exchange {
Subroutine unbind_exchange: not called CC=3 SCAR=24.8
Pod documented
391 0 my $self = shift;
392 0 my %args = @_;
393 0 ▶ die "No exchange specified" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
394 0 ▶ my $ch = delete $args{channel} or die "No channel provided";
Branch: 1 branch
BranchTrueFalseTotal
my $ch = delete $args{"channel"} or die "No channel provided" 0 0 0
395  
396 # Attempt to unbind after we've successfully declared the exchange.
397 0 ▶ retain_future($self->future->then(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
398 0 ▶ my $f = $ch->loop->new_future->set_label("unbind exchange [%s] from exchange [%s] with rkey [%s]", $self->queue_name, $args{exchange}, $args{routing_key} // '(none)');
Condition0%0/2
$args{"routing_key"} // "(none)"
l!l
00
MC/DC0%0/1
$args{"routing_key"} // "(none)"
$args{"routing_key"}
Truth table0%0/2
A
A= $args{"routing_key"}
Aresult
11
00
399 0 ▶ $ch->debug_printf("Unbinding queue [%s] from exchange [%s] with rkey [%s]", $self->queue_name, $args{exchange}, $args{routing_key} // '(none)');
Condition0%0/2
$args{"routing_key"} // "(none)"
l!l
00
MC/DC0%0/1
$args{"routing_key"} // "(none)"
$args{"routing_key"}
Truth table0%0/2
A
A= $args{"routing_key"}
Aresult
11
00
400  
401 my $frame = Net::AMQP::Frame::Method->new(
402 method_frame => Net::AMQP::Protocol::Queue::Unbind->new(
403 queue => Net::AMQP::Value::String->new($self->queue_name),
404 exchange => Net::AMQP::Value::String->new($args{exchange}),
405 0 ▶ (exists($args{routing_key}) ? ('routing_key' => Net::AMQP::Value::String->new($args{routing_key})) : ()),
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'routing_key'} ? : 0 0 0
406 ticket => 0,
407 nowait => 0,
408 )
409 );
410 0 $ch->push_pending(
411 'Queue::UnbindOk' => [ $f, $self ],
412 );
413 0 $ch->closure_protection($f);
414 0 $ch->send_frame($frame);
415 0 $f
416 0 }));
417 }
418  
419 =head2 delete
420  
421 Deletes this queue.
422  
423 $q->delete(
424 channel => $ch,
425 )->then(sub {
426 my ($q) = @_;
427 print "Queue $q deleted\n";
428 ...
429 })
430  
431 Expects the following named parameters:
432  
433 =over 4
434  
435 =item * channel - which channel to perform the bind on
436  
437 =back
438  
439 Returns a L<Future> which resolves with ($queue) on
440 completion.
441  
442 =cut
443  
444 0 ▶sub delete : method {
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
445 0 my $self = shift;
446 0 my %args = @_;
447 0 ▶ my $ch = delete $args{channel} or die "No channel provided";
Branch: 1 branch
BranchTrueFalseTotal
my $ch = delete $args{"channel"} or die "No channel provided" 0 0 0
448  
449 0 ▶ retain_future($self->future->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
450 0 my $f = $ch->loop->new_future->set_label("delete " . $self->queue_name);
451 0 $ch->debug_printf("Deleting queue [%s]", $self->queue_name);
452  
453 0 my $frame = Net::AMQP::Frame::Method->new(
454 method_frame => Net::AMQP::Protocol::Queue::Delete->new(
455 queue => Net::AMQP::Value::String->new($self->queue_name),
456 nowait => 0,
457 )
458 );
459 0 $ch->push_pending(
460 'Queue::DeleteOk' => [ $f, $self ],
461 );
462 0 $ch->closure_protection($f);
463 0 $ch->send_frame($frame);
464 0 $f
465 0 }));
466 }
467  
468 =head1 ACCESSORS
469  
470 These are mostly intended for internal use only.
471  
472 =cut
473  
474 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
475 0 my ($class, %args) = @_;
476 0 my $self = bless {}, $class;
477 0 $self->configure(%args);
478 0 $self
479 }
480  
481 =head2 configure
482  
483 Applies C<amqp> or C<future> value.
484  
485 =cut
486  
487 0 ▶sub configure {
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
488 0 my ($self, %args) = @_;
489 0 for(grep exists $args{$_}, qw(amqp)) {
490 0 Scalar::Util::weaken($self->{$_} = delete $args{$_})
491 }
492 0 for(grep exists $args{$_}, qw(future channel)) {
493 0 $self->{$_} = delete $args{$_};
494 }
495 $self
496 0 }
497  
498 =head2 amqp
499  
500 A weakref to the L<Net::Async::AMQP> instance.
501  
502 =cut
503  
504 0 ▶sub amqp { shift->{amqp} }
Subroutine amqp: not called CC=1 SCAR=6.9
Pod documented
505  
506 =head2 future
507  
508 A ref to the L<Future> representing the queue readiness.
509  
510 =cut
511  
512 0 ▶sub future { shift->{future} }
Subroutine future: not called CC=1 SCAR=6.9
Pod documented
513  
514 =head2 queue_name
515  
516 Sets or returns the queue name.
517  
518 =cut
519  
520 0 ▶sub queue_name {
Subroutine queue_name: not called CC=2 SCAR=17.9
Pod documented
521 0 my $self = shift;
522 0 ▶ return $self->{queue_name} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
523 0 $self->{queue_name} = shift;
524 0 $self
525 }
526  
527 19 1;
528  
529 __END__
« AMQP/RPC/Server.pm ↑ Summary AMQP/ConnectionManager/Channel.pm »