Stop agent on 404 only if GET /agents is ok, in case API is down/broken for awhile and 404 is happening for everything.

This commit is contained in:
Daniel Nichter
2013-09-18 20:00:52 -07:00
parent 9d79ca4683
commit d6a46cfd33
2 changed files with 71 additions and 13 deletions

View File

@@ -965,6 +965,23 @@ sub get {
return $resource_objects; return $resource_objects;
} }
sub head {
my ($self, %args) = @_;
have_required_args(\%args, qw(
link
)) or die;
my ($link) = $args{link};
eval {
$self->_request(
method => 'HEAD',
link => $link,
);
};
return $EVAL_ERROR;
}
sub post { sub post {
my $self = shift; my $self = shift;
$self->_set( $self->_set(
@@ -1565,7 +1582,7 @@ use FindBin qw();
eval { eval {
require Percona::Toolkit; require Percona::Toolkit;
require HTTPMicro; require HTTP::Micro;
}; };
{ {
@@ -1796,7 +1813,7 @@ sub pingback {
my $url = $args{url}; my $url = $args{url};
my $instances = $args{instances}; my $instances = $args{instances};
my $ua = $args{ua} || HTTPMicro->new( timeout => 3 ); my $ua = $args{ua} || HTTP::Micro->new( timeout => 3 );
my $response = $ua->request('GET', $url); my $response = $ua->request('GET', $url);
PTDEBUG && _d('Server response:', Dumper($response)); PTDEBUG && _d('Server response:', Dumper($response));
@@ -5781,11 +5798,23 @@ sub init_agent {
if ( $EVAL_ERROR ) { if ( $EVAL_ERROR ) {
my $code = $client->response->code; my $code = $client->response->code;
if ( $code && $code == 404 ) { if ( $code && $code == 404 ) {
my $agents_link = $client->entry_link . '/agents';
$agents_link =~ s!/{2,}!/!g; # //agents doesn't work
my $api_ok = eval {
$client->head(
link => $agents_link,
);
};
if ( $api_ok ) {
$logger->fatal("API reports agent not found: the agent has been " $logger->fatal("API reports agent not found: the agent has been "
. "deleted, or its UUID (" . ($agent->uuid || '?') . ") " . "deleted, or its UUID (" . ($agent->uuid || '?') . ") "
. "is wrong. Check https://cloud.percona.com/agents for a " . "is wrong. Check https://cloud.percona.com/agents for a "
. "list of active agents."); . "list of active agents.");
} }
else {
$logger->warning("API is down.");
}
}
else { else {
$logger->warning($EVAL_ERROR); $logger->warning($EVAL_ERROR);
} }
@@ -6238,6 +6267,14 @@ sub get_config {
if (blessed($e)) { if (blessed($e)) {
if ($e->isa('Percona::WebAPI::Exception::Request')) { if ($e->isa('Percona::WebAPI::Exception::Request')) {
if ( $e->status == 404 ) { if ( $e->status == 404 ) {
my $agents_link = $client->entry_link . '/agents';
$agents_link =~ s!/{2,}!/!g; # //agents doesn't work
my $api_ok = eval {
$client->head(
link => $agents_link,
);
};
if ( $api_ok ) {
stop_all_services( stop_all_services(
lib_dir => $lib_dir, lib_dir => $lib_dir,
); );
@@ -6246,6 +6283,10 @@ sub get_config {
. "is wrong. Check https://cloud.percona.com/agents for a " . "is wrong. Check https://cloud.percona.com/agents for a "
. "list of active agents."); . "list of active agents.");
} }
else {
$logger->warning("API is down. Will try again later.");
}
}
else { else {
$logger->info("$e"); # API error? $logger->info("$e"); # API error?
} }

View File

@@ -159,6 +159,23 @@ sub get {
return $resource_objects; return $resource_objects;
} }
sub head {
my ($self, %args) = @_;
have_required_args(\%args, qw(
link
)) or die;
my ($link) = $args{link};
eval {
$self->_request(
method => 'HEAD',
link => $link,
);
};
return $EVAL_ERROR;
}
# For a successful POST, the server sets the Location header with # For a successful POST, the server sets the Location header with
# the URI of the newly created resource. # the URI of the newly created resource.
sub post { sub post {