RabbitTail/FileTailer.pm

filter: Statementstmt 55.1% 27 / 49 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 60.0% 6 / 10 Podpod 0.0% 0 / 1 totaltotal 45.8% 33 / 72 CC 7 SCAR 25.1
CC
7
Cov
44%
__ANON__
30.0
_read_one_line
12.0
__ANON__
2.0
CRAP 12.2
↑ Summary RabbitTail.pm »
1 package App::RabbitTail::FileTailer;
2 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use MooseX::Types::Moose qw/CodeRef Num/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use MooseX::Types::Path::Class qw/File/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Coro::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 has fn => (
10 isa => File,
11 is => 'ro',
12 required => 1,
13 coerce => 1,
14 );
15  
16 has fh => (
17 is => 'ro',
18 lazy => 1,
19 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
20 0 my $fh = shift->fn->openr;
21 0 seek $fh, 0, 2;
22 0 $fh;
23 },
24 1 );
25  
26 1 has cb => (
27 isa => CodeRef,
28 is => 'ro',
29 required => 1,
30 );
31  
32 1 has _sleep_interval => (
33 isa => Num,
34 is => 'rw',
35 default => 0,
36 init_arg => undef,
37 );
38  
39 1 has _next_backoff => (
40 isa => Num,
41 is => 'rw',
42 clearer => '_clear_next_backoff',
43 predicate => '_has_next_backoff',
44 init_arg => undef,
45 );
46  
47 1 has backoff_increment => (
48 isa => Num,
49 is => 'ro',
50 default => 0.1,
51 );
52  
53 1 has max_sleep => (
54 isa => Num,
55 is => 'ro',
56 default => 10,
57 );
58  
59 1 has _watcher => (
60 is => 'rw'
61 );
62  
63 0 ▶sub tail {
Subroutine tail: not called CC=1 SCAR=6.9
Pod undocumented
64 0 my ($self) = @_;
65 $self->_watcher(AnyEvent->timer(
66 after => $self->_sleep_interval,
67 0 ▶ cb => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
68 0 ▶ if ( !$self->_read_one_line ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_read_one_line) 0 0 0
69 0 ▶ if (!$self->_has_next_backoff) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_has_next_backoff) 0 0 0
70 0 $self->_next_backoff($self->backoff_increment);
71 }
72 0 $self->_sleep_interval($self->_sleep_interval + $self->_next_backoff);
73 0 ▶ if ($self->_sleep_interval > $self->max_sleep) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_sleep_interval > $self->max_sleep) { } 0 0 0
elsif ($self->_sleep_interval < $self->max_sleep) { } 0 0 0
74 0 $self->_sleep_interval($self->max_sleep);
75 0 $self->_next_backoff(0);
76 }
77 elsif ($self->_sleep_interval < $self->max_sleep) {
78 0 $self->_next_backoff( $self->_next_backoff * 2 );
79 }
80 }
81 0 $self->tail;
82 },
83 0 ));
84 }
85  
86 0 ▶sub _read_one_line {
Subroutine _read_one_line: not called CC=3 SCAR=24.8
87 0 my $self = shift;
88 0 my $line = unblock($self->fh)->readline;
89 0 ▶ return if !defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 0 0
90 0 chomp($line);
91 0 $self->_sleep_interval(0);
92 0 $self->_clear_next_backoff;
93 0 ▶ $self->cb->($line) if length $line;
Branch: 1 branch
BranchTrueFalseTotal
if length $line 0 0 0
94 0 return $line;
95 }
96  
97 1 __PACKAGE__->meta->make_immutable;
98 __END__
↑ Summary RabbitTail.pm »