1 package Catalyst::Restarter;
8 use namespace::clean -except => 'meta';
18 isa => 'Catalyst::Watcher',
30 delete $p->{restart_sub};
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( Catalyst::Watcher->new( %{$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 $watcher = $self->_watcher;
64 my @files = $watcher->find_changed_files
67 print STDERR "Saw changes to the following files:\n";
68 print STDERR " - $_->{file} ($_->{status})\n" for @files;
70 print STDERR "Attempting to restart the server\n\n";
72 if ( $self->_child ) {
74 or die "Cannot send INT to child (" . $self->_child . "): $!";
77 $self->_fork_and_start;
79 return unless $self->_child;
86 if ( $self->_child ) {
87 kill 2, $self->_child;
91 __PACKAGE__->meta->make_immutable;
99 Catalyst::Restarter - Uses Catalyst::Watcher to check for changed files and restart the server
103 my $watcher = Catalyst::Watcher->new(
104 directory => '/path/to/MyApp',
105 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
110 my @changed_files = $watcher->watch();
115 This class monitors a directory of files for changes made to any file
116 matching a regular expression. It correctly handles new files added to the
117 application as well as files that are deleted.
121 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
123 Creates a new Watcher object.
125 =head2 find_changed_files
127 Returns a list of files that have been added, deleted, or changed
128 since the last time watch was called. Each element returned is a hash
129 reference with two keys. The C<file> key contains the filename, and
130 the C<status> key contains one of "modified", "added", or "deleted".
134 L<Catalyst>, L<Catalyst::Restarter>, <File::Modified>
138 Catalyst Contributors, see Catalyst.pm
142 This program is free software, you can redistribute it and/or modify
143 it under the same terms as Perl itself.