Skip to content

Commit c3eeb33

Browse files
authored
Resilience improvement for mend JRE download (#4974)
Signed-off-by: Vijayan T <[email protected]>
1 parent 65dbd45 commit c3eeb33

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

pkg/whitesource/scanUA.go

+15-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"regexp"
1212
"strings"
1313
"sync"
14+
"time"
1415

1516
"github.com/SAP/jenkins-library/pkg/log"
1617
"github.com/pkg/errors"
@@ -240,20 +241,24 @@ func downloadJre(config *ScanOptions, utils Utils) (string, error) {
240241
javaPath := "java"
241242
if err != nil {
242243
log.Entry().Infof("No Java installation found, downloading JVM from %v", config.JreDownloadURL)
243-
err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil)
244-
if err != nil {
245-
// we check if the copy error occurs and retry the download
246-
// if the copy error did not happen, we rerun the whole download mechanism once
247-
if strings.Contains(err.Error(), "unable to copy content from url to file") {
248-
// retry the download once again
249-
log.Entry().Warnf("Previous Download failed due to %v", err)
250-
err = nil
251-
err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil)
244+
const maxRetries = 3
245+
retries := 0
246+
for retries < maxRetries {
247+
err = utils.DownloadFile(config.JreDownloadURL, jvmTarGz, nil, nil)
248+
if err == nil {
249+
break
250+
}
251+
log.Entry().Warnf("Attempt %d: Download failed due to %v", retries+1, err)
252+
retries++
253+
if retries >= maxRetries {
254+
log.Entry().Errorf("Download failed after %d attempts", retries)
255+
return "", errors.Wrapf(err, "failed to download jre from URL '%s'", config.JreDownloadURL)
252256
}
257+
time.Sleep(1 * time.Second)
253258
}
254259

255260
if err != nil {
256-
return "", errors.Wrapf(err, "failed to download jre from URL '%s'", config.JreDownloadURL)
261+
return "", errors.Wrapf(err, "Even after retry failed to download jre from URL '%s'", config.JreDownloadURL)
257262
}
258263

259264
// ToDo: replace tar call with go library call

0 commit comments

Comments
 (0)