TailRabbit.pm

filter: Statementstmt 56.1% 50 / 89 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 48.0% 12 / 25 Podpod 0.0% 0 / 3 totaltotal 45.9% 62 / 135 CC 8 SCAR 19.7
CC
8
Cov
49%
_bind_anon_queue
30.0
_get_channel
12.0
get_config_from_file
6.0
CRAP 7.2
↑ Summary TailRabbit/Convertor/Null.pm »
1 package App::TailRabbit;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use MooseX::Getopt;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Net::RabbitFoot;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MooseX::Types::Moose qw/ ArrayRef Object Bool /;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MooseX::Types::Common::String qw/ NonEmptySimpleStr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use YAML qw/LoadFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Path::Class qw/ file /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use MooseX::Types::LoadableClass qw/ LoadableClass /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 our $VERSION = '0.003';
16  
17 1 with qw/
18 MooseX::Getopt
19 MooseX::ConfigFromFile
20 /;
21  
22 1 has exchange_name => (
23 is => 'ro',
24 isa => NonEmptySimpleStr,
25 required => 1,
26 );
27  
28 has routing_key => (
29 is => 'ro',
30 isa => ArrayRef[NonEmptySimpleStr],
31 0 ▶ default => sub { [] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 1 );
33  
34 1 has rabbitmq_host => (
35 isa => NonEmptySimpleStr,
36 is => 'ro',
37 default => 'localhost',
38 );
39  
40 1 has [qw/ rabbitmq_user rabbitmq_pass /] => (
41 isa => NonEmptySimpleStr,
42 is => 'ro',
43 default => 'guest',
44 );
45  
46 1 has convertor => (
47 isa => LoadableClass,
48 is => 'ro',
49 coerce => 1,
50 default => 'App::TailRabbit::Convertor::Null',
51 );
52  
53 has _convertor => (
54 is => 'ro',
55 isa => Object,
56 lazy => 1,
57 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
58 shift->convertor->new
59 0 },
60 1 handles => [qw/ convert /],
61 );
62  
63 1 has exchange_type => (
64 is => 'ro',
65 isa => NonEmptySimpleStr,
66 default => 'topic',
67 );
68  
69 1 has durable => (
70 is => 'ro',
71 isa => Bool,
72 default => 0,
73 );
74  
75 0 ▶sub get_config_from_file {
Subroutine get_config_from_file: not called CC=2 SCAR=17.9
Pod undocumented
76 0 my ($class, $file) = @_;
77 0 ▶ return LoadFile($file) if (-r $file);
Branch: 1 branch
BranchTrueFalseTotal
if -r $file 0 0 0
78 0 return {};
79 }
80  
81 1 has 'configfile' => (
82 default => file(File::HomeDir->my_home(), ".tailrabbit.yml")->stringify,
83 is => 'bare',
84 );
85  
86 1 my $rf = Net::RabbitFoot->new(
87 # verbose => 1,
88 )->load_xml_spec();
89  
90 0 ▶sub _get_mq {
Subroutine _get_mq: not called CC=1 SCAR=6.9
91 0 my $self = shift;
92 $rf->connect(
93 host => $self->rabbitmq_host,
94 port => 5672,
95 user => $self->rabbitmq_user,
96 pass => $self->rabbitmq_pass,
97 vhost => '/',
98 0 ▶ on_close => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 0 die("MQ connection closed");
100 },
101 0 ▶ on_read_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 die("READ FAILED");
103 },
104 0 ▶ on_failure => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
105 0 die("Failed to connect to mq");
106 },
107 0 );
108 0 return $rf;
109 }
110  
111 0 ▶sub _bind_anon_queue {
Subroutine _bind_anon_queue: not called CC=5 SCAR=34.0
112 0 my ($self, $ch) = @_;
113 0 my $queue_frame = $ch->declare_queue(
114 auto_delete => 1,
115 exclusive => 1,
116 )->method_frame;
117 0 my @keys = @{ $self->routing_key };
118 0 ▶ push(@keys, "#") unless scalar @keys;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @keys 0 0 0
119 0 foreach my $key (@keys) {
120 0 my $bind_frame = $ch->bind_queue(
121 queue => $queue_frame->queue,
122 exchange => $self->exchange_name,
123 routing_key => $key,
124 )->method_frame;
125 0 ▶ die Dumper($bind_frame) unless blessed $bind_frame and $bind_frame->isa('Net::AMQP::Protocol::Queue::BindOk');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($bind_frame) and $bind_frame->isa("Net::AMQP::Protocol::Queue::BindOk") 0 0 0
Condition0%0/3
builtin::blessed($bind_frame) and $bind_frame->isa("Net::AMQP::Protocol::Queue::BindOk")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($bind_frame) and $bind_frame->isa("Net::AMQP::Protocol::Queue::BindOk")
builtin::blessed($bind_frame) $bind_frame->isa("Net::AMQP::Protocol::Queue::BindOk")
Truth table0%0/3
A and B
A= builtin::blessed($bind_frame)B= $bind_frame->isa("Net::AMQP::Protocol::Queue::BindOk")
ABresult
0X0
100
111
126 }
127 }
128  
129 0 ▶sub _get_channel {
Subroutine _get_channel: not called CC=3 SCAR=24.8
130 0 my ($self, $rf) = @_;
131 my $ch = $rf->open_channel(
132 0 ▶ on_close => sub { warn("Channel closed - wrong exchange options!\n"); exit; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 );
134 0 my $reply = $ch->declare_exchange(
135 type => $self->exchange_type,
136 durable => $self->durable,
137 exchange => $self->exchange_name,
138 );
139 0 my $exch_frame = $reply->method_frame;
140 0 ▶ die Dumper($exch_frame) unless blessed $exch_frame and $exch_frame->isa('Net::AMQP::Protocol::Exchange::DeclareOk');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($exch_frame) and $exch_frame->isa("Net::AMQP::Protocol::Exchange::DeclareOk") 0 0 0
Condition0%0/3
builtin::blessed($exch_frame) and $exch_frame->isa("Net::AMQP::Protocol::Exchange::DeclareOk")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($exch_frame) and $exch_frame->isa("Net::AMQP::Protocol::Exchange::DeclareOk")
builtin::blessed($exch_frame) $exch_frame->isa("Net::AMQP::Protocol::Exchange::DeclareOk")
Truth table0%0/3
A and B
A= builtin::blessed($exch_frame)B= $exch_frame->isa("Net::AMQP::Protocol::Exchange::DeclareOk")
ABresult
0X0
100
111
141 0 return $ch;
142 }
143  
144 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
145 0 my $self = shift;
146 0 my $ch = $self->_get_channel($self->_get_mq);
147 0 $self->_bind_anon_queue($ch);
148 0 my $done = AnyEvent->condvar;
149 $ch->consume(
150 0 ▶ on_consume => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
151 0 my $message = shift;
152 0 my $payload = $self->convert($message->{body}->payload);
153 0 my $routing_key = $message->{deliver}->method_frame->routing_key;
154 0 $self->notify($payload, $routing_key, $message);
155 },
156 0 );
157 0 $done->recv; # Go into the event loop forever.
158 }
159  
160 0 ▶sub notify {
Subroutine notify: not called CC=1 SCAR=6.9
Pod undocumented
161 0 my ($self, $payload, $routing_key, $message) = @_;
162 0 print $routing_key,
163 ': ', $payload, "\n";
164 }
165  
166 1 __PACKAGE__->meta->make_immutable;
167 1 1;
168  
169 =head1 NAME
170  
171 App::TailRabbit - Listen to a RabbitMQ exchange and emit the messages to console.
172  
173 =head1 SYNOPSIS
174  
175 tail_reabbit --exchange_name firehose --routing_key # --rabbitmq_user guest --rabbitmq_user guest --rabbitmq_host localhost
176  
177 =head1 DESCRIPTION
178  
179 Simple module to consume messages from a RabitMQ message queue.
180  
181 =head1 BUGS
182  
183 =over
184  
185 =item Virtually no docs
186  
187 =item Creates all exchanges as durable.
188  
189 =item Always creates exchange if it doesn't exist
190  
191 =item Probably several more
192  
193 =back
194  
195 =head1 SEE ALSO
196  
197 L<Net::RabbitFoot>
198  
199 =head1 AUTHOR
200  
201 Tomas (t0m) Doran C<< <bobtfish@bobtfish.net> >>.
202  
203 =head1 COPYRIGHT & LICENSE
204  
205 Copyright the above author(s).
206  
207 Licensed under the same terms as perl itself.
208  
209 =cut
210  
↑ Summary TailRabbit/Convertor/Null.pm »