RabbitTail.pm

filter: Statementstmt 49.4% 41 / 83 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.0% 9 / 20 Podpod 0.0% 0 / 3 totaltotal 41.3% 50 / 121 CC 8 SCAR 15.6
CC
8
Cov
43%
_build_ch
12.0
tail
12.0
_build_rf
6.0
CRAP 4.8
« RabbitTail/FileTailer.pm ↑ Summary
1 package App::RabbitTail;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Net::RabbitFoot 1.03;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use App::RabbitTail::FileTailer;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Moose::Autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooseX::Types::Moose qw/ArrayRef Str Int/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Try::Tiny qw/ try catch /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = '0.002';
13 1 $VERSION = eval $VERSION;
14  
15 1 with 'MooseX::Getopt';
16  
17 1 has filename => (
18 isa => ArrayRef[Str],
19 is => 'ro',
20 cmd_aliases => ['fn'],
21 required => 1,
22 traits => ['Getopt'],
23 );
24  
25 has routing_key => (
26 isa => ArrayRef[Str],
27 is => 'ro',
28 cmd_aliases => ['rk'],
29 0 ▶ default => sub { [ '#' ] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 1 traits => ['Getopt'],
31 );
32  
33 1 has max_sleep => (
34 isa => Int,
35 is => 'ro',
36 default => 10,
37 documentation => 'The max sleep time between trying to read a line from an input file',
38 );
39  
40 has _cv => (
41 is => 'ro',
42 lazy => 1,
43 0 ▶ default => sub { AnyEvent->condvar },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 1 clearer => '_clear_cv',
45 );
46  
47 1 my $rf = Net::RabbitFoot->new(
48 varbose => 1,
49 )->load_xml_spec();
50  
51 1 has _rf => (
52 isa => 'Net::RabbitFoot',
53 is => 'ro',
54 lazy => 1,
55 builder => '_build_rf',
56 clearer => '_clear_rf',
57 );
58  
59 0 ▶sub _build_rf {
Subroutine _build_rf: not called CC=2 SCAR=17.9
60 0 my ($self) = @_;
61 0 my $rf_conn;
62 0 ▶ while (!$rf_conn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rf_conn) 0 0 0
63 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 $rf_conn = $rf->connect(
65 0 ▶ on_close => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 warn(sprintf("RabbitMQ connection to %s:%s closed!\n", $self->host, $self->port));
67 0 $self->_clear_ch;
68 0 $self->_clear_rf;
69 0 $self->_cv->send("ARGH");
70 },
71 0 map { $_ => $self->$_ }
72 qw/ host port user pass vhost /
73 );
74 }
75 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 0 warn($_);
77 0 sleep 2;
78 0 };
79 }
80 0 return $rf_conn;
81 }
82  
83 1 my %defaults = (
84 host => 'localhost',
85 port => 5672,
86 user => 'guest',
87 pass => 'guest',
88 vhost => '/',
89 exchange_type => 'direct',
90 exchange_name => 'logs',
91 exchange_durable => 0,
92 );
93  
94 1 foreach my $k (keys %defaults) {
95 8 has $k => ( is => 'ro', isa => Str, default => $defaults{$k} );
96 }
97  
98 1 has _ch => (
99 is => 'ro',
100 lazy => 1,
101 builder => '_build_ch',
102 clearer => '_clear_ch',
103 predicate => '_has_ch',
104 );
105  
106 0 ▶sub _build_ch {
Subroutine _build_ch: not called CC=3 SCAR=24.8
107 0 my ($self) = @_;
108 0 my $ch = $self->_rf->open_channel;
109 0 my $exch_frame = $ch->declare_exchange(
110 type => $self->exchange_type,
111 durable => $self->exchange_durable,
112 exchange => $self->exchange_name,
113 )->method_frame;
114 0 ▶ die Dumper($exch_frame) unless blessed $exch_frame
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
115 and $exch_frame->isa('Net::AMQP::Protocol::Exchange::DeclareOk');
116 0 return $ch;
117 }
118  
119 0 ▶sub run {
Subroutine run: not called CC=2 SCAR=17.9
Pod undocumented
120 0 my $self = shift;
121 0 my $tail_started = 0;
122 0 while (1) {
123 0 $self->_clear_cv;
124 0 $self->_ch; # Build channel before going into the event loop
125 0 ▶ $self->tail # Setup all the timers
Branch: 1 branch
BranchTrueFalseTotal
unless $tail_started++ 0 0 0
126 unless $tail_started++;
127 0 $self->_cv->recv; # Enter event loop. We will leave here if channel dies..
128 }
129 }
130  
131 0 ▶sub tail {
Subroutine tail: not called CC=3 SCAR=24.8
Pod undocumented
132 0 my $self = shift;
133 0 my $rkeys = $self->routing_key;
134 0 foreach my $fn ($self->filename->flatten) {
135 0 my $rk = $rkeys->shift;
136 0 ▶ $rkeys->unshift($rk) unless $rkeys->length;
Branch: 1 branch
BranchTrueFalseTotal
unless $rkeys->length 0 0 0
137 # warn("Setup tail for $fn on $rk");
138 0 my $ft = $self->setup_tail($fn, $rk);
139 0 $ft->tail;
140 }
141 }
142  
143 0 ▶sub setup_tail {
Subroutine setup_tail: not called CC=1 SCAR=6.9
Pod undocumented
144 0 my ($self, $file, $routing_key) = @_;
145 App::RabbitTail::FileTailer->new(
146 max_sleep => $self->max_sleep,
147 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
148 0 my $message = shift;
149 0 chomp($message);
150 # warn("SENT $message to " . $self->exchange_name . " with " . $routing_key);
151 0 ▶ if (!$self->_has_ch) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_ch) 0 0 0
152 0 warn("DROPPED $message to " . $self->exchange_name . " with " . $routing_key . "\n");
153 0 return;
154 }
155 $self->_ch->publish(
156 0 body => $message,
157 exchange => $self->exchange_name,
158 routing_key => $routing_key,
159 );
160 },
161 0 fn => $file,
162 );
163 }
164  
165 1 __PACKAGE__->meta->make_immutable;
166 __END__
« RabbitTail/FileTailer.pm ↑ Summary