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 # Best effort if we can't trap compiles..
24 $self->_make_components_mutable($class)
25 if !Catalyst::Engine::HTTP::Restarter::Watcher::DETECT_PACKAGE_COMPILATION();
27 my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new(
29 $options->{restart_directory} ||
30 File::Spec->catdir( $FindBin::Bin, '..' )
32 follow_symlinks => $options->{follow_symlinks},
33 regex => $options->{restart_regex},
34 delay => $options->{restart_delay},
37 $host ||= '127.0.0.1';
40 # poll for changed files
41 my @changed_files = $watcher->watch();
43 # check if our parent process has died
44 exit if $^O ne 'MSWin32' and getppid == 1;
46 # Restart if any files have changed
48 my $files = join ', ', @changed_files;
49 print STDERR qq/File(s) "$files" modified, restarting\n\n/;
51 require IO::Socket::INET;
52 require HTTP::Headers;
53 require HTTP::Request;
55 my $client = IO::Socket::INET->new(
59 or die "Can't create client socket (is server running?): ",
62 # build the Kill request
64 HTTP::Request->new( 'RESTART', '/',
65 HTTP::Headers->new( 'Connection' => 'close' ) );
66 $req->protocol('HTTP/1.0');
68 $client->send( $req->as_string )
69 or die "Can't send restart instruction: ", $!;
76 return $self->$orig( $class, $port, $host, $options );
79 # Naive way of trying to avoid Moose blowing up when you re-require components
80 # which have been made immutable.
81 sub _make_components_mutable {
82 my ($self, $class) = @_;
84 my @metas = map { find_meta($_) } ($class, map { blessed($_) } values %{ $class->components });
86 foreach my $meta (@metas) {
87 $meta->make_mutable if $meta->is_immutable;
96 Catalyst::Engine::HTTP::Restarter - Catalyst Auto-Restarting HTTP Engine
100 script/myapp_server.pl -restart
104 The Restarter engine will monitor files in your application for changes
105 and restart the server when any changes are detected.
113 L<Catalyst>, L<Catalyst::Engine::HTTP>, L<Catalyst::Engine::CGI>,
118 Catalyst Contributors, see Catalyst.pm
122 Many parts are ripped out of C<HTTP::Server::Simple> by Jesse Vincent.
126 This program is free software, you can redistribute it and/or modify it under
127 the same terms as Perl itself.