blib/lib/App/MultiModule/Tasks/FollowTail.pm

filter: Statementstmt 100% 20 / 20 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 27 / 27 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package App::MultiModule::Tasks::FollowTail;
2 $App::MultiModule::Tasks::FollowTail::VERSION = '1.161190';
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use POE qw(Wheel::FollowTail);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Message::Transform qw(mtransform);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use parent 'App::MultiModule::Task';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 =head1 NAME
12  
13 App::MultiModule::Tasks::FollowTail - File following task for App::MultiModule
14  
15 =cut
16  
17 =head1 SYNOPSIS
18  
19 Quick summary of what the module does.
20  
21 Perhaps a little code snippet.
22  
23 use App::MultiModule::Tasks::FollowTail;
24  
25 my $foo = App::MultiModule::Tasks::FollowTail->new();
26 ...
27  
28 =head1 EXPORT
29  
30 A list of functions that can be exported. You can delete this section
31 if you don't export anything, such as for a purely object-oriented module.
32  
33 =head1 SUBROUTINES/METHODS
34  
35 =head2 message
36  
37 =cut
38  
39 sub message {
40 my $self = shift;
41 my $message = shift;
42 my %args = @_;
43 $self->debug('message', message => $message)
44 if $self->{debug} > 5;
45 }
46  
47 sub _got_log_line {
48 my ($logfile, $line) = @_;
49 my @follow_names = keys %{$App::MultiModule::Tasks::FollowTail::logfile_map->{$logfile}};
50 foreach my $follow_name (@follow_names) {
51 my $follow_info = $App::MultiModule::Tasks::FollowTail::config->{follows}->{$follow_name};
52 my $regex_string = $follow_info->{regex};
53
54 if(not $App::MultiModule::Tasks::FollowTail::regex_cache->{$regex_string}) {
55 $App::MultiModule::Tasks::FollowTail::regex_cache->{$regex_string} = qr/$regex_string/;
56 }
57 my $re = $App::MultiModule::Tasks::FollowTail::regex_cache->{$regex_string};
58 if($line =~ $re) {
59 my $message = {
60 line => $line,
61 logfile => $logfile,
62 follow_name => $follow_name,
63 regex_string => $regex_string,
64 };
65 if(%+) {
66 while(my($key, $value) = each %+) {
67 $message->{$key} = $value;
68 }
69 }
70 mtransform $message, $follow_info->{transform}
71 if $follow_info->{transform};
72 $App::MultiModule::Tasks::FollowTail::self->emit($message);
73 }
74 }
75 }
76  
77 =head2 set_config
78  
79 =cut
80 sub set_config {
81 my $self = shift;
82 my $config = shift;
83 my %args = @_;
84 my $root_object = $args{root_object};
85 $self->{config} = $config;
86  
87 #only the main module will follow any files.
88 return unless $root_object->{module} eq 'main';
89 #perhaps premature optimization, but this thing has to be *fast*
90 $App::MultiModule::Tasks::FollowTail::config = $config;
91  
92 if(not $self->{local_state}) {
93 $self->{local_state} = {
94 following_files => {},
95 };
96 $App::MultiModule::Tasks::FollowTail::regex_cache = {};
97 $App::MultiModule::Tasks::FollowTail::logfile_map = {};
98 $App::MultiModule::Tasks::FollowTail::self = $self;
99 }
100 my $state = $self->{local_state};
101  
102 if($config->{follows}) {
103 my @follow_names = sort keys %{$config->{follows}};
104 foreach my $follow_name (@follow_names) {
105 my $follow_info = $config->{follows}->{$follow_name};
106 next unless my $logfile = $follow_info->{logfile};
107 $App::MultiModule::Tasks::FollowTail::logfile_map->{$logfile}->{$follow_name} = 1;
108 next if $state->{following_files}->{$logfile};
109 $self->add_session(
110 { inline_states => {
111 _start => sub {
112 $_[HEAP]{tailor} = POE::Wheel::FollowTail->new(
113 Filename => $logfile,
114 InputEvent => 'got_log_line',
115 ResetEvent => 'got_log_rollover',
116 );
117 },
118 got_log_line => sub {
119 _got_log_line($logfile, $_[ARG0]);
120 },
121 got_log_rollover => sub {
122 },
123 }
124 }
125 );
126 $state->{following_files}->{$logfile} = 1;
127 }
128 }
129 $self->debug('Router: set_config') if $self->{debug};
130 }
131  
132 =head1 AUTHOR
133  
134 Dana M. Diederich, C<< <dana@realms.org> >>
135  
136 =head1 BUGS
137  
138 Please report any bugs or feature requests through L<https://github.com/dana/App-MultiModule-Tasks-FollowTail/issues>. I will be notified, and then you'll
139 automatically be notified of progress on your bug as I make changes.
140  
141 =head1 SUPPORT
142  
143 You can find documentation for this module with the perldoc command.
144  
145 perldoc App::MultiModule::Tasks::FollowTail
146  
147  
148 You can also look for information at:
149  
150 =over 4
151  
152 =item * Report bugs here:
153  
154 L<https://github.com/dana/App-MultiModule-Tasks-FollowTail/issues>
155  
156 =item * AnnoCPAN: Annotated CPAN documentation
157  
158 L<http://annocpan.org/dist/App-MultiModule-Tasks-FollowTail>
159  
160 =item * CPAN Ratings
161  
162 L<http://cpanratings.perl.org/d/App-MultiModule-Tasks-FollowTail>
163  
164 =item * Search CPAN
165  
166 L<https://metacpan.org/module/App::MultiModule::Tasks::FollowTail>
167  
168 =back
169  
170 =head1 ACKNOWLEDGEMENTS
171  
172 =head1 LICENSE AND COPYRIGHT
173  
174 Copyright 2014,2016 Dana M. Diederich.
175  
176 This program is free software; you can redistribute it and/or modify it
177 under the terms of the the Artistic License (2.0). You may obtain a
178 copy of the full license at:
179  
180 L<http://www.perlfoundation.org/artistic_license_2_0>
181  
182 Any use, modification, and distribution of the Standard or Modified
183 Versions is governed by this Artistic License. By using, modifying or
184 distributing the Package, you accept this license. Do not use, modify,
185 or distribute the Package, if you do not accept this license.
186  
187 If your Modified Version has been derived from a Modified Version made
188 by someone other than you, you are nevertheless required to ensure that
189 your Modified Version complies with the requirements of this license.
190  
191 This license does not grant you the right to use any trademark, service
192 mark, tradename, or logo of the Copyright Holder.
193  
194 This license includes the non-exclusive, worldwide, free-of-charge
195 patent license to make, have made, use, offer to sell, sell, import and
196 otherwise transfer the Package with respect to any patent claims
197 licensable by the Copyright Holder that are necessarily infringed by the
198 Package. If you institute patent litigation (including a cross-claim or
199 counterclaim) against any party alleging that the Package constitutes
200 direct or contributory patent infringement, then this Artistic License
201 to you shall terminate on the date that such litigation is filed.
202  
203 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
204 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
205 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
206 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
207 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
208 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
209 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
210 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
211  
212  
213 =cut
214  
215 1; # End of App::MultiModule::Tasks::FollowTail
↑ Summary