1 package Catalyst::Restarter;
5 use Cwd qw( abs_path );
6 use File::ChangeNotify;
8 use namespace::clean -except => 'meta';
24 isa => 'File::ChangeNotify::Watcher',
37 defined $ENV{CATALYST_RESTARTER}
38 ? $ENV{CATALYST_RESTARTER}
43 $subclass = 'Catalyst::Restarter::' . $subclass;
55 delete $p->{start_sub};
57 $p->{filter} ||= qr/(?:\/|^)(?!\.\#).+(?:\.yml$|\.yaml$|\.conf|\.pm)$/;
58 $p->{directories} ||= abs_path( File::Spec->catdir( $FindBin::Bin, '..' ) );
60 # We could make this lazily, but this lets us check that we
61 # received valid arguments for the watcher up front.
62 $self->_watcher( File::ChangeNotify->instantiate_watcher( %{$p} ) );
68 $self->_fork_and_start;
70 return unless $self->_child;
72 $self->_restart_on_changes;
75 sub _restart_on_changes {
78 my @events = $self->_watcher->wait_for_events();
79 $self->_handle_events(@events);
87 print STDERR "Saw changes to the following files:\n";
89 for my $event (@events) {
90 my $path = $event->path();
91 my $type = $event->type();
93 print STDERR " - $path ($type)\n";
97 print STDERR "Attempting to restart the server\n\n";
101 $self->_fork_and_start;
103 $self->_restart_on_changes;
112 __PACKAGE__->meta->make_immutable;
120 Catalyst::Restarter - Uses File::ChangeNotify to check for changed files and restart the server
124 my $class = Catalyst::Restarter->pick_subclass;
126 my $restarter = $class->new(
127 directories => '/path/to/MyApp',
128 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
129 start_sub => sub { ... }
132 $restarter->run_and_watch;
136 This is the base class for all restarters, and it also provide
137 functionality for picking an appropriate restarter subclass for a
140 This class uses L<File::ChangeNotify> to watch one or more directories
141 of files and restart the Catalyst server when any of those files
148 Returns the name of an appropriate subclass for the given platform.
150 =head2 new ( start_sub => sub { ... }, ... )
152 This method creates a new restarter object, but should be called on a
153 subclass, not this class.
155 The "start_sub" argument is required. This is a subroutine reference
156 that can be used to start the Catalyst server.
160 This method forks, starts the server in a child process, and then
161 watched for changed files in the parent. When files change, it kills
162 the child, forks again, and starts a new server.
166 L<Catalyst>, <File::ChangeNotify>
170 Catalyst Contributors, see Catalyst.pm
174 This program is free software, you can redistribute it and/or modify
175 it under the same terms as Perl itself.