lib/InfluxDB/Writer/RememberingFileTailer.pm

filter: Statementstmt 40.4% 40 / 99 Branchbran 0.0% 0 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 68.7% 11 / 16 Podpod 50.0% 1 / 2 totaltotal 35.8% 52 / 145 CC 15 SCAR 35.0
CC
15
Cov
31%
slurp_and_send
90.0
archive_file
30.0
__ANON__
6.0
CRAP 33.2
« lib/InfluxDB/Writer/CompactFiles.pm ↑ Summary lib/InfluxDB/Writer/FileTailer.pm »
1 package InfluxDB::Writer::RememberingFileTailer;
2  
3 # ABSTRACT: RememberingFileTailer
4 1 our $VERSION = '1.003'; # VERSION
5  
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use feature 'say';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use IO::Async::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use IO::Async::FileStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Hijk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use InfluxDB::LineProtocol qw(line2data data2line);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Spec::Functions;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use File::Spec qw(splitpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 extends 'InfluxDB::Writer::FileTailer';
20  
21 1 has 'done_dir' => ( is => 'rw', isa => "Str" );
22  
23 0 ▶before 'run' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
24 0 my $self = shift;
25  
26 0 my $done_dir = catdir( $self->dir, 'done' );
27 0 ▶ if ( -d $done_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $done_dir) { } 0 0 0
28 0 $self->done_dir($done_dir);
29 }
30 else {
31 0 croak "Missing 'done' directory, please create: " . $done_dir;
32 }
33  
34 1 };
35  
36 0 ▶sub archive_file {
Subroutine archive_file: not called CC=5 SCAR=34.0
Pod undocumented
37 0 my $self = shift;
38 0 my $file = shift;
39  
40 0 my $done_dir = $self->done_dir;
41 0 my ( $vol, $dirs, $basename ) = File::Spec->splitpath($file);
42  
43 0 my $destination = catfile( $done_dir, $basename );
44 0 my $count = 0;
45 0 ▶ while ( -f $destination ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $destination) 0 0 0
46 0 $count++;
47 0 my $new_destination = catfile( $done_dir, $basename ) . '_' . $count;
48 0 $log->infof( "Renaming destination %s to %s",
49 $destination, $new_destination );
50 0 $destination = $new_destination;
51 }
52  
53 0 ▶ if ( -s $file == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-s $file == 0) 0 0 0
54  
55 0 ▶ if ( unlink($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (unlink $file) { } 0 0 0
56 0 $log->infof( "Unlinked zero size stats file %s", $file );
57 0 return 1;
58 }
59 else {
60 0 $log->warnf( "Could not unlink zero size stats file %s (%s)",
61 $file, $! );
62 0 return 0;
63 }
64  
65 0 return 0;
66 }
67  
68 0 ▶ if ( rename( $file, $destination ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (rename $file, $destination) { } 0 0 0
69 0 $log->infof( "Archived file %s to %s", $basename, $done_dir );
70 0 return 1;
71 }
72 else {
73 0 $log->errorf( "Failed to archive %s to %s", $basename, $done_dir );
74 0 croak "Failed to archive $file";
75 0 return 0;
76 }
77  
78 }
79  
80  
81 0 ▶sub slurp_and_send {
Subroutine slurp_and_send: not called CC=9 SCAR=45.0
Pod documented
82 0 my $self = shift;
83 0 my $file = shift;
84  
85 0 ▶ if ( open( my $fh, "<", $file ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $file) 0 0 0
86 0 $log->infof( "Slurping %s", $file );
87  
88 0 ▶ while ( my $line = <$fh> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
89 0 chomp($line);
90 0 ▶ if ( $self->has_tags ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_tags) 0 0 0
91 0 $line = $self->add_tags_to_line($line);
92 }
93 0 ▶ if ($line !~ /\s\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\s\d+$/) 0 0 0
94 # Line does not end in timestamp
95 0 $log->warnf("Skipping probably broken line %s", $line);
96 0 next;
97
98 }
99 0 $self->buffer_push($line);
100  
101 0 ▶ if ( $self->buffer_size > $self->flush_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->buffer_size > $self->flush_size) 0 0 0
102 0 ▶ if ( !$self->send ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->send) 0 0 0
103 0 $log->warnf( "Unable to send buffer (%i lines)",
104 $self->buffer_size );
105 0 return;
106 }
107 }
108 }
109  
110 0 ▶ if ( $self->buffer_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->buffer_size) 0 0 0
111 0 $log->infof( "Clear buffer (size %i) for file %s",
112 $self->buffer_size, $file );
113 0 ▶ if ( !$self->send ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->send) 0 0 0
114 0 $log->warnf( "Unable to send clear buffer (%i lines)",
115 $self->buffer_size );
116 0 return;
117 }
118 }
119  
120 0 $log->infof( "Finished slurping %s", $file );
121  
122 0 return 1;
123 }
124  
125 0 return;
126  
127 }
128  
129 0 ▶override 'archive_hook' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 0 shift->archive_file(@_);
131 0 super();
132 1 };
133  
134 0 ▶override 'cleanup_hook' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
135 0 my ( $self, $file ) = @_;
136  
137 0 ▶ if ( $self->slurp_and_send($file) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->slurp_and_send($file)) 0 0 0
138 0 $self->archive_file($file);
139 }
140 0 return super();
141  
142 1 };
143  
144 1 1;
145  
146 __END__
« lib/InfluxDB/Writer/CompactFiles.pm ↑ Summary lib/InfluxDB/Writer/FileTailer.pm »