diff --git a/src/Core_Command.php b/src/Core_Command.php index a4c0e58a..29960562 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -116,6 +116,9 @@ public function check_update( $_, $assoc_args ) { * [--version=] * : Select which version you want to download. Accepts a version number, 'latest' or 'nightly'. * + * [--retry=] + * : Number of retries in case of failure. Default is 0. Maximum is 5 retries. + * * [--skip-content] * : Download WP without the default themes and plugins. * @@ -290,28 +293,79 @@ function () use ( $temp ) { 'insecure' => $insecure, ]; - $response = Utils\http_request( 'GET', $download_url, null, $headers, $options ); + $retry = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); + $retry_delay = 5; // It will be incremented in the loop after each retry. + $current_try = 1; + + if ( 5 < $retry ) { + WP_CLI::warning( 'Maximum number of retries can be 5. Resetting to 5.' ); - if ( 404 === (int) $response->status_code ) { - WP_CLI::error( 'Release not found. Double-check locale or version.' ); - } elseif ( 20 !== (int) substr( $response->status_code, 0, 2 ) ) { - WP_CLI::error( "Couldn't access download URL (HTTP code {$response->status_code})." ); + $retry = 5; } + do { + $response = Utils\http_request( 'GET', $download_url, null, $headers, $options ); + $status_code = (int) $response->status_code; + $is_response_ok = $status_code >= 200 && $status_code < 300; + + // Exit the loop if the response is OK or the status code is 404. + if ( $is_response_ok || 404 === $status_code ) { + $retry = 0; + } + + if ( 404 === $status_code ) { + WP_CLI::error( 'Release not found. Double-check locale or version.' ); + } elseif ( ! $is_response_ok ) { + WP_CLI::warning( "Couldn't access download URL (HTTP code {$response->status_code})." ); + + if ( 0 < $retry ) { + $retry_delay *= $current_try++; + + WP_CLI::log( "Retrying in {$retry_delay} seconds. {$retry} retries left." ); + sleep( $retry_delay ); + } + } + } while ( 0 < $retry-- && ! $is_response_ok ); + if ( 'nightly' !== $version ) { unset( $options['filename'] ); - $md5_response = Utils\http_request( 'GET', $download_url . '.md5', null, [], $options ); - if ( $md5_response->status_code >= 200 && $md5_response->status_code < 300 ) { - $md5_file = md5_file( $temp ); - if ( $md5_file === $md5_response->body ) { - WP_CLI::log( 'md5 hash verified: ' . $md5_file ); + $retry = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); + $retry_delay = 5; // It will be incremented in the loop after each retry. + $current_try = 1; + + do { + $md5_response = Utils\http_request( 'GET', $download_url . '.md5', null, [], $options ); + $status_code = (int) $md5_response->status_code; + $is_response_ok = $status_code >= 200 && $status_code < 300; + + // Exit the loop if the response is OK or the status code is 404. + if ( $is_response_ok || 404 === $status_code ) { + $retry = 0; + } + + if ( $is_response_ok ) { + $md5_file = md5_file( $temp ); + + if ( $md5_file === $md5_response->body ) { + WP_CLI::log( 'md5 hash verified: ' . $md5_file ); + } else { + WP_CLI::error( "md5 hash for download ({$md5_file}) is different than the release hash ({$md5_response->body})." ); + } + } elseif ( 404 === $status_code ) { + WP_CLI::warning( 'md5 hash not found for release.' ); } else { - WP_CLI::error( "md5 hash for download ({$md5_file}) is different than the release hash ({$md5_response->body})." ); + WP_CLI::warning( "Couldn't access md5 hash for release ({$download_url}.md5, HTTP code {$md5_response->status_code})." ); + + if ( 0 < $retry ) { + $retry_delay *= $current_try++; + + WP_CLI::log( "Retrying in {$retry_delay} seconds. {$retry} retries left." ); + sleep( $retry_delay ); + } } - } else { - WP_CLI::warning( "Couldn't access md5 hash for release ({$download_url}.md5, HTTP code {$md5_response->status_code})." ); - } + } while ( 0 < $retry-- && ! $is_response_ok ); + } else { WP_CLI::warning( 'md5 hash checks are not available for nightly downloads.' ); }