From 370bedc8aee5e34b338039a77579cee4e4032d0b Mon Sep 17 00:00:00 2001 From: Anton Kolesnyk <41349689+antkmsft@users.noreply.github.com> Date: Wed, 13 Dec 2023 14:35:35 -0800 Subject: [PATCH] Add retry logic for CMake download operations (#5227) * Add retry logic for CMake donwload operations * Remove message * PR feedback * Move condition out of the loop * Another small improvement * Cleaner diff --------- Co-authored-by: Anton Kolesnyk --- cmake-modules/FolderList.cmake | 35 ++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/cmake-modules/FolderList.cmake b/cmake-modules/FolderList.cmake index eef28f3d8..e4981aeea 100644 --- a/cmake-modules/FolderList.cmake +++ b/cmake-modules/FolderList.cmake @@ -73,13 +73,40 @@ macro(DownloadDepVersion DEP_FOLDER DEP_NAME DEP_VERSION) set(DEP_PREFIX azure-sdk-for-cpp) if(FETCH_SOURCE_DEPS STREQUAL "LATEST") - message("Downloading latest version of ${DEP_NAME}") + SET(DOWNLOAD_MESSAGE "Downloading latest version of ${DEP_NAME}") #get the latest version from main - file(DOWNLOAD http://github.com/Azure/azure-sdk-for-cpp/archive/main.zip ${DOWNLOAD_FOLDER}/${DOWNLOAD_FILE}) + SET(DOWNLOAD_LINK "http://github.com/Azure/azure-sdk-for-cpp/archive/main.zip") else() - message("Downloading version ${DEP_VERSION} of ${DEP_NAME}") + SET(DOWNLOAD_MESSAGE "Downloading version ${DEP_VERSION} of ${DEP_NAME}") # get the zip - file(DOWNLOAD https://github.com/Azure/azure-sdk-for-cpp/archive/refs/tags/${DOWNLOAD_FILE} ${DOWNLOAD_FOLDER}/${DOWNLOAD_FILE}) + SET(DOWNLOAD_LINK "https://github.com/Azure/azure-sdk-for-cpp/archive/refs/tags/${DOWNLOAD_FILE}") + endif() + + foreach(RETRY_ATTEMPT RANGE 2) + math(EXPR RETRY_DELAY "10 * ${RETRY_ATTEMPT}" OUTPUT_FORMAT DECIMAL) + if (RETRY_ATTEMPT GREATER 0) + message("Waiting for ${RETRY_DELAY} seconds before retrying download.") + execute_process(COMMAND ${CMAKE_COMMAND} -E sleep ${RETRY_DELAY}) + endif() + + message(${DOWNLOAD_MESSAGE}) + file( + DOWNLOAD ${DOWNLOAD_LINK} + ${DOWNLOAD_FOLDER}/${DOWNLOAD_FILE} + SHOW_PROGRESS + STATUS DOWNLOAD_STATUS + ) + + list(GET DOWNLOAD_STATUS 0 STATUS_CODE) + if (${STATUS_CODE} EQUAL 0) + break() + else() + list(GET DOWNLOAD_STATUS 1 ERROR_MESSAGE) + message("Download failed with status code ${STATUS_CODE}: ${ERROR_MESSAGE}.") + endif() + endforeach() + if (NOT ${STATUS_CODE} EQUAL 0) + message(FATAL_ERROR "Dependency download failed (Link: ${DOWNLOAD_LINK}).") endif() #extract the zip