1 package Catalyst::Restarter;
4 use Cwd qw( abs_path );
5 use File::ChangeNotify;
7 use namespace::autoclean;
17 isa => 'File::ChangeNotify::Watcher',
30 defined $ENV{CATALYST_RESTARTER}
31 ? $ENV{CATALYST_RESTARTER}
36 $subclass = 'Catalyst::Restarter::' . $subclass;
48 delete $p->{start_sub};
50 $p->{filter} ||= qr/(?:\/|^)(?!\.\#).+(?:\.yml$|\.yaml$|\.conf|\.pm)$/;
51 $p->{directories} ||= abs_path( File::Spec->catdir( $FindBin::Bin, '..' ) );
53 # We could make this lazily, but this lets us check that we
54 # received valid arguments for the watcher up front.
55 $self->_watcher( File::ChangeNotify->instantiate_watcher( %{$p} ) );
61 $self->_fork_and_start;
63 return unless $self->_child;
65 $self->_restart_on_changes;
68 sub _restart_on_changes {
71 my @events = $self->_watcher->wait_for_events();
72 $self->_handle_events(@events);
80 print STDERR "Saw changes to the following files:\n";
82 for my $event (@events) {
83 my $path = $event->path();
84 my $type = $event->type();
86 print STDERR " - $path ($type)\n";
90 print STDERR "Attempting to restart the server\n\n";
94 $self->_fork_and_start;
96 $self->_restart_on_changes;
105 __PACKAGE__->meta->make_immutable;
113 Catalyst::Restarter - Uses File::ChangeNotify to check for changed files and restart the server
117 my $class = Catalyst::Restarter->pick_subclass;
119 my $restarter = $class->new(
120 directories => '/path/to/MyApp',
121 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
122 start_sub => sub { ... }
125 $restarter->run_and_watch;
129 This is the base class for all restarters, and it also provide
130 functionality for picking an appropriate restarter subclass for a
133 This class uses L<File::ChangeNotify> to watch one or more directories
134 of files and restart the Catalyst server when any of those files
141 Returns the name of an appropriate subclass for the given platform.
143 =head2 new ( start_sub => sub { ... }, ... )
145 This method creates a new restarter object, but should be called on a
146 subclass, not this class.
148 The "start_sub" argument is required. This is a subroutine reference
149 that can be used to start the Catalyst server.
153 This method forks, starts the server in a child process, and then
154 watched for changed files in the parent. When files change, it kills
155 the child, forks again, and starts a new server.
159 L<Catalyst>, <File::ChangeNotify>
163 Catalyst Contributors, see Catalyst.pm
167 This program is free software, you can redistribute it and/or modify
168 it under the same terms as Perl itself.