From c3eeb3398ef2189d4a1ac3b790b5135d82674047 Mon Sep 17 00:00:00 2001 From: Vijayan T Date: Tue, 25 Jun 2024 19:08:27 +0530 Subject: [PATCH] Resilience improvement for mend JRE download (#4974) Signed-off-by: Vijayan T --- pkg/whitesource/scanUA.go | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/pkg/whitesource/scanUA.go b/pkg/whitesource/scanUA.go index 990946df5e..95753f1678 100644 --- a/pkg/whitesource/scanUA.go +++ b/pkg/whitesource/scanUA.go @@ -11,6 +11,7 @@ import ( "regexp" "strings" "sync" + "time" "github.com/SAP/jenkins-library/pkg/log" "github.com/pkg/errors" @@ -240,20 +241,24 @@ func downloadJre(config *ScanOptions, utils Utils) (string, error) { javaPath := "java" if err != nil { log.Entry().Infof("No Java installation found, downloading JVM from %v", config.JreDownloadURL) - err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil) - if err != nil { - // we check if the copy error occurs and retry the download - // if the copy error did not happen, we rerun the whole download mechanism once - if strings.Contains(err.Error(), "unable to copy content from url to file") { - // retry the download once again - log.Entry().Warnf("Previous Download failed due to %v", err) - err = nil - err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil) + const maxRetries = 3 + retries := 0 + for retries < maxRetries { + err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil) + if err == nil { + break + } + log.Entry().Warnf("Attempt %d: Download failed due to %v", retries+1, err) + retries++ + if retries >= maxRetries { + log.Entry().Errorf("Download failed after %d attempts", retries) + return "", errors.Wrapf(err, "failed to download jre from URL '%s'", config.JreDownloadURL) } + time.Sleep(1 * time.Second) } if err != nil { - return "", errors.Wrapf(err, "failed to download jre from URL '%s'", config.JreDownloadURL) + return "", errors.Wrapf(err, "Even after retry failed to download jre from URL '%s'", config.JreDownloadURL) } // ToDo: replace tar call with go library call