1 package Catalyst::Restarter;
5 use File::ChangeNotify;
6 use namespace::clean -except => 'meta';
16 isa => 'File::ChangeNotify::Watcher',
28 delete $p->{restart_sub};
30 $p->{filter} ||= qr/(?:\/|^)(?!\.\#).+(?:\.yml$|\.yaml$|\.conf|\.pm)$/;
32 # We could make this lazily, but this lets us check that we
33 # received valid arguments for the watcher up front.
34 $self->_watcher( File::ChangeNotify->instantiate_watcher( %{$p} ) );
40 $self->_fork_and_start;
42 return unless $self->_child;
44 $self->_restart_on_changes;
50 if ( my $pid = fork ) {
54 $self->restart_sub->();
58 sub _restart_on_changes {
61 my @events = $self->_watcher->wait_for_events();
62 $self->_handle_events(@events);
70 print STDERR "Saw changes to the following files:\n";
72 for my $event (@events) {
73 my $path = $event->path();
74 my $type = $event->type();
76 print STDERR " - $path ($type)\n";
80 print STDERR "Attempting to restart the server\n\n";
84 $self->_fork_and_start;
86 $self->_restart_on_changes;
92 return unless $self->_child;
94 return unless kill 0, $self->_child;
96 local $SIG{CHLD} = 'IGNORE';
97 unless ( kill 'INT', $self->_child ) {
98 # The kill 0 thing does not work on Windows, but the restarter
99 # seems to work fine on Windows with this hack.
100 return if $^O eq 'MSWin32';
101 die "Cannot send INT signal to ", $self->_child, ": $!";
111 __PACKAGE__->meta->make_immutable;
119 Catalyst::Restarter - Uses Catalyst::Watcher to check for changed files and restart the server
123 my $watcher = Catalyst::Watcher->new(
124 directory => '/path/to/MyApp',
125 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
130 my @changed_files = $watcher->watch();
135 This class monitors a directory of files for changes made to any file
136 matching a regular expression. It correctly handles new files added to the
137 application as well as files that are deleted.
141 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
143 Creates a new Watcher object.
145 =head2 find_changed_files
147 Returns a list of files that have been added, deleted, or changed
148 since the last time watch was called. Each element returned is a hash
149 reference with two keys. The C<file> key contains the filename, and
150 the C<status> key contains one of "modified", "added", or "deleted".
154 L<Catalyst>, L<Catalyst::Restarter>, <File::Modified>
158 Catalyst Contributors, see Catalyst.pm
162 This program is free software, you can redistribute it and/or modify
163 it under the same terms as Perl itself.