@@ -8,7 +8,7 @@ use Mojo::Base -strict, -signatures;
88use Cpanel::JSON::XS;
99use Data::Dump ' pp' ;
1010use Exporter ' import' ;
11- use OpenQA::Client ;
11+ use OpenQA::Command ;
1212use OpenQA::Jobs::Constants;
1313use Mojo::File ' path' ;
1414use Mojo::URL;
@@ -17,6 +17,11 @@ use OpenQA::Script::CloneJobSUSE;
1717use List::Util ' any' ;
1818use HTTP::Status qw( :constants) ;
1919
20+ package OpenQA::Script::CloneJob::Command {
21+ use Mojo::Base ' OpenQA::Command' , -signatures;
22+ sub handle_result ($self , $tx , $orig_tx = undef ) { $tx }
23+ }
24+
2025our @EXPORT = qw(
2126 clone_jobs
2227 clone_job_apply_settings
@@ -89,18 +94,23 @@ sub _handle_txn_error ($tx, $jobid, $ctx) {
8994 _handle_unexpected_return_code($tx ) unless $tx -> res-> code == HTTP_OK;
9095}
9196
97+ sub _get_with_retry ($url_handler , $url , $jobid , $ctx , $options ) {
98+ my $remote = $url_handler -> {remote };
99+ my $tx = $url_handler -> {command }-> retry_tx($remote , $remote -> get($url ), $options -> {retry });
100+ _handle_txn_error($tx , $jobid , $ctx );
101+ return $tx -> res-> json;
102+ }
103+
92104sub _get_vars ($jobid , $url_handler , $options ) {
93105 my $url = $url_handler -> {remote_url }-> clone-> path(" /tests/$jobid /file/vars.json" );
94- _handle_txn_error(my $tx = $url_handler -> {remote }-> max_redirects(3)-> get($url ), $jobid , ' vars.json of job' );
95- return $tx -> res-> json;
106+ return _get_with_retry($url_handler , $url , $jobid , ' vars.json of job' , $options );
96107}
97108
98109sub clone_job_get_job ($jobid , $url_handler , $options ) {
99110 my $url = $url_handler -> {remote_url }-> clone;
100111 $url -> path(" jobs/$jobid " );
101112 $url -> query-> merge(check_assets => 1) unless $options -> {' ignore-missing-assets' };
102- _handle_txn_error(my $tx = $url_handler -> {remote }-> max_redirects(3)-> get($url ), $jobid , ' job' );
103- my $job = $tx -> res-> json-> {job };
113+ my $job = _get_with_retry($url_handler , $url , $jobid , ' job' , $options )-> {job };
104114 print STDERR Cpanel::JSON::XS-> new-> pretty-> encode($job ) if $options -> {verbose };
105115 $job -> {vars } = _get_vars($jobid , $url_handler , $options ) if $options -> {reproduce };
106116 return $job ;
@@ -219,7 +229,7 @@ sub split_jobid ($url_string) {
219229}
220230
221231sub make_curl_arguments ($options ) {
222- my @args = (' --follow' , ' --retry' , ( $options -> {retry } // 5) , ' --retry-connrefused' );
232+ my @args = (' --follow' , ' --retry' , $options -> {retry }, ' --retry-connrefused' );
223233 push @args , ' --no-progress-meter' unless $options -> {' show-progress' };
224234 push @args , ' --verbose' if $options -> {verbose };
225235 return \@args ;
@@ -236,22 +246,30 @@ sub create_url_handler ($options) {
236246 # configure user agent for destination host (usually localhost)
237247 my $local_url = OpenQA::Client::url_from_host($options -> {host });
238248 $local_url -> path(' /api/v1/jobs' );
239- my $local = OpenQA::Client -> new(
240- api => $local_url -> host ,
249+ my $command = OpenQA::Script::CloneJob::Command -> new(
250+ name => ' openqa-clone-job ' ,
241251 apikey => $options -> {apikey },
242- apisecret => $options -> {apisecret });
252+ apisecret => $options -> {apisecret },
253+ options => $options
254+ );
255+ my $local = $command -> client($local_url )-> max_redirects(3);
243256 die " API key/secret for '$options ->{host}' missing. Check out '$0 --help' for the config file syntax/lookup.\n "
244257 if !$options -> {' export-command' } && !($local -> apikey && $local -> apisecret);
245258
259+ # configure the default for the number of retries and use exponential backoff by default
260+ $options -> {retry } //= 5;
261+ $ENV {OPENQA_CLI_RETRY_FACTOR } //= 2;
262+
246263 # configure user agents for the source host (usually a remote host)
247264 my $remote_url = OpenQA::Client::url_from_host($options -> {from });
248265 $remote_url -> path(' /api/v1/jobs' );
249266 return {
250267 curl_args => make_curl_arguments($options ),
251268 secrets => read_secrets($remote_url -> host),
269+ command => $command ,
252270 local => $local ,
253271 local_url => $local_url ,
254- remote => OpenQA::Client -> new( api => $remote_url -> host ),
272+ remote => $command -> client( $remote_url ) -> apikey( undef ) -> apisecret( undef ) -> max_redirects(3 ),
255273 remote_url => $remote_url
256274 };
257275}
@@ -316,8 +334,10 @@ sub clone_jobs ($jobid, $options) {
316334 clone_job($jobid , $url_handler , $options , my $post_params = {}, my $jobs = {});
317335 for my $counter (1 .. $repeat ) {
318336 append_idx_to_test_name($counter , $digits , $post_params ) if $repeat > 1;
319- my $tx = post_jobs($post_params , $url_handler , $options );
320- handle_tx($tx , $url_handler , $options , $jobs ) if $tx ;
337+ if (my $tx = post_jobs($post_params , $url_handler , $options )) {
338+ $tx = $url_handler -> {command }-> retry_tx($url_handler -> {local }, $tx , $options -> {retry });
339+ handle_tx($tx , $url_handler , $options , $jobs );
340+ }
321341 }
322342}
323343
@@ -397,7 +417,7 @@ sub post_jobs ($post_params, $url_handler, $options) {
397417 return undef ;
398418 }
399419 print STDERR Cpanel::JSON::XS-> new-> pretty-> encode(\%composed_params ) if $options -> {verbose };
400- return $local -> max_redirects(3) -> post($local_url , form => \%composed_params );
420+ return $local -> post($local_url , form => \%composed_params );
401421}
402422
4034231;
0 commit comments