1 package Catalyst::Watcher::Inotify;
7 use namespace::clean -except => 'meta';
9 extends 'Catalyst::Watcher';
13 isa => 'Linux::Inotify2',
14 default => sub { Linux::Inotify2->new },
27 # If this is done via a lazy_build then the call to
28 # ->_add_directory ends up causing endless recursion when it calls
30 $self->_add_directory($_) for @{ $self->directories };
37 my $restarter = shift;
39 my @events = $self->_wait_for_events;
41 $restarter->handle_changes( map { $self->_event_to_change($_) } @events );
46 sub _wait_for_events {
49 my $regex = $self->regex;
52 # This is a blocking read, so it will not return until
53 # something happens. The restarter will end up calling ->watch
54 # again after handling the changes.
55 my @events = $self->_inotify->read;
58 for my $event (@events) {
59 if ( $event->IN_CREATE && $event->IN_ISDIR ) {
60 $self->_add_directory( $event->fullname );
61 push @interesting, $event;
63 elsif ( $event->IN_DELETE_SELF ) {
65 push @interesting, $event;
67 elsif ( $event->fullname =~ /$regex/ ) {
68 push @interesting, $event;
72 return @interesting if @interesting;
79 my $mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF;
80 $mask |= IN_DONT_FOLLOW unless $self->follow_symlinks;
92 my $path = File::Spec->rel2abs($File::Find::name);
93 return unless -d $path;
95 $self->_inotify->watch( $path, $self->_mask );
97 follow_fast => $self->follow_symlinks ? 1 : 0,
104 sub _event_to_change {
108 my %change = ( file => $event->fullname );
109 if ( $event->IN_CREATE ) {
110 $change{status} = 'added';
112 elsif ( $event->IN_MODIFY ) {
113 $change{status} = 'modified';
115 elsif ( $event->IN_DELETE ) {
116 $change{status} = 'deleted';
119 $change{status} = 'containing directory modified';
125 __PACKAGE__->meta->make_immutable;
133 Catalyst::Watcher - Watch for changed application files
137 my $watcher = Catalyst::Watcher->new(
138 directory => '/path/to/MyApp',
139 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
144 my @changed_files = $watcher->watch();
149 This class monitors a directory of files for changes made to any file
150 matching a regular expression. It correctly handles new files added to the
151 application as well as files that are deleted.
155 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
157 Creates a new Watcher object.
159 =head2 find_changed_files
161 Returns a list of files that have been added, deleted, or changed
162 since the last time watch was called. Each element returned is a hash
163 reference with two keys. The C<file> key contains the filename, and
164 the C<status> key contains one of "modified", "added", or "deleted".
168 L<Catalyst>, L<Catalyst::Restarter>, <File::Modified>
172 Catalyst Contributors, see Catalyst.pm
176 This program is free software, you can redistribute it and/or modify
177 it under the same terms as Perl itself.