Client/Consumer.pm

filter: Statementstmt 27.0% 20 / 74 Branchbran 0.0% 0 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 22.2% 4 / 18 Podpod 0.0% 0 / 2 totaltotal 24.2% 25 / 103 CC 5 SCAR 12.7
CC
5
Cov
26%
consume_p
12.0
close
6.0
BEGIN
2.0
CRAP 3.6
« Client/Publisher.pm ↑ Summary Client/Method.pm »
1 package Mojo::RabbitMQ::Client::Consumer;
2 5 ▶use Mojo::Base 'Mojo::EventEmitter';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use Mojo::Promise;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 require Mojo::RabbitMQ::Client;
7  
8 5 ▶use constant DEBUG => $ENV{MOJO_RABBITMQ_DEBUG} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"MOJO_RABBITMQ_DEBUG"} // 0
l!l
05
MC/DC0%0/1
$ENV{"MOJO_RABBITMQ_DEBUG"} // 0
$ENV{"MOJO_RABBITMQ_DEBUG"}
Truth table50%1/2
A
A= $ENV{"MOJO_RABBITMQ_DEBUG"}
Aresult
11
00
9  
10 5 has url => undef;
11 5 has client => undef;
12 5 has channel => undef;
13 5 has queue => undef;
14 5 has setup => 0;
15 0 ▶has defaults => sub { {} };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16  
17 0 ▶sub consume_p {
Subroutine consume_p: not called CC=3 SCAR=24.8
Pod undocumented
18 0 my $self = shift;
19  
20 0 my $promise = Mojo::Promise->new()->resolve();
21  
22 0 weaken $self;
23 0 ▶ unless ($self->client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->client) 0 0 0
24 $promise = $promise->then(
25 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
26 0 warn "-- spawn new client\n" if DEBUG;
27 0 my $client_promise = Mojo::Promise->new();
28 0 my $client = Mojo::RabbitMQ::Client->new(url => $self->url);
29 0 $self->client($client);
30  
31 # Catch all client related errors
32 0 ▶ $self->client->catch(sub { $client_promise->reject($_[1]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
33  
34 # When connection is in Open state, open new channel
35 $client->on(
36 0 ▶ open => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
37 0 warn "-- client open\n" if DEBUG;
38 0 $client_promise->resolve;
39 }
40 0 );
41 0 ▶ $client->on('close' => sub { shift; $self->emit('close', @_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
42  
43 # Start connection
44 0 $client->connect;
45  
46 0 return $client_promise;
47 }
48 0 );
49 }
50  
51 # Create a new channel with auto-assigned id
52 0 ▶ unless ($self->channel) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->channel) 0 0 0
53 $promise = $promise->then(
54 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
55 0 warn "-- create new channel\n" if DEBUG;
56 0 my $channel_promise = Mojo::Promise->new;
57 0 my $channel = Mojo::RabbitMQ::Client::Channel->new();
58  
59 0 ▶ $channel->catch(sub { $channel_promise->reject($_[1]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 ▶ $channel->on(close => sub { warn 'Channel closed: ' . $_[1]->method_frame->reply_text; });
Subroutine __ANON__: not called CC=1 SCAR=6.9
61  
62 $channel->on(
63 0 ▶ open => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 my ($channel) = @_;
65 0 warn "-- channel opened\n" if DEBUG;
66  
67 0 $self->channel($channel);
68 0 $channel->qos(%{$self->defaults->{qos}})->deliver;
69 0 $channel_promise->resolve;
70 }
71 0 );
72  
73 0 $self->client->open_channel($channel);
74 0 return $channel_promise;
75 }
76 0 );
77 }
78  
79 # Start consuming messages
80 $promise = $promise->then(
81 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my $consumer_promise = Mojo::Promise->new;
83 my $consumer = $self->channel->consume(
84 queue => $self->client->url->query->param('queue'),
85 0 %{$self->defaults->{consumer}}
86 );
87 $consumer->on(
88 0 ▶ message => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 warn "-- message received\n" if DEBUG;
90 0 my ($client, $message) = @_;
91 0 $self->emit('message', $message);
92 }
93 0 );
94 0 ▶ $consumer->on('success' => sub { $consumer_promise->resolve(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 $consumer->deliver;
96 0 return $consumer_promise;
97 }
98 0 );
99  
100 0 return $promise;
101 }
102  
103 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
Pod undocumented
104 0 my $self = shift;
105  
106 0 ▶ if ($self->client) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->client) 0 0 0
107 0 $self->client->close();
108 }
109 }
110  
111 5 1;
112  
113 =encoding utf8
114  
115 =head1 NAME
116  
117 Mojo::RabbitMQ::Client::Consumer - simple Mojo::RabbitMQ::Client based consumer
118  
119 =head1 SYNOPSIS
120  
121 use Mojo::RabbitMQ::Client::Consumer;
122 my $consumer = Mojo::RabbitMQ::Consumer->new(
123 url => 'amqp://guest:guest@127.0.0.1:5672/?exchange=mojo&queue=mojo',
124 defaults => {
125 qos => {prefetch_count => 1},
126 queue => {durable => 1},
127 consumer => {no_ack => 0},
128 }
129 );
130  
131 $consumer->catch(sub { die "Some error caught in Consumer" } );
132 $consumer->on('success' => sub { say "Consumer ready" });
133 $consumer->on(
134 'message' => sub {
135 my ($consumer, $message) = @_;
136  
137 $consumer->channel->ack($message)->deliver;
138 }
139 );
140 $consumer->consume_p->wait;
141  
142 Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
143  
144 =head1 DESCRIPTION
145  
146 =head1 EVENTS
147  
148 L<Mojo::RabbitMQ::Client::Consumer> inherits all events from L<Mojo::EventEmitter> and can emit the
149 following new ones.
150  
151 =head1 ATTRIBUTES
152  
153 L<Mojo::RabbitMQ::Client::Consumer> has following attributes.
154  
155 =head1 METHODS
156  
157 L<Mojo::RabbitMQ::Client::Consumer> inherits all methods from L<Mojo::EventEmitter> and implements
158 the following new ones.
159  
160 =head1 SEE ALSO
161  
162 L<Mojo::RabbitMQ::Client>
163  
164 =head1 COPYRIGHT AND LICENSE
165  
166 Copyright (C) 2015-2017, Sebastian Podjasek and others
167  
168 This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
169  
170 =cut
« Client/Publisher.pm ↑ Summary Client/Method.pm »