CSE/Command/Watch.pm untested

filter: Statementstmt 0.0% 0 / 131 Branchbran 0.0% 0 / 34 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 176 CC 18 SCAR 54.3
CC
18
Cov
0%
execute
342.0
BEGIN
2.0
BEGIN
2.0
CRAP 228.7
1 package App::CSE::Command::Watch;
2 $App::CSE::Command::Watch::VERSION = '0.016';
3 use Moose;
4 extends qw/App::CSE::Command/;
5 with qw/App::CSE::Role::DirIndex/;
6  
7  
8 use App::CSE::Command::Check;
9 use App::CSE::Command::Index;
10 use App::CSE::File;
11  
12 use File::MimeInfo::Magic;
13  
14 use Filesys::Notify::Simple;
15  
16 use Lucy::Search::IndexSearcher;
17 use Lucy::Index::Indexer;
18  
19 use Log::Log4perl;
20 my $LOGGER = Log::Log4perl->get_logger();
21  
22  
23 sub execute{
24 my ($self) = @_;
25  
26 my $colorizer = $self->cse()->colorizer();
27 my $colored = sub{ $colorizer->colored(@_);};
28  
29 my $cse = $self->cse();
30  
31 # Check the index.
32 # Re-index if nothing is there.
33 my $check = App::CSE::Command::Check->new({ cse => $cse });
34 if( $check->execute() ){
35 $LOGGER->info(&$colored("Rebuilding the index..", 'green bold'));
36 my $index_cmd = App::CSE::Command::Index->new( { cse => $cse });
37 if( $index_cmd->execute() ){
38 $LOGGER->error(&$colored("Building index failed", 'red'));
39 return 1;
40 }
41 }
42  
43 if( my $previous_pid = $cse->index_meta()->{'watcher.pid'} ){
44 # A previous pid should be a number.
45 ( $previous_pid ) = ( $previous_pid =~ /(\d+)/ );
46 if( kill(0, $previous_pid ) ){
47 $LOGGER->error(&$colored("Previous watcher (PID=".$previous_pid.") is still running. Try cse unwatch first",
48 "red bold"));
49 return 1;
50 }
51 }
52  
53  
54  
55  
56  
57 my $direct_child = fork();
58 confess("Cannot fork() a direct child: $!") unless defined $direct_child;
59  
60 if( $direct_child ){
61 waitpid($direct_child , 0 );
62 return 0;
63 }
64  
65 my $pid = fork();
66 confess("Cannot fork() a worker child: $!") unless defined $pid;
67  
68 if( $pid ){
69 $cse->index_meta()->{'watcher.pid'} = $pid;
70 $cse->index_meta()->{'watcher.started'} = DateTime->now()->iso8601();
71 $cse->save_index_meta();
72 $LOGGER->info(&$colored("PID=$pid : Watching for changes in ".$self->dir_index().", updating ".$cse->index_dir(), "green bold"));
73 exit(0);
74 }
75  
76 my $deamon_log = q|log4perl.rootLogger = INFO, SYSLOG
77 log4perl.appender.SYSLOG = Log::Dispatch::Syslog
78 log4perl.appender.SYSLOG.min_level = debug
79 log4perl.appender.SYSLOG.ident = cse V|.$cse->version().q|[|.$$.q|]
80 log4perl.appender.SYSLOG.facility = daemon
81 log4perl.appender.SYSLOG.layout = Log::Log4perl::Layout::SimpleLayout
82 |;
83  
84 Log::Log4perl::init(\$deamon_log);
85  
86 $LOGGER->info("Watching for changes in ".$self->dir_index().", updating ".$cse->index_dir());
87  
88  
89 my $lock = 0;
90 my $should_exit = 0;
91  
92 $SIG{TERM} = $SIG{INT} = sub{
93 $LOGGER->info("Caught INT or TERM signal. Will exit");
94 $should_exit = 1;
95  
96 unless( $lock ){
97 exit(0);
98 }
99 };
100  
101  
102  
103  
104 my $watcher = Filesys::Notify::Simple->new([ $self->dir_index()->absolute()->stringify() ]);
105 while( !$should_exit ){
106 $watcher->wait(sub {
107 my @events = @_;
108  
109 # Lock exiting before we do anything meaty
110 $lock = 1;
111  
112 eval{
113  
114  
115  
116  
117 foreach my $event ( @events ) {
118  
119  
120 my $file_name = $event->{path};
121 # file_path here is absolute.
122 # $self->dir_index() can be relative
123 if ( $self->dir_index()->is_relative() ) {
124 # We should remove the absolute prefix from the file path
125 my $abs_prefix = $self->dir_index->absolute()->stringify();
126 $file_name =~ s/^$abs_prefix/\./ ;
127 }
128  
129 # Unreadable files are invalid. This will help
130 # not adding deleted files.
131 my $is_hidden = 0;
132 my $is_gone = 0;
133 $cse->is_file_valid( $file_name , { on_hidden => sub{ $is_hidden = 1 ; return 0; },
134 on_unreadable => sub{ $is_gone = 1 ; return 0; },
135 on_skip => sub{ $is_gone = 1 ; return 0; } # Consider skipped files as gone.
136 } );
137 if( $is_hidden ){
138 # We dont do anything about hidden files.
139 next;
140 }
141  
142  
143 # Build an indexer just for this event. This is to avoid complex
144 # committing logic.
145 my $searcher = Lucy::Search::IndexSearcher->new( index => $cse->index_dir().'' );
146 my $indexer = Lucy::Index::Indexer->new( schema => $searcher->get_schema(),
147 index => $cse->index_dir().'' );
148  
149  
150 # Delete the file anyway.
151 $LOGGER->debug("Deleting $file_name from index");
152 $indexer->delete_by_term( field => 'path.raw',
153 term => $file_name );
154  
155 # If the file is gone, that is it. We need to commit and go to the next iteration.
156 if( $is_gone ){
157 $indexer->commit();
158 next;
159 }
160  
161 my $mime_type = $cse->valid_mime_type($file_name);
162 unless( $mime_type ){
163 $indexer->commit();
164 next;
165 }
166  
167 my $file_class = App::CSE::File->class_for_mime($mime_type, $file_name.'');
168 unless( $file_class ){
169 $indexer->commit();
170 next;
171 }
172  
173 ## Build a file instance.
174 my $file = $file_class->new({cse => $cse,
175 mime_type => $mime_type,
176 file_path => $file_name.'' })->effective_object();
177  
178 my $content = $file->content();
179  
180  
181 $LOGGER->debug("Adding $file_name to index as ".$file->mime_type());
182 $indexer->add_doc({
183 path => $file->file_path(),
184 'path.raw' => $file->file_path(),
185 dir => $file->dir(),
186 mime => $file->mime_type(),
187 mtime => $file->mtime->iso8601(),
188 $content ? ( content => $content ) : ()
189 });
190  
191 $indexer->commit();
192 } # End of event loop
193  
194 };
195 if ( my $err = $@ ) {
196 $LOGGER->error("ERROR reacting to event: $err");
197 }
198  
199 # Unlock exiting
200 $lock = 0;
201 if ( $should_exit ) {
202 exit(0);
203 }
204  
205 });
206  
207 sleep(10);
208 } # End of while !$should_exit
209  
210  
211 $LOGGER->warn("Terminating myself. Should not occur");
212 exit(0);
213  
214 }
215  
216 __PACKAGE__->meta->make_immutable();