1 package Catalyst::Restarter;
6 use namespace::clean -except => 'meta';
16 isa => 'Catalyst::Watcher',
28 delete $p->{restart_sub};
30 # We could make this lazily, but this lets us check that we
31 # received valid arguments for the watcher up front.
32 $self->_watcher( Catalyst::Watcher->instantiate_subclass( %{$p} ) );
38 $self->_fork_and_start;
40 return unless $self->_child;
42 $self->_restart_on_changes;
48 if ( my $pid = fork ) {
52 $self->restart_sub->();
56 sub _restart_on_changes {
59 $self->_watcher->watch($self);
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";
74 $self->_fork_and_start;
76 $self->_restart_on_changes;
82 return unless $self->_child;
84 return unless kill 0, $self->_child;
86 local $SIG{CHLD} = 'IGNORE';
87 unless ( kill 'INT', $self->_child ) {
88 # The kill 0 thing does not work on Windows, but the restarter
89 # seems to work fine on Windows with this hack.
90 return if $^O eq 'MSWin32';
91 die "Cannot send INT signal to ", $self->_child, ": $!";
101 __PACKAGE__->meta->make_immutable;
109 Catalyst::Restarter - Uses Catalyst::Watcher to check for changed files and restart the server
113 my $watcher = Catalyst::Watcher->new(
114 directory => '/path/to/MyApp',
115 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
120 my @changed_files = $watcher->watch();
125 This class monitors a directory of files for changes made to any file
126 matching a regular expression. It correctly handles new files added to the
127 application as well as files that are deleted.
131 =head2 new ( directory => $path [, regex => $regex, delay => $delay ] )
133 Creates a new Watcher object.
135 =head2 find_changed_files
137 Returns a list of files that have been added, deleted, or changed
138 since the last time watch was called. Each element returned is a hash
139 reference with two keys. The C<file> key contains the filename, and
140 the C<status> key contains one of "modified", "added", or "deleted".
144 L<Catalyst>, L<Catalyst::Restarter>, <File::Modified>
148 Catalyst Contributors, see Catalyst.pm
152 This program is free software, you can redistribute it and/or modify
153 it under the same terms as Perl itself.