AnyEvent-Monitor
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/AnyEvent/Monitor.pm view on Meta::CPAN
package AnyEvent::Monitor;
use 5.10.1;
use AnyEvent;
use Method::Signatures::Simple;
use Any::Moose;
our $VERSION = '0.32';
has on_softfail => ( is => "ro", isa => "CodeRef" );
has on_hardfail => ( is => "ro", isa => "CodeRef" );
has on_resume => ( is => "ro", isa => "CodeRef" );
has on_fatal => ( is => "ro", isa => "CodeRef" );
has status => ( is => "rw", isa => "Str", default => sub {''} );
has timer => ( is => 'ro', isa => 'HashRef', default => sub { {} } );
has soft_timeout => ( is => 'rw', isa => 'Num', default => sub { 10 } );
has hard_timeout => ( is => 'rw', isa => 'Num', default => sub { 45 } );
has fail_detected => ( is => 'rw', isa => 'Num' );
method BUILD {
$self->install_timers(0);
};
method install_timers($delay) {
$self->install_timer( soft => $delay + $self->soft_timeout );
$self->install_timer( hard => $delay + $self->hard_timeout );
}
method install_timer($which, $after) {
my $method = "${which}fail";
$self->timer->{$which} = AnyEvent->timer(after => $after,
cb => sub {
$self->fail_detected(AnyEvent->now) unless $self->fail_detected;
$self->$method();
},
);
}
method heartbeat($timestamp, $status) {
if ($status eq 'normal') {
$self->install_timers($timestamp - AnyEvent->now);
if ($self->status ne 'normal') {
my $outage = $self->fail_detected ? AnyEvent->now - $self->fail_detected : 0;
$self->fail_detected(0);
$self->on_resume->($self->status, $outage);
}
}
$self->status($status);
}
method softfail {
$self->status('soft timeout')
if $self->status eq 'normal';
$self->on_softfail->();
}
method hardfail {
$self->status('hard timeout')
if $self->status eq 'normal';
$self->on_hardfail->(sub {
$self->install_timers(shift || 60)
unless $self->status eq 'normal'
});
}
__PACKAGE__->meta->make_immutable;
no Any::Moose;
1;
__END__
=encoding utf-8
=for stopwords
view all matches for this distributionview release on metacpan - search on metacpan
( run in 3.650 seconds using v1.00-cache-2.02-grep-82fe00e-cpan-72ae3ad1e6da )