diff --git a/CHANGELOG.md b/CHANGELOG.md index f940e65..c345669 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ This update introduces minor bug fixes. ### Changed - Update progress bar text to display basename only - Updated exception handling in PackageReader + - Updated the Download function to attempt to retry the download upon an error for at least 3 times. ### Fixed - Improve build efficiency by preventing duplicate merges diff --git a/src/ncc/Managers/PackageManager.php b/src/ncc/Managers/PackageManager.php index c4e3982..c67047a 100644 --- a/src/ncc/Managers/PackageManager.php +++ b/src/ncc/Managers/PackageManager.php @@ -861,7 +861,7 @@ * @throws NetworkException * @noinspection UnusedFunctionResultInspection */ - private function downloadFile(string $url, string $path): string + private function downloadFile(string $url, string $path, int $retries=3): string { $file_path = basename(parse_url($url, PHP_URL_PATH)); $curl = curl_init($url); @@ -934,17 +934,24 @@ curl_exec($curl); fclose($file_handle); + if(curl_errno($curl)) + { + ShutdownHandler::declareTemporaryPath($file_path); + + if($retries === 0) + { + throw new NetworkException(sprintf('Failed to download file from %s: %s', $url, curl_error($curl))); + } + + Console::outWarning(sprintf('Failed to download file from %s: %s, retrying', $url, curl_error($curl))); + return $this->downloadFile($url, $path, ($retries - 1)); + } + $progress_bar->setMaxValue(100); $progress_bar->setValue(100); $progress_bar->setMiscText('done', true); unset($progress_bar); - if(curl_errno($curl)) - { - ShutdownHandler::declareTemporaryPath($file_path); - throw new NetworkException(sprintf('Failed to download file from %s: %s', $url, curl_error($curl))); - } - curl_close($curl); return $file_path; }