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 # Avoid "Setting config after setup" error restarting MyApp.pm
24 $class->setup_finished(0);
25 # Best effort if we can't trap compiles..
26 $self->_make_components_mutable($class)
27 if !Catalyst::Engine::HTTP::Restarter::Watcher::DETECT_PACKAGE_COMPILATION;
29 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
31 $options->{restart_directory} ||
32 File::Spec->catdir( $FindBin::Bin, '..' )
34 follow_symlinks => $options->{follow_symlinks},
35 regex => $options->{restart_regex},
36 delay => $options->{restart_delay},
39 $host ||= '127.0.0.1';
42 # poll for changed files
43 my @changed_files = $watcher->watch();
45 # check if our parent process has died
46 exit if $^O ne 'MSWin32' and getppid == 1;
48 # Restart if any files have changed
50 my $files = join ', ', @changed_files;
51 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
53 require IO::Socket::INET;
54 require HTTP::Headers;
55 require HTTP::Request;
57 my $client = IO::Socket::INET->new(
61 or die "Can't create client socket (is server running?): ",
64 # build the Kill request
66 HTTP::Request->new( 'RESTART', '/',
67 HTTP::Headers->new( 'Connection' => 'close' ) );
68 $req->protocol('HTTP/1.0');
70 $client->send( $req->as_string )
71 or die "Can't send restart instruction: ", $!;
78 return $self->$orig( $class, $port, $host, $options );
81 # Naive way of trying to avoid Moose blowing up when you re-require components
82 # which have been made immutable.
83 sub _make_components_mutable {
84 my ($self, $class) = @_;
86 my @metas = map { find_meta($_) } ($class, map { blessed($_) } values %{ $class->components });
88 foreach my $meta (@metas) {
89 $meta->make_mutable if $meta->is_immutable;
98 Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
102 script/myapp_server.pl -restart
106 The Restarter engine will monitor files in your application for changes
107 and restart the server when any changes are detected.
115 L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
120 Catalyst Contributors, see Catalyst.pm
124 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
128 This program is free software, you can redistribute it and/or modify it under
129 the same terms as Perl itself.