Created
October 23, 2011 10:38
-
-
Save kysnm/1307226 to your computer and use it in GitHub Desktop.
AnyEvent::HTTP で 200 以外が帰ってきた時にリトライする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use AnyEvent; | |
use AnyEvent::HTTP; | |
my $w; | |
my $cv = AE::cv; | |
my @urls = qw{ | |
http://localhost:5000/ | |
http://localhost:5001/ | |
}; | |
my @watchers; | |
for my $url (@urls) { | |
my $retry; | |
$cv->begin; | |
my $http_get; $http_get = sub { | |
http_get $url, | |
recurse => 0, | |
timeout => 30, | |
sub { | |
my ($body, $hdr) = @_; | |
my $status = $hdr->{Status}; | |
my $reason = defined $hdr->{Reason}? $hdr->{Reason}: ''; | |
if ($status == 200) { | |
warn "$url access ok"; | |
$cv->end; | |
} else { | |
warn "$url access ng"; | |
if ($retry++ < 2) { | |
$cv->begin; | |
push @watchers, AE::timer(1, 0, $http_get); | |
} | |
$cv->end; | |
} | |
} | |
}; | |
push @watchers, AE::timer(0, 0, $http_get); | |
} | |
$cv->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment