Skip to content

Commit

Permalink
Change retry syntax (#44)
Browse files Browse the repository at this point in the history
* Change retry syntax

* Change retry syntax
  • Loading branch information
rdsubhas authored Dec 23, 2016
1 parent a967d7a commit 0938478
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 24 deletions.
9 changes: 4 additions & 5 deletions core/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 20 additions & 17 deletions core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions examples/retry/myke.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0938478

Please sign in to comment.