From c13339896741b86434a988a5762fa94f5b2f5588 Mon Sep 17 00:00:00 2001 From: Rene Krenn Date: Tue, 4 May 2021 13:11:10 +0200 Subject: [PATCH] TT#121056 prevent cron messages while restapi is down die with proper response body only on client errors, or server errors other than 502, 503, 504 errors. skip further processing silently on 502, 503, 504 (to prevent running into them again and again). so cron messages will only pop if there is a real problem. Change-Id: I1ee193c69ce8ea6f07fe4b228cd19bc47c2e35b6 --- bin/ngcp-credit-warning | 15 ++++++++++++++- bin/ngcp-fraud-notifier | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/bin/ngcp-credit-warning b/bin/ngcp-credit-warning index 9f6eedc..f2bb211 100755 --- a/bin/ngcp-credit-warning +++ b/bin/ngcp-credit-warning @@ -118,7 +118,7 @@ sub get_data { my @result = (); while (my $res = $client->next_page($uri)) { - die "$res->result\n" unless $res->is_success; + return [] unless check_api_error($res); my $res_hash = $res->as_hash; my $data = $res_hash->{_embedded}{'ngcp:' . $link}; if ('ARRAY' eq ref $data) { @@ -134,6 +134,19 @@ sub get_data { return \@result; } +sub check_api_error { + my $res = shift; + if ($res->is_success) { + return 1; + } else { + die($res->result . "\n") if ( + $res->is_client_error + or ($res->is_server_error and not scalar grep { $_ == $res->code; } qw(502 503 504)) + ); + return 0; + } +} + sub get_email_template { my $templates_data = get_data('/api/emailtemplates/', 'emailtemplates'); foreach my $template (@{$templates_data}) { diff --git a/bin/ngcp-fraud-notifier b/bin/ngcp-fraud-notifier index 7ef96d1..2108f29 100755 --- a/bin/ngcp-fraud-notifier +++ b/bin/ngcp-fraud-notifier @@ -88,7 +88,7 @@ sub get_data { my @result = (); while (my $res = $client->next_page($uri)) { - die "$res->result\n" unless $res->is_success; + return [] unless check_api_error($res); my $res_hash = $res->as_hash; my $data = $res_hash->{_embedded}{'ngcp:' . $link}; if ('ARRAY' eq ref $data) { @@ -104,6 +104,19 @@ sub get_data { return \@result; } +sub check_api_error { + my $res = shift; + if ($res->is_success) { + return 1; + } else { + die($res->result . "\n") if ( + $res->is_client_error + or ($res->is_server_error and not scalar grep { $_ == $res->code; } qw(502 503 504)) + ); + return 0; + } +} + sub get_email_template { my $event = shift;