[Catalyst-commits] r10017 - Catalyst-Devel/1.00/branches/improved-restarter/lib/Catalyst/Watcher

autarch at dev.catalyst.perl.org autarch at dev.catalyst.perl.org
Tue May 5 19:29:21 GMT 2009


Author: autarch
Date: 2009-05-05 19:29:21 +0000 (Tue, 05 May 2009)
New Revision: 10017

Modified:
   Catalyst-Devel/1.00/branches/improved-restarter/lib/Catalyst/Watcher/Inotify.pm
Log:
Make the inotify watcher actually work.

It was totally broken before, but I _thought_ it was working because
of a combination of stupidity, and the fact that I'd improved the
FileModified code made it less obvious that I was using the old
watcher code.


Modified: Catalyst-Devel/1.00/branches/improved-restarter/lib/Catalyst/Watcher/Inotify.pm
===================================================================
--- Catalyst-Devel/1.00/branches/improved-restarter/lib/Catalyst/Watcher/Inotify.pm	2009-05-05 19:28:34 UTC (rev 10016)
+++ Catalyst-Devel/1.00/branches/improved-restarter/lib/Catalyst/Watcher/Inotify.pm	2009-05-05 19:29:21 UTC (rev 10017)
@@ -2,15 +2,18 @@
 
 use Moose;
 
+use File::Find;
 use Linux::Inotify2;
 use namespace::clean -except => 'meta';
 
 extends 'Catalyst::Watcher';
 
 has _inotify => (
-    is         => 'rw',
-    isa        => 'Linux::Inotify2',
-    lazy_build => 1,
+    is       => 'rw',
+    isa      => 'Linux::Inotify2',
+    default  => sub { Linux::Inotify2->new },
+    init_arg => undef,
+
 );
 
 has _mask => (
@@ -19,6 +22,17 @@
     lazy_build => 1,
 );
 
+sub BUILD {
+    my $self = shift;
+
+    # If this is done via a lazy_build then the call to
+    # ->_add_directory ends up causing endless recursion when it calls
+    # ->_inotify itself.
+    $self->_add_directory($_) for @{ $self->directories };
+
+    return $self;
+}
+
 sub watch {
     my $self      = shift;
     my $restarter = shift;
@@ -33,6 +47,8 @@
 sub _wait_for_events {
     my $self = shift;
 
+    my $regex = $self->regex;
+
     while (1) {
         # This is a blocking read, so it will not return until
         # something happens. The restarter will end up calling ->watch
@@ -40,16 +56,16 @@
         my @events = $self->_inotify->read;
 
         my @interesting;
-        for my $event ( grep { $_->mask | IN_ISDIR } @events ) {
-            if ( $event->mask | IM_CREATE ) {
+        for my $event (@events) {
+            if ( ( $event->IN_CREATE && $event->IN_ISDIR ) ) {
                 $self->_add_directory( $event->fullname );
                 push @interesting, $event;
             }
-            elsif ( $event->mask | IM_DELETE_SELF ) {
+            elsif ( $event->IN_DELETE_SELF ) {
                 $event->w->cancel;
                 push @interesting, $event;
             }
-            elsif ( $event->name =~ /$regex/ ) {
+            elsif ( $event->fullname =~ /$regex/ ) {
                 push @interesting, $event;
             }
         }
@@ -58,16 +74,6 @@
     }
 }
 
-sub _build__inotify {
-    my $self = shift;
-
-    my $inotify = Linux::Inotify2->new();
-
-    $self->_add_directory($_) for @{ $self->directories };
-
-    return $inotify;
-}
-
 sub _build__mask {
     my $self = shift;
 
@@ -92,7 +98,7 @@
             follow_fast => $self->follow_symlinks ? 1 : 0,
             no_chdir    => 1
         },
-        $dir;
+        $dir
     );
 }
 
@@ -100,14 +106,14 @@
     my $self  = shift;
     my $event = shift;
 
-    my %change = { file => $event->fullname };
-    if ( $event->mask() | IN_CREATE || $event->mask() ) {
+    my %change = ( file => $event->fullname );
+    if ( $event->IN_CREATE ) {
         $change{status} = 'added';
     }
-    elsif ( $event->mask() | IN_MODIFY ) {
+    elsif ( $event->IN_MODIFY ) {
         $change{status} = 'modified';
     }
-    elsif ( $event->mask() | IN_DELETE || $event->mask() ) {
+    elsif ( $event->IN_DELETE ) {
         $change{status} = 'deleted';
     }
     else {




More information about the Catalyst-commits mailing list