Skip to content

Instantly share code, notes, and snippets.

@tokuhirom
Created February 5, 2009 03:48
Show Gist options
  • Save tokuhirom/58538 to your computer and use it in GitHub Desktop.
Save tokuhirom/58538 to your computer and use it in GitHub Desktop.
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