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
64 || $event->fullname =~ /$regex/ ) {
65 push @interesting, $event;
69 return @interesting if @interesting;
76 my $mask = IN_MODIFY | IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF;
77 $mask |= IN_DONT_FOLLOW unless $self->follow_symlinks;
89 my $path = File::Spec->rel2abs($File::Find::name);
90 return unless -d $path;
92 $self->_inotify->watch( $path, $self->_mask );
94 follow_fast => $self->follow_symlinks ? 1 : 0,
101 sub _event_to_change {
105 my %change = ( file => $event->fullname );
106 if ( $event->IN_CREATE ) {
107 $change{status} = 'added';
109 elsif ( $event->IN_MODIFY ) {
110 $change{status} = 'modified';
112 elsif ( $event->IN_DELETE ) {
113 $change{status} = 'deleted';
116 $change{status} = 'containing directory modified';
122 __PACKAGE__->meta->make_immutable;
130 Catalyst::Watcher - Watch for changed application files
134 my $watcher = Catalyst::Watcher->new(
135 directory => '/path/to/MyApp',
136 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
141 my @changed_files = $watcher->watch();
146 This class monitors a directory of files for changes made to any file
147 matching a regular expression. It correctly handles new files added to the
148 application as well as files that are deleted.
152 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
154 Creates a new Watcher object.
156 =head2 find_changed_files
158 Returns a list of files that have been added, deleted, or changed
159 since the last time watch was called. Each element returned is a hash
160 reference with two keys. The C<file> key contains the filename, and
161 the C<status> key contains one of "modified", "added", or "deleted".
165 L<Catalyst>, L<Catalyst::Restarter>, <File::Modified>
169 Catalyst Contributors, see Catalyst.pm
173 This program is free software, you can redistribute it and/or modify
174 it under the same terms as Perl itself.