AMQP/Queue.pm

filter: Statementstmt 23.2% 26 / 112 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 30.7% 8 / 26 Podpod 90.0% 9 / 10 totaltotal 23.1% 43 / 186 CC 21 SCAR 20.5
CC
21
Cov
18%
__ANON__
12.0
__ANON__
12.0
cancel
12.0
CRAP 7.7
« AMQP/ConnectionManager.pm ↑ Summary AMQP.pm »
1 package Net::Async::AMQP::Queue;
2 16 $Net::Async::AMQP::Queue::VERSION = '0.031';
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶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 0.031
13  
14 =cut
15  
16 16 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 16 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use Class::ISA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 16 ▶use Variable::Disposition qw(retain_future);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 16 ▶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 sub listen {
60 0 ▶ my $self = shift;
Subroutine listen: not called CC=2 SCAR=17.9
Pod documented
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 retain_future($self->future->then(sub {
68 0 ▶ my $f = $ch->loop->new_future;
Subroutine __ANON__: not called CC=3 SCAR=24.8
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 'Basic::ConsumeOk' => (sub {
82 0 ▶ my ($amqp, $frame) = @_;
Subroutine __ANON__: not called CC=3 SCAR=24.8
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 )->on_fail(sub {
97 # We should report this, but where to?
98 0 ▶ $ch->debug_printf("Failed to cancel listener %s", $ctag);
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 sub cancel {
138 0 ▶ my $self = shift;
Subroutine cancel: not called CC=3 SCAR=24.8
Pod documented
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 retain_future($self->future->then(sub {
145 0 ▶ my $f = $ch->loop->new_future;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 'Basic::CancelOk' => (sub {
154 0 ▶ my ($amqp, $frame) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
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 bind_exchange
169  
170 Binds this queue to an exchange.
171  
172 $q->bind_exchange(
173 channel => $ch,
174 exchange => '',
175 )->then(sub {
176 my ($q) = @_;
177 print "Queue $q bound to default exchange\n";
178 ...
179 })
180  
181 Expects the following named parameters:
182  
183 =over 4
184  
185 =item * channel - which channel to perform the bind on
186  
187 =item * exchange - the exchange to bind, can be '' for default
188  
189 =item * routing_key (optional) - a routing key for the binding
190  
191 =back
192  
193 Returns a L<Future> which resolves with ($queue) on
194 completion.
195  
196 =cut
197  
198 sub bind_exchange {
199 0 ▶ my $self = shift;
Subroutine bind_exchange: not called CC=3 SCAR=24.8
Pod documented
200 0 my %args = @_;
201 0 ▶ die "No exchange specified" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
202 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
203  
204 # Attempt to bind after we've successfully declared the exchange.
205 retain_future($self->future->then(sub {
206 0 ▶ my $f = $ch->loop->new_future;
Subroutine __ANON__: not called CC=3 SCAR=24.8
207 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
208  
209 my $frame = Net::AMQP::Frame::Method->new(
210 method_frame => Net::AMQP::Protocol::Queue::Bind->new(
211 queue => Net::AMQP::Value::String->new($self->queue_name),
212 exchange => Net::AMQP::Value::String->new($args{exchange}),
213 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
214 ticket => 0,
215 nowait => 0,
216 )
217 );
218 0 $ch->push_pending(
219 'Queue::BindOk' => [ $f, $self ],
220 );
221 0 $ch->closure_protection($f);
222 0 $ch->send_frame($frame);
223 0 $f
224 0 }));
225 }
226  
227 =head2 unbind_exchange
228  
229 Unbinds this queue from an exchange.
230  
231 $q->unbind_exchange(
232 channel => $ch,
233 exchange => '',
234 )->then(sub {
235 my ($q) = @_;
236 print "Queue $q unbound from default exchange\n";
237 ...
238 })
239  
240 Expects the following named parameters:
241  
242 =over 4
243  
244 =item * channel - which channel to perform the bind on
245  
246 =item * exchange - the exchange to bind, can be '' for default
247  
248 =item * routing_key (optional) - a routing key for the binding
249  
250 =back
251  
252 Returns a L<Future> which resolves with ($queue) on
253 completion.
254  
255 =cut
256  
257 sub unbind_exchange {
258 0 ▶ my $self = shift;
Subroutine unbind_exchange: not called CC=3 SCAR=24.8
Pod documented
259 0 my %args = @_;
260 0 ▶ die "No exchange specified" unless exists $args{exchange};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"exchange"} 0 0 0
261 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
262  
263 # Attempt to unbind after we've successfully declared the exchange.
264 retain_future($self->future->then(sub {
265 0 ▶ my $f = $ch->loop->new_future;
Subroutine __ANON__: not called CC=3 SCAR=24.8
266 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
267  
268 my $frame = Net::AMQP::Frame::Method->new(
269 method_frame => Net::AMQP::Protocol::Queue::Unbind->new(
270 queue => Net::AMQP::Value::String->new($self->queue_name),
271 exchange => Net::AMQP::Value::String->new($args{exchange}),
272 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
273 ticket => 0,
274 nowait => 0,
275 )
276 );
277 0 $ch->push_pending(
278 'Queue::UnbindOk' => [ $f, $self ],
279 );
280 0 $ch->closure_protection($f);
281 0 $ch->send_frame($frame);
282 0 $f
283 0 }));
284 }
285  
286 =head2 delete
287  
288 Deletes this queue.
289  
290 $q->delete(
291 channel => $ch,
292 )->then(sub {
293 my ($q) = @_;
294 print "Queue $q deleted\n";
295 ...
296 })
297  
298 Expects the following named parameters:
299  
300 =over 4
301  
302 =item * channel - which channel to perform the bind on
303  
304 =back
305  
306 Returns a L<Future> which resolves with ($queue) on
307 completion.
308  
309 =cut
310  
311 sub delete : method {
312 0 ▶ my $self = shift;
Subroutine delete: not called CC=2 SCAR=17.9
Pod documented
313 0 my %args = @_;
314 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
315  
316 retain_future($self->future->then(sub {
317 0 ▶ my $f = $ch->loop->new_future;
Subroutine __ANON__: not called CC=1 SCAR=6.9
318 0 $ch->debug_printf("Deleting queue [%s]", $self->queue_name);
319  
320 0 my $frame = Net::AMQP::Frame::Method->new(
321 method_frame => Net::AMQP::Protocol::Queue::Delete->new(
322 queue => Net::AMQP::Value::String->new($self->queue_name),
323 nowait => 0,
324 )
325 );
326 0 $ch->push_pending(
327 'Queue::DeleteOk' => [ $f, $self ],
328 );
329 0 $ch->closure_protection($f);
330 0 $ch->send_frame($frame);
331 0 $f
332 0 }));
333 }
334  
335 =head1 ACCESSORS
336  
337 These are mostly intended for internal use only.
338  
339 =cut
340  
341 sub new {
342 0 ▶ my ($class, %args) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
343 0 my $self = bless {}, $class;
344 0 $self->configure(%args);
345 0 $self
346 }
347  
348 =head2 configure
349  
350 Applies C<amqp> or C<future> value.
351  
352 =cut
353  
354 sub configure {
355 0 ▶ my ($self, %args) = @_;
Subroutine configure: not called CC=3 SCAR=24.8
Pod documented
356 0 for(grep exists $args{$_}, qw(amqp)) {
357 0 Scalar::Util::weaken($self->{$_} = delete $args{$_})
358 }
359 0 for(grep exists $args{$_}, qw(future channel)) {
360 0 $self->{$_} = delete $args{$_};
361 }
362 $self
363 0 }
364  
365 =head2 amqp
366  
367 A weakref to the L<Net::Async::AMQP> instance.
368  
369 =cut
370  
371 0 ▶sub amqp { shift->{amqp} }
Subroutine amqp: not called CC=1 SCAR=6.9
Pod documented
372  
373 =head2 future
374  
375 A ref to the L<Future> representing the queue readiness.
376  
377 =cut
378  
379 0 ▶sub future { shift->{future} }
Subroutine future: not called CC=1 SCAR=6.9
Pod documented
380  
381 =head2 queue_name
382  
383 Sets or returns the queue name.
384  
385 =cut
386  
387 sub queue_name {
388 0 ▶ my $self = shift;
Subroutine queue_name: not called CC=2 SCAR=17.9
Pod documented
389 0 ▶ return $self->{queue_name} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
390 0 $self->{queue_name} = shift;
391 0 $self
392 }
393  
394 16 1;
395  
396 __END__
« AMQP/ConnectionManager.pm ↑ Summary AMQP.pm »