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 # We use this loop in order to avoid having _handle_events() call back
79 # into this method. We used to do that, and the end result was that stack
80 # traces became longer and longer with every restart. Using this loop, the
81 # portion of the stack trace that covers this code does not grow.
83 my @events = $self->_watcher->wait_for_events();
84 $self->_handle_events(@events);
93 print STDERR "Saw changes to the following files:\n";
95 for my $event (@events) {
96 my $path = $event->path();
97 my $type = $event->type();
99 print STDERR " - $path ($type)\n";
103 print STDERR "Attempting to restart the server\n\n";
107 $self->_fork_and_start;
116 __PACKAGE__->meta->make_immutable;
124 Catalyst::Restarter - Uses File::ChangeNotify to check for changed files and restart the server
128 my $class = Catalyst::Restarter->pick_subclass;
130 my $restarter = $class->new(
131 directories => '/path/to/MyApp',
132 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
133 start_sub => sub { ... }
136 $restarter->run_and_watch;
140 This is the base class for all restarters, and it also provide
141 functionality for picking an appropriate restarter subclass for a
144 This class uses L<File::ChangeNotify> to watch one or more directories
145 of files and restart the Catalyst server when any of those files
152 Returns the name of an appropriate subclass for the given platform.
154 =head2 new ( start_sub => sub { ... }, ... )
156 This method creates a new restarter object, but should be called on a
157 subclass, not this class.
159 The "start_sub" argument is required. This is a subroutine reference
160 that can be used to start the Catalyst server.
164 This method forks, starts the server in a child process, and then
165 watched for changed files in the parent. When files change, it kills
166 the child, forks again, and starts a new server.
170 L<Catalyst>, <File::ChangeNotify>
174 Catalyst Contributors, see Catalyst.pm
178 This program is free software, you can redistribute it and/or modify
179 it under the same terms as Perl itself.