Return immediately if any task in the errgroup fails

This commit is contained in:
marcos
2018-01-14 14:28:35 -03:00
parent 0e95554f31
commit f330b35057

View File

@@ -235,7 +235,7 @@ func (p *pwd) SessionSetup(session *types.Session, sconf SessionSetupConf) error
return sessionNotEmpty return sessionNotEmpty
} }
g, _ := errgroup.WithContext(context.Background()) g, ctx := errgroup.WithContext(context.Background())
for _, conf := range sconf.Instances { for _, conf := range sconf.Instances {
conf := conf conf := conf
@@ -291,12 +291,24 @@ func (p *pwd) SessionSetup(session *types.Session, sconf SessionSetupConf) error
} }
for _, cmd := range conf.Run { for _, cmd := range conf.Run {
exitCode, err := p.InstanceExec(i, cmd) var errch chan error
if err != nil { go func() {
exitCode, err := p.InstanceExec(i, cmd)
if err != nil {
errch <- err
}
if exitCode != 0 {
errch <- fmt.Errorf("Command returned %d on instance %s", exitCode, i.IP)
}
errch <- nil
}()
// ctx.Done() could be called if the errgroup is cancelled due to a previous error. In that case, return immediately
select {
case err = <-errch:
return err return err
} case <-ctx.Done():
if exitCode != 0 { return ctx.Err()
return fmt.Errorf("Command returned %d on instance %s", exitCode, i.IP)
} }
} }
return nil return nil