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 $restarter = Catalyst::Restarter->new(
119 directories => '/path/to/MyApp',
120 regex => '\.yml$|\.yaml$|\.conf|\.pm$',
121 start_sub => sub { ... }
124 $restarter->run_and_watch;
128 This class uses L<File::ChangeNotify> to watch one or more directories
129 of files and restart the Catalyst server when any of those files
134 =head2 new ( start_sub => sub { ... }, ... )
136 This method creates a new restarter object.
138 The "start_sub" argument is required. This is a subroutine reference
139 that can be used to start the Catalyst server.
143 This method forks, starts the server in a child process, and then
144 watched for changed files in the parent. When files change, it kills
145 the child, forks again, and starts a new server.
149 L<Catalyst>, <File::ChangeNotify>
153 Catalyst Contributors, see Catalyst.pm
157 This program is free software, you can redistribute it and/or modify
158 it under the same terms as Perl itself.