PubSub/Consumer.pm

filter: Statementstmt 30.7% 24 / 78 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 20.6% 6 / 29 Podpod 55.5% 5 / 9 totaltotal 29.1% 35 / 120 CC 3 SCAR 8.4
CC
3
Cov
29%
reject
6.0
ack
6.0
init
2.0
CRAP 2.3
« PubSub/Publisher.pm ↑ Summary
1 package AnyEvent::RabbitMQ::PubSub::Consumer;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use AnyEvent::RabbitMQ::PubSub;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Time::HiRes qw(usleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Promises qw(deferred collect);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 AnyEvent::RabbitMQ::PubSub::Consumer - rabbitmq consumer
13  
14 =cut
15  
16 1 has channel => (
17 is => 'ro', isa => 'AnyEvent::RabbitMQ::Channel', required => 1
18 );
19 1 has exchange => (
20 is => 'ro', isa => 'HashRef', required => 1
21 );
22 1 has queue => (
23 is => 'ro', isa => 'HashRef', required => 1
24 );
25 1 has routing_key => (
26 is => 'ro', isa => 'Str', default => '#'
27 );
28 1 has prefetch_count => (
29 is => 'ro', isa => 'Int', default => 5,
30 );
31  
32 =head1 METHODS
33  
34 =head2 init()
35  
36 set prefetch_count
37  
38 declare exchange and queue
39  
40 =cut
41  
42 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
43 0 my ($self) = @_;
44  
45 0 $self->channel->qos(prefetch_count => $self->prefetch_count);
46  
47 0 my $cv = AnyEvent->condvar;
48  
49 $self->declare_exchange_and_queue()
50 0 ▶ ->then( sub { $self->bind_queue() })
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 0 ▶ ->then( sub { $cv->send() })
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 0 ▶ ->catch(sub { $cv->croak(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
53  
54 0 $cv->recv();
55 return
56 0 }
57  
58 =head2 consume($cv, $on_consume)
59  
60 run consume C<$on_consume> code on channel
61  
62 return L<Promise>
63  
64 my $cv = AnyEvent->condvar();
65 $self->consume(
66 $cv,
67 sub {
68 my ($consumer, $msg) = @_;
69  
70 ...
71 }
72 )->then(sub {
73 say 'Consumer was started...';
74 });
75  
76  
77 =cut
78  
79 0 ▶sub consume {
Subroutine consume: not called CC=1 SCAR=6.9
Pod documented
80 0 my ($self, $cv, $on_consume) = @_;
81  
82 0 my $d = deferred();
83  
84 $self->channel->consume(
85 queue => $self->queue->{queue},
86 no_ack => 0,
87 0 ▶ on_success => sub { $d->resolve() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 ▶ on_cancel => sub {AnyEvent::RabbitMQ::PubSub::_report_error($cv, @_)},
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ on_failure => sub {AnyEvent::RabbitMQ::PubSub::_report_error($cv, @_)},
Subroutine __ANON__: not called CC=1 SCAR=6.9
90 0 ▶ on_consume => sub { $on_consume->($self, @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
91 0 );
92  
93 0 return $d->promise
94 }
95  
96 =head2 reject_and_republish($msg)
97  
98 reject (drop) message
99  
100 and after 10ms (to avoid 100% CPU)
101  
102 republish message back (to end of queue)
103  
104 =cut
105  
106 0 ▶sub reject_and_republish {
Subroutine reject_and_republish: not called CC=1 SCAR=6.9
Pod documented
107 0 my ($self, $msg) = @_;
108  
109 0 usleep 10_000; # wait 10 ms before republish to avoid 100 % CPU
110 0 $self->reject($msg);
111  
112 0 $msg->{header}{headers}{trials}++;
113 $self->channel->publish(
114 body => $msg->{body}->{payload},
115 header => $msg->{header},
116 exchange => "",
117 routing_key => $self->queue->{queue},
118 0 );
119 }
120  
121 =head2 reject($msg)
122  
123 reject (drop) message
124  
125 =cut
126  
127 0 ▶sub reject {
Subroutine reject: not called CC=2 SCAR=17.9
Pod documented
128 0 my ($self, $msg) = @_;
129  
130 0 ▶ warn "Message to reject not specified" if !defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
131  
132 0 my $delivery_tag = $msg->{deliver}{method_frame}{delivery_tag};
133 0 $self->channel->reject(delivery_tag => $delivery_tag);
134 }
135  
136 =head2 ack($msg)
137  
138 ack C<$msg> same as
139  
140 $consumer->channel->ack(delivery_tag => $msg->{deliver}{method_frame}{delivery_tag});
141  
142 =cut
143  
144 0 ▶sub ack {
Subroutine ack: not called CC=2 SCAR=17.9
Pod documented
145 0 my ($self, $msg) = @_;
146  
147 0 ▶ warn "Message to ack not specified" if !defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg 0 0 0
148  
149 0 my $delivery_tag = $msg->{deliver}{method_frame}{delivery_tag};
150 0 $self->channel->ack(delivery_tag => $delivery_tag);
151 }
152  
153 0 ▶sub declare_exchange_and_queue {
Subroutine declare_exchange_and_queue: not called CC=1 SCAR=6.9
Pod undocumented
154 0 my ($self, $cv) = @_;
155  
156 return collect(
157 $self->declare_exchange(),
158 $self->declare_queue(),
159 0 ▶ )->then(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
160 0 return @{ $_[0] }
161 0 });
162 }
163  
164 0 ▶sub declare_queue {
Subroutine declare_queue: not called CC=1 SCAR=6.9
Pod undocumented
165 0 my ($self) = @_;
166  
167 0 my $d = deferred;
168 $self->channel->declare_queue(
169 0 %{ $self->queue },
170 0 ▶ on_success => sub { $d->resolve() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
171 0 ▶ on_failure => sub { $d->reject(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
172 0 );
173 0 return $d->promise()
174 }
175  
176 0 ▶sub declare_exchange {
Subroutine declare_exchange: not called CC=1 SCAR=6.9
Pod undocumented
177 0 my ($self) = @_;
178  
179 0 my $d = deferred;
180 $self->channel->declare_exchange(
181 0 %{ $self->exchange },
182 0 ▶ on_success => sub { $d->resolve() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
183 0 ▶ on_failure => sub { $d->reject(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
184 0 );
185 0 return $d->promise()
186 }
187  
188 0 ▶sub bind_queue {
Subroutine bind_queue: not called CC=1 SCAR=6.9
Pod undocumented
189 0 my ($self) = @_;
190  
191 0 my $d = deferred;
192 $self->channel->bind_queue(
193 queue => $self->queue->{queue},
194 exchange => $self->exchange->{exchange},
195 routing_key => $self->routing_key,
196 0 ▶ on_success => sub { $d->resolve() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
197 0 ▶ on_failure => sub { $d->reject(@_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
198 0 );
199 0 return $d->promise()
200 }
201  
202 1 1
« PubSub/Publisher.pm ↑ Summary