| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | package Juno::Check::TCP; | ||
| 4 | # ABSTRACT: A TCP check for Juno | ||
| 5 | $Juno::Check::TCP::VERSION = '0.010'; | ||
| 6 | use Moo; | ||
| 7 | use MooX::Types::MooseLike::Base qw<Int>; | ||
| 8 | use AnyEvent::Socket; | ||
| 9 | use namespace::sweep; | ||
| 10 | |||
| 11 | with 'Juno::Role::Check'; | ||
| 12 | |||
| 13 | has port => ( | ||
| 14 | is => 'ro', | ||
| 15 | isa => Int, | ||
| 16 | required => 1, | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub check { | ||
| 20 | my $self = shift; | ||
| 21 | my @hosts = @{ $self->hosts }; | ||
| 22 | my $port = $self->port; | ||
| 23 | |||
| 24 | foreach my $host (@hosts) { | ||
| 25 | $self->has_on_before | ||
| 26 | and $self->on_before->( $self, $host ); | ||
| 27 | |||
| 28 | tcp_connect $host, $port, sub { | ||
| 29 | my ($fh) = @_; | ||
| 30 | |||
| 31 | if ( $self->has_on_result ) { | ||
| 32 | $self->on_result->( $self, $fh ); | ||
| 33 | } | ||
| 34 | |||
| 35 | if ( ! defined $fh ) { | ||
| 36 | my $error = $!; | ||
| 37 | |||
| 38 | $self->has_on_fail | ||
| 39 | and $self->on_fail->( $self, $error ); | ||
| 40 | |||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | $self->has_on_success | ||
| 45 | and $self->on_success->( $self, $fh ); | ||
| 46 | } | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | 1; | ||
| 54 | |||
| 55 | __END__ |