1 package Catalyst::Restarter;
5 use Cwd qw( abs_path );
6 use File::ChangeNotify;
8 use namespace::clean -except => 'meta';
18 isa => 'File::ChangeNotify::Watcher',
31 defined $ENV{CATALYST_RESTARTER}
32 ? $ENV{CATALYST_RESTARTER}
37 $subclass = 'Catalyst::Restarter::' . $subclass;
49 delete $p->{start_sub};
51 $p->{filter} ||= qr/(?:\/|^)(?!\.\#).+(?:\.yml$|\.yaml$|\.conf|\.pm)$/;
52 $p->{directories} ||= abs_path( File::Spec->catdir( $FindBin::Bin, '..' ) );
54 # We could make this lazily, but this lets us check that we
55 # received valid arguments for the watcher up front.
56 $self->_watcher( File::ChangeNotify->instantiate_watcher( %{$p} ) );
62 $self->_fork_and_start;
64 return unless $self->_child;
66 $self->_restart_on_changes;
69 sub _restart_on_changes {
72 my @events = $self->_watcher->wait_for_events();
73 $self->_handle_events(@events);
81 print STDERR "Saw changes to the following files:\n";
83 for my $event (@events) {
84 my $path = $event->path();
85 my $type = $event->type();
87 print STDERR " - $path ($type)\n";
91 print STDERR "Attempting to restart the server\n\n";
95 $self->_fork_and_start;
97 $self->_restart_on_changes;
106 __PACKAGE__->meta->make_immutable;
114 Catalyst::Restarter - Uses File::ChangeNotify to check for changed files and restart the server
118 my $class = Catalyst::Restarter->pick_subclass;
120 my $restarter = $class->new(
121 directories => '/path/to/MyApp',
122 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
123 start_sub => sub { ... }
126 $restarter->run_and_watch;
130 This is the base class for all restarters, and it also provide
131 functionality for picking an appropriate restarter subclass for a
134 This class uses L<File::ChangeNotify> to watch one or more directories
135 of files and restart the Catalyst server when any of those files
142 Returns the name of an appropriate subclass for the given platform.
144 =head2 new ( start_sub => sub { ... }, ... )
146 This method creates a new restarter object, but should be called on a
147 subclass, not this class.
149 The "start_sub" argument is required. This is a subroutine reference
150 that can be used to start the Catalyst server.
154 This method forks, starts the server in a child process, and then
155 watched for changed files in the parent. When files change, it kills
156 the child, forks again, and starts a new server.
160 L<Catalyst>, <File::ChangeNotify>
164 Catalyst Contributors, see Catalyst.pm
168 This program is free software, you can redistribute it and/or modify
169 it under the same terms as Perl itself.