Skip to content

Commit

Permalink
Make openqa-cli retry also on connection errors
Browse files Browse the repository at this point in the history
There may be temporary network problems or an overloaded webui. In those
cases we also want to retry.

Issue: https://progress.opensuse.org/issues/138545
  • Loading branch information
perlpunk committed Nov 13, 2023
1 parent 6e1f097 commit 5136bc4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/OpenQA/Command.pm
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ sub retry_tx ($self, $client, $tx, $retries = undef, $delay = undef) {
for (;; --$retries) {
$tx = $client->start($tx);
my $res_code = $tx->res->code // 0;
return $self->handle_result($tx) unless $res_code =~ /50[23]/ && $retries > 0;
return $self->handle_result($tx) unless $res_code =~ /^(50[23]|0)$/ && $retries > 0;
print encode('UTF-8',
"Request failed, hit error $res_code, retrying up to $retries more times after waiting …\n");
sleep $delay;
Expand Down
6 changes: 6 additions & 0 deletions t/43-cli-api.t
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ my @host = ('--host', $host);
# Default options for authentication tests
my @auth = ('--apikey', 'ARTHURKEY01', '--apisecret', 'EXCALIBUR', @host);

$ENV{MOJO_CONNECT_TIMEOUT} = 1;
$ENV{OPENQA_CLI_RETRY_SLEEP_TIME_S} = 0;
my $cli = OpenQA::CLI->new;
my $api = OpenQA::CLI::api->new;
Expand Down Expand Up @@ -451,6 +452,11 @@ EOF

($stdout, $stderr, @result) = capture sub { $api->run('--retries', '1', @params) };
like $stdout, qr/failed.*retrying/, 'requests are retried on error if requested';

@params = ('--host', 'http://localhost:123456', '--retries', 1, 'api', 'test');
($stdout, $stderr, @result) = capture sub { $api->run(@params) };
like $stderr, qr/Connection refused/, 'aborts on connection refused';
like $stdout, qr/failed.*retrying/, 'requests are retried on error if requested';
};

subtest 'Pretty print JSON' => sub {
Expand Down

0 comments on commit 5136bc4

Please sign in to comment.