1 package Catalyst::Engine::HTTP::Restarter::Watcher;
4 with 'MooseX::Emulate::Class::Accessor::Fast';
9 use Time::HiRes qw/sleep/;
11 has delay => (is => 'rw');
12 has regex => (is => 'rw');
13 has modified => (is => 'rw');
14 has directory => (is => 'rw');
15 has watch_list => (is => 'rw');
16 has follow_symlinks => (is => 'rw');
27 my $watch_list = $self->_index_directory;
28 $self->watch_list($watch_list);
33 files => [ keys %{$watch_list} ],
44 my $delay = ( defined $self->delay ) ? $self->delay : 1;
46 sleep $delay if $delay > 0;
48 eval { @changes = $self->modified->changed };
51 # File::Modified will die if a file is deleted.
52 my ($deleted_file) = $@ =~ /stat '(.+)'/;
53 push @changed_files, $deleted_file || 'unknown file';
58 # update all mtime information
59 $self->modified->update;
61 # check if any files were changed
62 @changed_files = grep { -f $_ } @changes;
64 # Check if only directories were changed. This means
65 # a new file was created.
66 unless (@changed_files) {
68 # re-index to find new files
69 my $new_watch = $self->_index_directory;
71 # look through the new list for new files
72 my $old_watch = $self->watch_list;
73 @changed_files = grep { !defined $old_watch->{$_} }
76 return unless @changed_files;
80 for my $file (@changed_files) {
81 next unless $file =~ /\.pm$/;
82 if ( my $error = $self->_test($file) ) {
83 print STDERR qq/File "$file" modified, not restarting\n\n/;
84 print STDERR '*' x 80, "\n";
86 print STDERR '*' x 80, "\n";
92 return @changed_files;
95 sub _index_directory {
98 my $dir = $self->directory;
99 die "No directory specified" if !$dir or ref($dir) && !@{$dir};
101 my $regex = $self->regex || '\.pm$';
107 my $file = File::Spec->rel2abs($File::Find::name);
108 return unless $file =~ /$regex/;
109 return unless -f $file;
110 $file =~ s{/script/..}{};
113 # also watch the directory for changes
114 my $cur_dir = File::Spec->rel2abs($File::Find::dir);
115 $cur_dir =~ s{/script/..}{};
118 follow_fast => $self->follow_symlinks ? 1 : 0,
121 ref $dir eq 'ARRAY' ? @{$dir} : $dir
127 my ( $self, $file ) = @_;
130 local $SIG{__WARN__} = sub { };
132 open my $olderr, '>&STDERR';
133 open STDERR, '>', File::Spec->devnull;
134 eval "require '$file'";
135 open STDERR, '>&', $olderr;
137 return ($@) ? $@ : 0;
145 Catalyst::Engine::HTTP::Restarter::Watcher - Watch for changed application
150 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
151 directory => '/path/to/MyApp',
152 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
157 my @changed_files = $watcher->watch();
162 This class monitors a directory of files for changes made to any file
163 matching a regular expression. It correctly handles new files added to the
164 application as well as files that are deleted.
168 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
170 Creates a new Watcher object.
174 Returns a list of files that have been added, deleted, or changed since the
175 last time watch was called.
179 L<Catalyst>, L<Catalyst::Engine::HTTP::Restarter>, L<File::Modified>
183 Catalyst Contributors, see Catalyst.pm
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.