From 161eb2071fd5ef1344d78c2875d00feef4131bf2 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Fri, 26 Apr 2024 15:44:54 +0530 Subject: [PATCH 1/4] Add retry and retry-delay in core download cli --- src/Core_Command.php | 66 ++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 17 deletions(-) diff --git a/src/Core_Command.php b/src/Core_Command.php index a4c0e58a..212dd242 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -116,6 +116,12 @@ 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. + * + * [--retry-delay=] + * : Delay between retries in seconds. Default is 5 seconds. + * * [--skip-content] * : Download WP without the default themes and plugins. * @@ -290,28 +296,54 @@ function () use ( $temp ) { 'insecure' => $insecure, ]; - $response = Utils\http_request( 'GET', $download_url, null, $headers, $options ); - - 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 = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); + $retry_delay = (int) Utils\get_flag_value( $assoc_args, 'retry-delay', 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; + + 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 ) { + WP_CLI::log( "Retrying in {$retry_delay} seconds..." ); + 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 ); + + 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; + + 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 ) { + WP_CLI::log( "Retrying in {$retry_delay} seconds..." ); + 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.' ); } From 742714deb45e204c64950ec7d98e9c3c215bdd29 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Fri, 26 Apr 2024 15:59:20 +0530 Subject: [PATCH 2/4] Exit early from the retry loop if the status is ok or 404 --- src/Core_Command.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Core_Command.php b/src/Core_Command.php index 212dd242..ac3daded 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -304,6 +304,11 @@ function () use ( $temp ) { $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 ) { @@ -324,6 +329,11 @@ function () use ( $temp ) { $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 ); From 76c4f61840942421ae02392e0982cd9e6f7e24ba Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Fri, 26 Apr 2024 16:08:44 +0530 Subject: [PATCH 3/4] Fix phpcs code sniffing issue --- src/Core_Command.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Core_Command.php b/src/Core_Command.php index ac3daded..81481cc7 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -296,7 +296,7 @@ function () use ( $temp ) { 'insecure' => $insecure, ]; - $retry = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); + $retry = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); $retry_delay = (int) Utils\get_flag_value( $assoc_args, 'retry-delay', 5 ); do { @@ -325,8 +325,8 @@ function () use ( $temp ) { $retry = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); do { - $md5_response = Utils\http_request( 'GET', $download_url . '.md5', null, [], $options ); - $status_code = (int) $md5_response->status_code; + $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. From dd81ca8400dca3cd853d32432351c726370233ea Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Fri, 26 Apr 2024 18:51:55 +0530 Subject: [PATCH 4/4] Add max retry to 5 and remove control of retry-delay --- src/Core_Command.php | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/Core_Command.php b/src/Core_Command.php index 81481cc7..29960562 100644 --- a/src/Core_Command.php +++ b/src/Core_Command.php @@ -116,11 +116,8 @@ 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. - * - * [--retry-delay=] - * : Delay between retries in seconds. Default is 5 seconds. + * [--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. @@ -297,7 +294,14 @@ function () use ( $temp ) { ]; $retry = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); - $retry_delay = (int) Utils\get_flag_value( $assoc_args, 'retry-delay', 5 ); + $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.' ); + + $retry = 5; + } do { $response = Utils\http_request( 'GET', $download_url, null, $headers, $options ); @@ -313,8 +317,11 @@ function () use ( $temp ) { 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 ) { - WP_CLI::log( "Retrying in {$retry_delay} seconds..." ); + $retry_delay *= $current_try++; + + WP_CLI::log( "Retrying in {$retry_delay} seconds. {$retry} retries left." ); sleep( $retry_delay ); } } @@ -322,7 +329,10 @@ function () use ( $temp ) { if ( 'nightly' !== $version ) { unset( $options['filename'] ); - $retry = (int) Utils\get_flag_value( $assoc_args, 'retry', 0 ); + + $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 ); @@ -348,7 +358,9 @@ function () use ( $temp ) { WP_CLI::warning( "Couldn't access md5 hash for release ({$download_url}.md5, HTTP code {$md5_response->status_code})." ); if ( 0 < $retry ) { - WP_CLI::log( "Retrying in {$retry_delay} seconds..." ); + $retry_delay *= $current_try++; + + WP_CLI::log( "Retrying in {$retry_delay} seconds. {$retry} retries left." ); sleep( $retry_delay ); } }