Merge branch 'improved-restarter'
Florian Ragwitz [Wed, 13 May 2009 18:03:45 +0000 (18:03 +0000)]
Conflicts:
Changes
lib/Catalyst/Devel.pm

1  2 
Changes
lib/Catalyst/Devel.pm
lib/Catalyst/Helper.pm

diff --combined Changes
+++ b/Changes
@@@ -1,9 -1,30 +1,33 @@@
  This file documents the revision history for Perl extension Catalyst-Devel.
  
 -1.14_02   2009-05-11 04:29
 +        - Remove -p option for pid file, and restore it to the port number,
 +          as it should be -port|-p (t0m)
 +        - Document -pidfile in myapp_server.pl POD (t0m)
 +
++1.14_02 2009-05-11 04:29
+         - Small changes to work with File::ChangeNotify 0.03.
 -1.14_01   2009-05-11 03:45
++1.14_01 2009-05-11 03:45
+         - This release moves the restarter functionality into a new
+           module Catalyst::Restarter, that is not a Catalyst Engine
+           subclass.
+           The new restarter is simpler and more reliable, because it
+           does not try to test if a changed piece of code can be
+           compiled. It simply restarts the server whenever it detects
+           changes. This also makes it much faster.
+           However, this does mean that the server can simply die when
+           a changed file cannot compile. This is different than the
+           old behavior, where the server remained running without
+           incorporating the change.
+           Finally, the new restarter relies on File::ChangeNotify,
+           which is designed to accomodate per-OS methods of detecting
+           changed files. As of this writing, it currently ships with a
+           file watcher that uses inotify on Linux systems, and a
+           default fallback class that does everything in pure Perl.
 -
  1.13    2009-05-11 02:50
          - add [-pidfile|-p] option for myapp_server.pl (caelum)
          - Bump dependency on Config::General (caelum)
diff --combined lib/Catalyst/Devel.pm
@@@ -3,9 -3,11 +3,11 @@@ package Catalyst::Devel
  use strict;
  use warnings;
  
- our $VERSION             = '1.13';
+ our $VERSION             = '1.14_02';
 -our $CATALYST_SCRIPT_GEN = 35;
 +our $CATALYST_SCRIPT_GEN = 36;
  
+ $VERSION = eval $VERSION;
  =head1 NAME
  
  Catalyst::Devel - Catalyst Development Tools
diff --combined lib/Catalyst/Helper.pm
@@@ -648,7 -648,7 +648,7 @@@ package [% name %]
  use strict;
  use warnings;
  
 -use Catalyst::Runtime '5.70';
 +use Catalyst::Runtime 5.70;
  
  # Set flags and add plugins for the application
  #
@@@ -969,13 -969,14 +969,14 @@@ my $host              = undef
  my $port              = $ENV{[% appenv %]_PORT} || $ENV{CATALYST_PORT} || 3000;
  my $keepalive         = 0;
  my $restart           = $ENV{[% appenv %]_RELOAD} || $ENV{CATALYST_RELOAD} || 0;
  my $background        = 0;
  my $pidfile           = undef;
  
+ my $check_interval;
+ my $file_regex;
+ my $watch_directory;
+ my $follow_symlinks;
  my @argv = @ARGV;
  
  GetOptions(
      'port=s'              => \$port,
      'keepalive|k'         => \$keepalive,
      'restart|r'           => \$restart,
-     'restartdelay|rd=s'   => \$restart_delay,
-     'restartregex|rr=s'   => \$restart_regex,
-     'restartdirectory=s@' => \$restart_directory,
+     'restartdelay|rd=s'   => \$check_interval,
+     'restartregex|rr=s'   => \$file_regex,
+     'restartdirectory=s@' => \$watch_directory,
      'followsymlinks'      => \$follow_symlinks,
      'background'          => \$background,
 -    'pidfile|p=s'         => \$pidfile,
 +    'pidfile=s'           => \$pidfile,
  );
  
  pod2usage(1) if $help;
  
- if ( $restart && $ENV{CATALYST_ENGINE} eq 'HTTP' ) {
-     $ENV{CATALYST_ENGINE} = 'HTTP::Restarter';
- }
  if ( $debug ) {
      $ENV{CATALYST_DEBUG} = 1;
  }
  
- # This is require instead of use so that the above environment
- # variables can be set at runtime.
- require [% name %];
- [% name %]->run( $port, $host, {
-     argv              => \@argv,
-     'fork'            => $fork,
-     keepalive         => $keepalive,
-     restart           => $restart,
-     restart_delay     => $restart_delay,
-     restart_regex     => qr/$restart_regex/,
-     restart_directory => $restart_directory,
-     follow_symlinks   => $follow_symlinks,
-     background        => $background,
-     pidfile           => $pidfile,
- } );
+ # If we load this here, then in the case of a restarter, it does not
+ # need to be reloaded for each restart.
+ require Catalyst;
+ # If this isn't done, then the Catalyst::Devel tests for the restarter
+ # fail.
+ $| = 1 if $ENV{HARNESS_ACTIVE};
+ my $runner = sub {
+     # This is require instead of use so that the above environment
+     # variables can be set at runtime.
+     require [% name %];
+     [% name %]->run(
+         $port, $host,
+         {
+             argv       => \@argv,
+             'fork'     => $fork,
+             keepalive  => $keepalive,
+             background => $background,
+             pidfile    => $pidfile,
+         }
+     );
+ };
+ if ( $restart ) {
+     require Catalyst::Restarter;
+     die "Cannot run in the background and also watch for changed files.\n"
+         if $background;
+     my %args;
+     $args{follow_symlinks} = 1
+         if $follow_symlinks;
+     $args{directories} = $watch_directory
+         if defined $watch_directory;
+     $args{sleep_interval} = $check_interval
+         if defined $check_interval;
+     $args{filter} = qr/$file_regex/
+         if defined $file_regex;
+     my $restarter = Catalyst::Restarter->new(
+         %args,
+         start_sub => $runner,
+     );
+     $restarter->run_and_watch;
+ }
+ else {
+     $runner->();
+ }
  
  1;
  
     -r -restart        restart when files get modified
                        (defaults to false)
     -rd -restartdelay  delay between file checks
+                       (ignored if you have Linux::Inotify2 installed)
     -rr -restartregex  regex match files that trigger
                        a restart when modified
                        (defaults to '\.yml$|\.yaml$|\.conf|\.pm$')
     -follow_symlinks   follow symlinks in search directories
                        (defaults to false. this is a no-op on Win32)
     -background        run the process in the background
 +   -pidfile           specify filename for pid file
 +
   See also:
     perldoc Catalyst::Manual
     perldoc Catalyst::Manual::Intro