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
#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);
1;
+=head1 NAME
+
+Object::Remote::WatchDog - alarm-based event loop timeout singleton
+
+=head1 DESCRIPTION
+
+This is a singleton class intended to be used in remote nodes to kill the
+process if the event loop seems to have stalled for longer than the timeout
+specified.
+
+=head1 METHODS
+
+The following are all class methods.
+
+=head2 instance
+
+ my $d = Object::Remote::WatchDog->instance(timeout => 10);
+
+Creates a new watch dog if there wasn't one yet, with the timeout set to the
+specified value. The timeout argument is required. The timeout is immediately
+started by calling C<alarm> with the timeout specified. The C<ALRM> signal is
+replaced with a handler that, when triggered, quits the process with an error.
+
+If there already was a watchdog it just returns that, however in that case the
+timeout value is ignored.
+
+=head2 reset
+
+ Object::Remote::WatchDog->reset;
+
+Calls C<alarm> with the timeout value of the current watch dog singleton to
+reset it. Throws an exception if there is no current singleton. Intended to be
+called repeatedly by the event loop to signal it's still running and not
+stalled.
+
+=head2 shutdown
+
+ Object::Remote::WatchDog->shutdown;
+
+Sets C<alarm> back to 0, thus preventing the C<ALRM> handler from quitting the
+process.
+
+=cut
+