From: Matt S Trout Date: Tue, 19 Mar 2013 19:55:49 +0000 (+0000) Subject: refactor watchdog code a bit X-Git-Tag: v0.003002~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e42ea8c86203339ab8290ec9f6647778f0bdec1c;p=scpubgit%2FObject-Remote.git refactor watchdog code a bit --- diff --git a/lib/Object/Remote/Node.pm b/lib/Object/Remote/Node.pm index dd7b85f..a3f88a5 100644 --- a/lib/Object/Remote/Node.pm +++ b/lib/Object/Remote/Node.pm @@ -30,7 +30,7 @@ sub run { #a command that clears the alarm in all instances - even #if the Object::Remote::Watchdog is not being used if ($args{watchdog_timeout}) { - Object::Remote::WatchDog->new(timeout => $args{watchdog_timeout}); + Object::Remote::WatchDog->instance(timeout => $args{watchdog_timeout}); } else { #reset connection watchdog from the fatnode alarm(0); diff --git a/lib/Object/Remote/WatchDog.pm b/lib/Object/Remote/WatchDog.pm index 3b80f85..bbcd188 100644 --- a/lib/Object/Remote/WatchDog.pm +++ b/lib/Object/Remote/WatchDog.pm @@ -8,13 +8,13 @@ has timeout => ( is => 'ro', required => 1 ); BEGIN { router()->exclude_forwarding; } -around new => sub { - my ($orig, $self, @args) = @_; - our ($WATCHDOG); +sub instance { + my ($class, @args) = @_; - return $WATCHDOG if defined $WATCHDOG; - log_trace { "Constructing new instance of global watchdog" }; - return $WATCHDOG = $self->$orig(@args); + return our $WATCHDOG ||= do { + log_trace { "Constructing new instance of global watchdog" }; + $class->new(@args); + }; }; #start the watchdog @@ -36,9 +36,8 @@ sub BUILD { #invoke at least once per timeout to stop #the watchdog from killing the process sub reset { - our ($WATCHDOG); die "Attempt to reset the watchdog before it was constructed" - unless defined $WATCHDOG; + unless defined our $WATCHDOG; log_debug { "Watchdog has been reset" }; alarm($WATCHDOG->timeout);