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,10 +5798,22 @@ 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 ) {
$logger->fatal("API reports agent not found: the agent has been " my $agents_link = $client->entry_link . '/agents';
. "deleted, or its UUID (" . ($agent->uuid || '?') . ") " $agents_link =~ s!/{2,}!/!g; # //agents doesn't work
. "is wrong. Check https://cloud.percona.com/agents for a " my $api_ok = eval {
. "list of active agents."); $client->head(
link => $agents_link,
);
};
if ( $api_ok ) {
$logger->fatal("API reports agent not found: the agent has been "
. "deleted, or its UUID (" . ($agent->uuid || '?') . ") "
. "is wrong. Check https://cloud.percona.com/agents for a "
. "list of active agents.");
}
else {
$logger->warning("API is down.");
}
} }
else { else {
$logger->warning($EVAL_ERROR); $logger->warning($EVAL_ERROR);
@@ -6238,13 +6267,25 @@ 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 ) {
stop_all_services( my $agents_link = $client->entry_link . '/agents';
lib_dir => $lib_dir, $agents_link =~ s!/{2,}!/!g; # //agents doesn't work
); my $api_ok = eval {
$logger->fatal("API reports agent not found: the agent has been " $client->head(
. "deleted, or its UUID (" . ($agent->uuid || '?') . ") " link => $agents_link,
. "is wrong. Check https://cloud.percona.com/agents for a " );
. "list of active agents."); };
if ( $api_ok ) {
stop_all_services(
lib_dir => $lib_dir,
);
$logger->fatal("API reports agent not found: the agent has been "
. "deleted, or its UUID (" . ($agent->uuid || '?') . ") "
. "is wrong. Check https://cloud.percona.com/agents for a "
. "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 {