cleanup trailing whitespace ugliness
[scpubgit/Object-Remote.git] / lib / Object / Remote / WatchDog.pm
CommitLineData
55c0d020 1package Object::Remote::WatchDog;
c824fdf3 2
55c0d020 3use Object::Remote::MiniLoop;
5add5e29 4use Object::Remote::Logging qw (:log :dlog router);
55c0d020 5use Moo;
c824fdf3 6
c824fdf3 7has timeout => ( is => 'ro', required => 1 );
8
5add5e29 9BEGIN { router()->exclude_forwarding; }
10
c824fdf3 11around new => sub {
55c0d020 12 my ($orig, $self, @args) = @_;
c824fdf3 13 our ($WATCHDOG);
55c0d020 14
c824fdf3 15 return $WATCHDOG if defined $WATCHDOG;
16 log_trace { "Constructing new instance of global watchdog" };
5add5e29 17 return $WATCHDOG = $self->$orig(@args);
c824fdf3 18};
19
20#start the watchdog
21sub BUILD {
22 my ($self) = @_;
55c0d020 23
f129bfaf 24 $SIG{ALRM} = sub {
25 #if the Watchdog is killing the process we don't want any chance of the
26 #process not actually exiting and die could be caught by an eval which
55c0d020 27 #doesn't do us any good
867e4de5 28 log_fatal { "Watchdog has expired, terminating the process" };
29 exit(1);
55c0d020 30 };
31
c824fdf3 32 Dlog_debug { "Initializing watchdog with timeout of $_ seconds" } $self->timeout;
33 alarm($self->timeout);
34}
35
36#invoke at least once per timeout to stop
55c0d020 37#the watchdog from killing the process
c824fdf3 38sub reset {
39 our ($WATCHDOG);
40 die "Attempt to reset the watchdog before it was constructed"
55c0d020 41 unless defined $WATCHDOG;
42
8d757beb 43 log_debug { "Watchdog has been reset" };
55c0d020 44 alarm($WATCHDOG->timeout);
c824fdf3 45}
46
47#must explicitly call this method to stop the
48#watchdog from killing the process - if the
49#watchdog is lost because it goes out of scope
50#it makes sense to still terminate the process
51sub shutdown {
52 my ($self) = @_;
8d757beb 53 log_debug { "Watchdog is shutting down" };
55c0d020 54 alarm(0);
c824fdf3 55}
56
571;
58
59