X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Runtime.git;a=blobdiff_plain;f=lib%2FCatalyst%2FEngine%2FHTTP%2FRestarter.pm;h=0cae24353ed0ae15167680f3b2327272d540ae86;hp=56290b8d93cd20c79833ab2f110a81dcba68b6df;hb=2f3812528068bc1d9f7840067f0c03d36cd47e6d;hpb=65586a18685daa023a1a623cf228943e4a4f830d diff --git a/lib/Catalyst/Engine/HTTP/Restarter.pm b/lib/Catalyst/Engine/HTTP/Restarter.pm index 56290b8..0cae243 100644 --- a/lib/Catalyst/Engine/HTTP/Restarter.pm +++ b/lib/Catalyst/Engine/HTTP/Restarter.pm @@ -1,51 +1,75 @@ package Catalyst::Engine::HTTP::Restarter; -use strict; -use warnings; -use base 'Catalyst::Engine::HTTP'; +use Moose; +extends 'Catalyst::Engine::HTTP'; + use Catalyst::Engine::HTTP::Restarter::Watcher; -use NEXT; -sub run { +around run => sub { + my $orig = shift; my ( $self, $class, $port, $host, $options ) = @_; $options ||= {}; # Setup restarter - my $restarter; - my $parent = $$; - - unless ( $restarter = fork ) { + unless ( my $restarter = fork ) { # Prepare close STDIN; close STDOUT; - + my $watcher = Catalyst::Engine::HTTP::Restarter::Watcher->new( - directory => File::Spec->catdir( $FindBin::Bin, '..' ), + directory => ( + $options->{restart_directory} || + File::Spec->catdir( $FindBin::Bin, '..' ) + ), + follow_symlinks => $options->{follow_symlinks}, regex => $options->{restart_regex}, delay => $options->{restart_delay}, ); + $host ||= '127.0.0.1'; while (1) { + # poll for changed files my @changed_files = $watcher->watch(); - + # check if our parent process has died - exit if ( getppid == 1 ); - + exit if $^O ne 'MSWin32' and getppid == 1; + # Restart if any files have changed - if ( @changed_files ) { + if (@changed_files) { my $files = join ', ', @changed_files; print STDERR qq/File(s) "$files" modified, restarting\n\n/; - kill( 1, $parent ); + + require IO::Socket::INET; + require HTTP::Headers; + require HTTP::Request; + + my $client = IO::Socket::INET->new( + PeerAddr => $host, + PeerPort => $port + ) + or die "Can't create client socket (is server running?): ", + $!; + + # build the Kill request + my $req = + HTTP::Request->new( 'RESTART', '/', + HTTP::Headers->new( 'Connection' => 'close' ) ); + $req->protocol('HTTP/1.0'); + + $client->send( $req->as_string ) + or die "Can't send restart instruction: ", $!; + $client->close(); exit; } } } - return $self->NEXT::run( $class, $port, $host, $options ); -} + return $self->$orig( $class, $port, $host, $options ); + no Moose; +}; 1; __END__ @@ -65,11 +89,7 @@ and restart the server when any changes are detected. =head1 METHODS -=over 4 - -=item run - -=back +=head2 run =head1 SEE ALSO @@ -78,11 +98,7 @@ L. =head1 AUTHORS -Sebastian Riedel, - -Dan Kubb, - -Andy Grundman, +Catalyst Contributors, see Catalyst.pm =head1 THANKS