diff --git a/core/execution.go b/core/execution.go index 7bb4318..d2f78b2 100644 --- a/core/execution.go +++ b/core/execution.go @@ -44,12 +44,11 @@ func (e *Execution) Execute() error { err := retry(func(attempt int) (bool, error) { err := e.executeTask() - if err != nil && attempt < e.Task.Retries { - retryMs := time.Duration(e.Task.RetryMs) * time.Millisecond - log.Debugf("%v: Failed, Retrying %v/%v in %v", displayName, attempt, e.Task.Retries, retryMs) - time.Sleep(retryMs) + if err != nil && attempt < e.Task.Retry { + log.Debugf("%v: Failed, Retrying %v/%v in %v", displayName, attempt, e.Task.Retry, e.Task.RetryDelay) + time.Sleep(e.Task.RetryDelay) } - return attempt < e.Task.Retries, err + return attempt < e.Task.Retry, err }) elapsed := time.Since(start) diff --git a/core/task.go b/core/task.go index f0203b0..194aa98 100644 --- a/core/task.go +++ b/core/task.go @@ -5,21 +5,24 @@ import ( "github.com/apex/log" "strings" "strconv" + "time" ) type Task struct { - Name string - Desc string - Cmd string - Before string - After string - Shell string - Retries int - RetryMs int + Name string + Desc string + Cmd string + Before string + After string + Shell string + Retry int + RetryDelay time.Duration } +const default_delay = time.Duration(1) * time.Second + func loadTaskJson(name string, json gjson.Result) Task { - t := Task{Retries:0,RetryMs:1000,} + t := Task{Retry:0, RetryDelay:default_delay,} t.Name = name if j := json.Get("desc"); j.Exists() { @@ -37,18 +40,18 @@ func loadTaskJson(name string, json gjson.Result) Task { if j := json.Get("shell"); j.Exists() { t.Shell = strings.TrimSpace(j.String()) } - if j := json.Get("retries"); j.Exists() { - if retries, err := strconv.Atoi(strings.TrimSpace(j.String())); err != nil { - log.WithError(err).Warnf("invalid retries in task %s", name) + if j := json.Get("retry"); j.Exists() { + if retry, err := strconv.Atoi(strings.TrimSpace(j.String())); err != nil { + log.WithError(err).Warnf("invalid retry in task %s", name) } else { - t.Retries = retries + t.Retry = retry } } - if j := json.Get("retry_ms"); j.Exists() { - if retryMs, err := strconv.Atoi(strings.TrimSpace(j.String())); err != nil { - log.WithError(err).Warnf("invalid retry_ms in task %s", name) + if j := json.Get("retry_delay"); j.Exists() { + if delay, err := time.ParseDuration(strings.TrimSpace(j.String())); err != nil { + log.WithError(err).Warnf("invalid retry_delay in task %s", name) } else { - t.RetryMs = retryMs + t.RetryDelay = delay } } return t diff --git a/examples/retry/myke.yml b/examples/retry/myke.yml index ee433c1..9f12ae8 100644 --- a/examples/retry/myke.yml +++ b/examples/retry/myke.yml @@ -4,5 +4,6 @@ desc: demonstrates use of templates and parameters in command tasks: retry: cmd: "false" - retries: 5 - retry_ms: 10 + retry: 5 + # retry_delay defaults to 1s + retry_delay: 10ms