Skip to content

Commit

Permalink
Add retries during source url download (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubham149 authored Mar 28, 2023
1 parent 93dd99e commit 9a70dc3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
18 changes: 8 additions & 10 deletions plugin/harness/execer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,29 +127,27 @@ func (e *Execer) Exec(ctx context.Context) error {
return nil
}

// else if the plugin is a Bash script

// determine the default script path
script := out.Run.Bash.Path
shell := "/bin/bash"
path := filepath.Join(e.Source, script)

slog.Debug("execute", slog.String("file", script))

var path, shell string
// if the bash shell does not exist fallback
// to posix shell.
switch runtime.GOOS {
case "windows":
// TODO we may want to disable profile and interactive mode
// when executing powershell scripts -noprofile -noninteractive
shell = "powershell"
path = filepath.Join(e.Source, out.Run.Pwsh.Path)

case "linux", "darwin":
shell = "bash"
path = filepath.Join(e.Source, out.Run.Bash.Path)

// fallback to the posix shell if bash
// is not available on the host.
if _, err := exec.LookPath("bash"); err != nil {
shell = "/bin/sh"
shell = "sh"
}
}
slog.Debug("execute", slog.String("file", path))

// execute the binary
cmd := exec.Command(shell, path)
Expand Down
15 changes: 14 additions & 1 deletion plugin/internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/drone/plugin/cache"
"github.com/klauspost/compress/zstd"
"github.com/pkg/errors"
"golang.org/x/exp/slog"
)

const (
Expand All @@ -24,7 +25,7 @@ func Download(url string) (string, error) {
binPath := filepath.Join(key, "step.exe")

downloadFn := func() error {
if err := download(url, binPath); err != nil {
if err := downloadWithRetries(url, binPath); err != nil {
return errors.Wrap(err, fmt.Sprintf("url: %s", url))
}
return nil
Expand All @@ -36,6 +37,18 @@ func Download(url string) (string, error) {
return binPath, nil
}

func downloadWithRetries(url, path string) error {
var err error
for i := 0; i < 3; i++ {
if err = download(url, path); err == nil {
return nil
}
slog.Error("failed to download url, retrying", slog.String("url", url), "error", err)
time.Sleep(1 * time.Second)
}
return err
}

// download method downloads a source & writes it to a file.
// If file is compressed, it also decompresses the file on the basis of
// file extension in url. Currently it supports zstd format.
Expand Down

0 comments on commit 9a70dc3

Please sign in to comment.