Tripletail
view release on metacpan or search on metacpan
lib/Tripletail/FileSentinel.pm view on Meta::CPAN
if(!$_INSTANCE) {
$_INSTANCE = $class->_new(@_);
}
$_INSTANCE;
}
sub __install {
my $this = shift;
$TL->setHook('postRequest', _HOOK_PRIORITY, sub { $this->__postRequest });
}
sub watch {
my $this = shift;
my $fpath = shift;
$fpath = File::Spec->rel2abs($fpath);
my $lastmod = (stat($fpath))[9];
if(!$lastmod) {
die __PACKAGE__."#watch: failed to stat [$fpath]: $! (ãã¡ã¤ã«ãstatã§ãã¾ããã§ãã)\n";
}
if ($TL->INI->get(TL => filelog => 'update') eq 'full') {
$TL->log(
__PACKAGE__,
sprintf(
'Watching file [%s]: last modified time: [%s]',
$fpath,
scalar(localtime($lastmod))
)
);
}
$this->{lastmod}{$fpath} = $lastmod;
$this;
}
sub autoWatch {
my $this = shift;
my $include = shift;
my $exclude = shift;
local $_;
return unless defined $include || defined $exclude;
my @targets = values %INC;
@targets = grep m{$include}, @targets if $include;
@targets = grep !m{$exclude}, @targets if $exclude;
foreach ( @targets ) {
$TL->log(__PACKAGE__, " autoWatch(): $_");
$this->watch($_);
}
$this;
}
sub _new {
my $class = shift;
my $this = bless {} => $class;
$this->{lastmod} = {}; # {file path => epoch}
if(defined($0)) {
$this->watch($0);
}
my $filename = $TL->INI->getFilePath;
if(defined($filename)) {
$this->watch($filename);
}
$this->autoWatch(
$TL->INI->get(FileSentinel => autowatch_include => undef),
$TL->INI->get(FileSentinel => autowatch_exclude => undef)
);
$this;
}
sub __postRequest {
my $this = shift;
while(my ($fpath, $lastmod) = each %{$this->{lastmod}}) {
my $current = (stat($fpath))[9];
if(!$current) {
die __PACKAGE__."#postRequest: failed to stat [$fpath]: $! (ãã¡ã¤ã«ãstatã§ãã¾ããã§ãã)\n";
}
if($current != $lastmod) {
my $time = localtime($current);
$TL->log("File Update: file [$fpath] has been updated at [$time]");
$TL->_fcgi_restart(1);
}
}
}
__END__
=encoding utf-8
=head1 NAME
Tripletail::FileSentinel - ãã¡ã¤ã«ã®æ´æ°ã®ç£è¦
=head1 SYNOPSIS
my $fsenti = $TL->getFileSentinel;
$fsenti->watch('/etc/passwd');
$fsenti->watch('/var/log/wtmp');
=head1 DESCRIPTION
FCGI ã¢ã¼ãã®éã«ãç¹å®ã®ãã¡ã¤ã«ãæ´æ°ããããã©ããã調ã¹ã¦ã
æ´æ°ãæ¤åºããå ´åã«ããã»ã¹ãåèµ·åããããã®ã¢ã¸ã¥ã¼ã«ã¯
FCGI ã¢ã¼ãã§èªåçã«ä½¿ç¨ãããFCGI ã¢ã¼ãã§ãªãæã«ã¯ä½¿ç¨ãããªãã
=head2 METHODS
( run in 0.881 second using v1.01-cache-2.11-cpan-2e0ccfb7a10 )