diff --git a/cmd/execute.go b/cmd/execute.go index 70d17af..4d4e443 100644 --- a/cmd/execute.go +++ b/cmd/execute.go @@ -22,7 +22,9 @@ THE SOFTWARE. package cmd import ( + "fmt" "sync" + "time" "gitea.elkins.co/Networking/ccl/internal/pkg/command" "gitea.elkins.co/Networking/ccl/internal/pkg/container" @@ -31,30 +33,45 @@ import ( func execForEach(tgts []container.Container, getSet func(*container.Container) command.CommandSet) { var wg sync.WaitGroup + var ser sync.Mutex // serialize non-async containers for i := range tgts { - async := !tgts[i].StartOrder.Valid - runSet := func(cont *container.Container) { - if async { - defer wg.Done() - } - set := getSet(cont) - for _, cmd := range set.Commands { - if err := cmd.Execute(output, fake, set.ID); err != nil { - cont.LogEntry().WithFields(logrus.Fields{ - "error": err, - "action": set.ID, - }).Errorln("Failed") - return - } - } - } + async := tgts[i].StartOrder.ValueOrZero() == 0 + wg.Add(1) if async { - wg.Add(1) - go runSet(&tgts[i]) + // TODO: need to log errors on this branch + go runSet(&tgts[i], getSet, &wg) } else { - runSet(&tgts[i]) + ser.Lock() + go func(cont *container.Container, ser *sync.Mutex) { + defer ser.Unlock() + runSet(cont, getSet, &wg) + time.Sleep(5 * time.Second) + }(&tgts[i], &ser) } } wg.Wait() } +func runSet(cont *container.Container, getSet func(*container.Container) command.CommandSet, wg *sync.WaitGroup) { + defer wg.Done() + set := getSet(cont) + for _, cmd := range set.Commands { + if err := cmd.Execute(output, fake, set.ID); err != nil { + cont.LogEntry().WithFields(logrus.Fields{ + "error": err, + "action": set.ID, + }).Errorln("Failed") + return + } + } +} + +// For debugging +func printConts(conts []container.Container) { + names := []string{} + for i := range conts { + names = append(names, fmt.Sprintf("[%s %d]", conts[i].Name, conts[i].StartOrder.ValueOrZero())) + } + fmt.Printf("%v\n", names) +} +