1 package Catalyst::Engine::HTTP::Restarter::Watcher;
7 use Time::HiRes qw/sleep/;
9 has delay => (is => 'rw');
10 has regex => (is => 'rw');
11 has modified => (is => 'rw');
12 has directory => (is => 'rw');
13 has watch_list => (is => 'rw');
14 has follow_simlinks => (is => 'rw');
25 my $watch_list = $self->_index_directory;
26 $self->watch_list($watch_list);
31 files => [ keys %{$watch_list} ],
42 my $delay = ( defined $self->delay ) ? $self->delay : 1;
44 sleep $delay if $delay > 0;
46 eval { @changes = $self->modified->changed };
49 # File::Modified will die if a file is deleted.
50 my ($deleted_file) = $@ =~ /stat '(.+)'/;
51 push @changed_files, $deleted_file || 'unknown file';
56 # update all mtime information
57 $self->modified->update;
59 # check if any files were changed
60 @changed_files = grep { -f $_ } @changes;
62 # Check if only directories were changed. This means
63 # a new file was created.
64 unless (@changed_files) {
66 # re-index to find new files
67 my $new_watch = $self->_index_directory;
69 # look through the new list for new files
70 my $old_watch = $self->watch_list;
71 @changed_files = grep { !defined $old_watch->{$_} }
74 return unless @changed_files;
78 for my $file (@changed_files) {
79 next unless $file =~ /\.pm$/;
80 if ( my $error = $self->_test($file) ) {
81 print STDERR qq/File "$file" modified, not restarting\n\n/;
82 print STDERR '*' x 80, "\n";
84 print STDERR '*' x 80, "\n";
90 return @changed_files;
93 sub _index_directory {
96 my $dir = $self->directory;
97 die "No directory specified" if !$dir or ref($dir) && !@{$dir};
99 my $regex = $self->regex || '\.pm$';
105 my $file = File::Spec->rel2abs($File::Find::name);
106 return unless $file =~ /$regex/;
107 return unless -f $file;
108 $file =~ s{/script/..}{};
111 # also watch the directory for changes
112 my $cur_dir = File::Spec->rel2abs($File::Find::dir);
113 $cur_dir =~ s{/script/..}{};
116 follow_fast => $self->follow_symlinks ? 1 : 0,
119 ref $dir eq 'ARRAY' ? @{$dir} : $dir
125 my ( $self, $file ) = @_;
128 local $SIG{__WARN__} = sub { };
130 open my $olderr, '>&STDERR';
131 open STDERR, '>', File::Spec->devnull;
132 eval "require '$file'";
133 open STDERR, '>&', $olderr;
135 return ($@) ? $@ : 0;
143 Catalyst::Engine::HTTP::Restarter::Watcher - Watch for changed application
148 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
149 directory => '/path/to/MyApp',
150 regex => '\.yml$|\.yaml$|\.pm$',
155 my @changed_files = $watcher->watch();
160 This class monitors a directory of files for changes made to any file
161 matching a regular expression. It correctly handles new files added to the
162 application as well as files that are deleted.
166 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
168 Creates a new Watcher object.
172 Returns a list of files that have been added, deleted, or changed since the
173 last time watch was called.
177 L<Catalyst>, L<Catalyst::Engine::HTTP::Restarter>, L<File::Modified>
181 Sebastian Riedel, <sri@cpan.org>
183 Andy Grundman, <andy@hybridized.org>
187 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
191 This program is free software, you can redistribute it and/or modify it under
192 the same terms as Perl itself.