Created
February 5, 2009 03:48
-
-
Save tokuhirom/58538 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use strict; | |
use warnings; | |
use File::Spec; | |
use FindBin; | |
use Path::Class; | |
use File::Modified; | |
use POSIX ':sys_wait_h'; | |
# ------------------------------------------------------------------------- | |
# configuration part | |
my @WATCH_DIRS = dir($FindBin::Bin, '..', 'lib'); | |
my $REGEX = qr/\.pm$/; | |
# ------------------------------------------------------------------------- | |
# code part | |
&main; exit; | |
sub main { | |
my @child = @ARGV; | |
my $files = File::Modified->new( | |
files => [ get_file_list(), $0 ] | |
); | |
my $pid = spawn_child(@child); | |
while (1) { | |
if (my @changes = $files->changed) { | |
for my $fname (@changes) { | |
print "$fname was changed\n"; | |
} | |
$files->update; | |
kill 15, $pid; | |
waitpid $pid, WIFEXITED; | |
$pid = spawn_child(@child); | |
} | |
sleep 1; | |
} | |
} | |
sub spawn_child { | |
my @child = @_; | |
if (my $pid = fork()) { | |
# parent(watcher) | |
return $pid; | |
} elsif ($pid == 0) { | |
# child(application server) | |
exec @child; | |
die "cannot reach here"; | |
} else { | |
die "cannot fork"; | |
} | |
} | |
sub get_file_list { | |
my @r; | |
for my $dir (@WATCH_DIRS) { | |
$dir->recurse( | |
callback => sub { | |
my $f = shift; | |
if ($f =~ $REGEX) { | |
push @r, $f; | |
} | |
} | |
); | |
} | |
return @r; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment