X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=scpubgit%2FObject-Remote.git;a=blobdiff_plain;f=lib%2FObject%2FRemote%2FWatchDog.pm;h=bbcd1884077f0efc37ca91ddf9109e2d02f2ddec;hp=170b2b2151447993d9da08a15c64bc7a6341ce45;hb=e42ea8c86203339ab8290ec9f6647778f0bdec1c;hpb=f129bfaf05b1ae0e2e2992cad47a70482dec9885 diff --git a/lib/Object/Remote/WatchDog.pm b/lib/Object/Remote/WatchDog.pm index 170b2b2..bbcd188 100644 --- a/lib/Object/Remote/WatchDog.pm +++ b/lib/Object/Remote/WatchDog.pm @@ -1,45 +1,46 @@ -package Object::Remote::WatchDog; +package Object::Remote::WatchDog; -use Object::Remote::MiniLoop; -use Object::Remote::Logging qw ( :log :dlog ); -use Moo; +use Object::Remote::MiniLoop; +use Object::Remote::Logging qw (:log :dlog router); +use Moo; has timeout => ( is => 'ro', required => 1 ); -around new => sub { - my ($orig, $self, @args) = @_; - our ($WATCHDOG); - - return $WATCHDOG if defined $WATCHDOG; - log_trace { "Constructing new instance of global watchdog" }; - return $WATCHDOG = $self->$orig(@args); +BEGIN { router()->exclude_forwarding; } + +sub instance { + my ($class, @args) = @_; + + return our $WATCHDOG ||= do { + log_trace { "Constructing new instance of global watchdog" }; + $class->new(@args); + }; }; #start the watchdog sub BUILD { my ($self) = @_; - + $SIG{ALRM} = sub { #if the Watchdog is killing the process we don't want any chance of the #process not actually exiting and die could be caught by an eval which - #doesn't do us any good - log_error { sprintf("Watchdog has expired, terminating the process at file %s line %s", __FILE__, __LINE__ + 1); }; - exit(1); - }; - + #doesn't do us any good + log_fatal { "Watchdog has expired, terminating the process" }; + exit(1); + }; + Dlog_debug { "Initializing watchdog with timeout of $_ seconds" } $self->timeout; alarm($self->timeout); } #invoke at least once per timeout to stop -#the watchdog from killing the process +#the watchdog from killing the process sub reset { - our ($WATCHDOG); die "Attempt to reset the watchdog before it was constructed" - unless defined $WATCHDOG; - - log_trace { "Watchdog has been reset" }; - alarm($WATCHDOG->timeout); + unless defined our $WATCHDOG; + + log_debug { "Watchdog has been reset" }; + alarm($WATCHDOG->timeout); } #must explicitly call this method to stop the @@ -48,7 +49,8 @@ sub reset { #it makes sense to still terminate the process sub shutdown { my ($self) = @_; - alarm(0); + log_debug { "Watchdog is shutting down" }; + alarm(0); } 1;