[Catalyst-commits] r7075 - in trunk/Catalyst-Engine-HTTP-Sprocket: . lib/Catalyst/Engine/HTTP/Sprocket

andyg at dev.catalyst.perl.org andyg at dev.catalyst.perl.org
Sat Oct 27 19:25:22 GMT 2007


Author: andyg
Date: 2007-10-27 19:25:21 +0100 (Sat, 27 Oct 2007)
New Revision: 7075

Modified:
   trunk/Catalyst-Engine-HTTP-Sprocket/Makefile.PL
   trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm
Log:
Restart support for Sprocket engine

Modified: trunk/Catalyst-Engine-HTTP-Sprocket/Makefile.PL
===================================================================
--- trunk/Catalyst-Engine-HTTP-Sprocket/Makefile.PL	2007-10-27 02:58:50 UTC (rev 7074)
+++ trunk/Catalyst-Engine-HTTP-Sprocket/Makefile.PL	2007-10-27 18:25:21 UTC (rev 7075)
@@ -3,7 +3,7 @@
 name 'Catalyst-Engine-HTTP-Sprocket';
 all_from 'lib/Catalyst/Engine/HTTP/Sprocket.pm';
 
-requires 'Catalyst::Runtime';
+requires 'Catalyst::Runtime' => '5.7011';
 requires 'MIME::Types';
 requires 'Sprocket';
 

Modified: trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm
===================================================================
--- trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm	2007-10-27 02:58:50 UTC (rev 7074)
+++ trunk/Catalyst-Engine-HTTP-Sprocket/lib/Catalyst/Engine/HTTP/Sprocket/Server.pm	2007-10-27 18:25:21 UTC (rev 7075)
@@ -17,6 +17,7 @@
 use Sprocket::AIO;
 use URI::Escape qw(uri_unescape);
 
+use Catalyst::Engine::HTTP::Restarter::Watcher;
 use Catalyst::Engine::HTTP::Sprocket::Worker;
 
 sub DEBUG ()         { $ENV{CATALYST_POE_DEBUG} || 0 }
@@ -69,12 +70,15 @@
         object_states => [
             $self => [ qw(
                 _start
+                shutdown
                 
                 spawn_child
                 kill_child
                 watchdog
                 
-                sig_int
+                check_restart
+                restart
+                
                 sig_child
                 dump_state
                 
@@ -94,9 +98,13 @@
     
     $kernel->alias_set( "$self" );
     
-    # Shut down cleanly on INT
-    $kernel->sig( INT => 'sig_int' );
+    # Shut down cleanly on INT and TERM
+    $kernel->sig( INT  => 'shutdown' );
+    $kernel->sig( TERM => 'shutdown' );
     
+    # restart on HUP
+    $kernel->sig( HUP => 'restart' );
+    
     # dump state on USR1
     if ( DEBUG ) {
         $kernel->sig( USR1 => 'dump_state' );
@@ -110,6 +118,12 @@
     # watchdog process, starts/kills children as necessary
     $kernel->delay_set( watchdog => WATCHDOG_TIME );
     
+    # Init restarter
+    if ( $self->{config}->{restart} ) {
+        my $delay = $self->{config}->{restart_delay} || 1;
+        $kernel->delay_set( check_restart => $delay );
+    }
+    
     return $self;
 }
 
@@ -167,10 +181,10 @@
     delete $self->{stats_child}->{ $wheel_id };
 }
 
-sub sig_int {
+sub shutdown {
     my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
     
-    DEBUG && warn "SIGINT received, shutting down\n";
+    DEBUG && warn "Shutting down\n";
     
     for my $child ( values %{ $self->{children} } ) {
         $child->kill();
@@ -178,7 +192,15 @@
     
     $kernel->sig_handled();
     
-    exit;
+    delete $self->{children};
+    delete $self->{child_busy};
+    
+    # Shut down the Sprocket server
+    $kernel->call( $self->{session_id} => 'shutdown' );
+    
+    $kernel->alias_remove( "$self" );
+    
+    exit unless $self->{no_exit};
 }
 
 sub sig_child {
@@ -256,6 +278,54 @@
     $kernel->delay_set( watchdog => WATCHDOG_TIME );
 }
 
+sub check_restart {
+    my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
+    
+    my $config = $self->{config};
+    
+    # Init watcher object with no delay
+    if ( !$self->{watcher} ) {        
+        $self->{watcher} = Catalyst::Engine::HTTP::Restarter::Watcher->new(
+            directory => ( 
+                $config->{restart_directory} || 
+                File::Spec->catdir( $FindBin::Bin, '..' )
+            ),
+            regex     => $config->{restart_regex},
+        );
+    }
+    
+    my @changed_files = $self->{watcher}->watch();
+    
+    # Restart if any files have changed
+    if ( @changed_files ) {
+        my $files = join ', ', @changed_files;
+        print STDERR qq/File(s) "$files" modified, restarting\n\n/;
+        
+        $kernel->yield( 'restart' );
+    }
+    else {
+        # Schedule next check
+        $kernel->delay_set( check_restart => $config->{restart_delay} );
+    }
+}
+
+sub restart {
+    my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
+    
+    # Tell the shutdown handler to not exit, exec will take care of that
+    $self->{no_exit} = 1;
+    
+    $kernel->call( $_[SESSION], 'shutdown' );
+    
+    ### if the standalone server was invoked with perl -I .. we will loose
+    ### those include dirs upon re-exec. So add them to PERL5LIB, so they
+    ### are available again for the exec'ed process --kane
+    use Config;
+    $ENV{PERL5LIB} .= join $Config{path_sep}, @INC;
+    
+    exec $^X . ' "' . $0 . '" ' . join( ' ', @{ $self->{config}->{argv} } );
+}
+
 ### Sprocket server methods
 
 sub local_connected {




More information about the Catalyst-commits mailing list