From 6dccf76849c9bf48b66aa2e0b1ae7251cf719f00 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 21 Aug 2022 12:53:13 +0100 Subject: [PATCH] prefork-interp: autoreload check Signed-off-by: Ian Jackson --- pm/Proc/Prefork/Interp.pm | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/pm/Proc/Prefork/Interp.pm b/pm/Proc/Prefork/Interp.pm index d11051f..1406351 100644 --- a/pm/Proc/Prefork/Interp.pm +++ b/pm/Proc/Prefork/Interp.pm @@ -11,6 +11,7 @@ use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK); use IO::FDPass; use POSIX qw(_exit setsid :sys_wait_h :errno_h :signal_h); use Sys::Syslog qw(openlog syslog LOG_INFO LOG_ERR LOG_WARNING); +use Time::HiRes qw(); our $logger; @@ -156,6 +157,19 @@ sub protocol_exchange () { } } +sub autoreload_check ($) { + my ($f) = @_; + my @s = Time::HiRes::stat($f); + if (!@s) { + $!==ENOENT or fail_log("autoreload check: stat failed: $f: $!"); + return; + } + if ($s[9] > $startup_mtime) { + syslog(LOG_INFO, "$0 prefork [$$]: reloading; due to $f"); + _exit(0); + } +} + sub initialisation_complete { my %opts = @_; @@ -271,7 +285,14 @@ sub initialisation_complete { fail_log("watcher stderr read: $!"); } - # TODO stat checking, quit here if we are stale + if (%opts{autoreload_inc} // 1) { + foreach my $f (values %INC) { + autoreload_check($f); + } + } + foreach my $f (@{ %opts{autoreload_extra} // [] }) { + autoreload_check($f); + } # Anything to accept ? if (accept(CALL, LISTEN)) { -- 2.30.2