Skip to content

Commit

Permalink
Merge pull request #147 from ivanilves/wait-with-tolerance
Browse files Browse the repository at this point in the history
IMPROVE: Tolerate errors on `Pull` and `Push` actions
  • Loading branch information
vonrabbe authored Apr 29, 2018
2 parents 7dfacac + 51dce40 commit 8a53b51
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions api/v1/v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (api *API) PullTags(cn *collection.Collection) error {
}(repo, tags, done)
}

return wait.Until(done)
return wait.WithTolerance(done)
}

// PushTags compares images from remote and "push" (usually local) registries,
Expand Down Expand Up @@ -357,7 +357,7 @@ func (api *API) PushTags(cn *collection.Collection, push PushConfig) error {
}(repo, tags, done)
}

return wait.Until(done)
return wait.WithTolerance(done)
}

func logDebugData(data io.Reader) {
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type Options struct {
} `positional-args:"yes" required:"yes"`
}

var exitCode = 0

var doNotFail = false

func suicide(err error, critical bool) {
Expand All @@ -44,6 +46,8 @@ func suicide(err error, critical bool) {
if !doNotFail || critical {
os.Exit(1)
}

exitCode = 254 // not typical error code, for "git grep" friendliness
}

func parseFlags() (*Options, error) {
Expand Down Expand Up @@ -169,7 +173,7 @@ func main() {
}

if !o.DaemonMode {
os.Exit(0)
os.Exit(exitCode)
}

fmt.Printf("WAIT: %v\n-\n", o.PollingInterval)
Expand Down
28 changes: 28 additions & 0 deletions util/wait/wait.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package wait

import "fmt"

// Until iterates over buffered error channel and:
// * upon receiving non-nil value from the channel, makes an early return with this value
// * if no non-nil values were received from iteration over the channel, it just returns nil
Expand All @@ -21,3 +23,29 @@ func Until(done chan error) error {

return nil
}

// WithTolerance is the same as Until, but it does not return immediately on errors
// rather loops through all channel capacity returning "composite" error in the end.
func WithTolerance(done chan error) error {
var errMessage string

i := 0

for err := range done {
if err != nil {
errMessage = errMessage + err.Error() + "\n"
}

i++

if i >= cap(done) {
close(done)
}
}

if len(errMessage) == 0 {
return nil
}

return fmt.Errorf(errMessage)
}

0 comments on commit 8a53b51

Please sign in to comment.