1 package Catalyst::Engine::HTTP::Restarter;
3 use Moose::Util qw/find_meta/;
4 use namespace::clean -except => 'meta';
6 extends 'Catalyst::Engine::HTTP';
8 use Catalyst::Engine::HTTP::Restarter::Watcher;
12 my ( $self, $class, $port, $host, $options ) = @_;
17 unless ( my $restarter = fork ) {
23 $self->_make_components_mutable($class);
25 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
27 $options->{restart_directory} ||
28 File::Spec->catdir( $FindBin::Bin, '..' )
30 follow_symlinks => $options->{follow_symlinks},
31 regex => $options->{restart_regex},
32 delay => $options->{restart_delay},
35 $host ||= '127.0.0.1';
38 # poll for changed files
39 my @changed_files = $watcher->watch();
41 # check if our parent process has died
42 exit if $^O ne 'MSWin32' and getppid == 1;
44 # Restart if any files have changed
46 my $files = join ', ', @changed_files;
47 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
49 require IO::Socket::INET;
50 require HTTP::Headers;
51 require HTTP::Request;
53 my $client = IO::Socket::INET->new(
57 or die "Can't create client socket (is server running?): ",
60 # build the Kill request
62 HTTP::Request->new( 'RESTART', '/',
63 HTTP::Headers->new( 'Connection' => 'close' ) );
64 $req->protocol('HTTP/1.0');
66 $client->send( $req->as_string )
67 or die "Can't send restart instruction: ", $!;
74 return $self->$orig( $class, $port, $host, $options );
77 # Naive way of trying to avoid Moose blowing up when you re-require components
78 # which have been made immutable.
79 sub _make_components_mutable {
80 my ($self, $class) = @_;
82 my @metas = map { find_meta($_) } ($class, map { blessed($_) } values %{ $class->components });
84 foreach my $meta (@metas) {
85 $meta->make_mutable if $meta->is_immutable;
94 Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
98 script/myapp_server.pl -restart
102 The Restarter engine will monitor files in your application for changes
103 and restart the server when any changes are detected.
111 L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
116 Catalyst Contributors, see Catalyst.pm
120 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
124 This program is free software, you can redistribute it and/or modify it under
125 the same terms as Perl itself.