Execute: probable bikeshedding

This commit is contained in:
Joel Elkins 2022-08-16 00:57:27 -05:00
parent e3aa4c2ad2
commit 4caabc0327
No known key found for this signature in database
GPG Key ID: 133589DC38921AE2

View File

@ -35,12 +35,11 @@ import (
)
func execForEach(tgts []container.Container, getSet func(*container.Container) command.CommandSet, groupScale int) {
var ser sync.Mutex // serialize non-async containers
runLevel := make(map[int][]container.Container)
runLevel := make(map[int][]*container.Container)
for i := range tgts {
rl := tgts[i].StartGroup * groupScale
runLevel[rl] = append(runLevel[rl], tgts[i])
runLevel[rl] = append(runLevel[rl], &tgts[i])
}
rls := maps.Keys(runLevel)
@ -48,7 +47,6 @@ func execForEach(tgts []container.Container, getSet func(*container.Container) c
for _, r := range rls {
cs := runLevel[r]
ser.Lock()
var dispScale string
if groupScale == 0 {
dispScale = "<all>"
@ -56,40 +54,32 @@ func execForEach(tgts []container.Container, getSet func(*container.Container) c
dispScale = strconv.Itoa(r / groupScale)
}
fmt.Fprintln(output, "*** Running a command set for group", dispScale, mapNames(cs))
go func(async bool, conts []container.Container, ser *sync.Mutex) {
var wg sync.WaitGroup
defer func() {
wg.Wait()
ser.Unlock()
}()
for i := range conts {
wg.Add(1)
go runSet(&conts[i], getSet, &wg)
}
if !async {
time.Sleep(2 * time.Second)
}
}(r == 0, cs, &ser)
}
ser.Lock()
}
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(log.Fields{
"error": err,
"action": set.ID,
}).Errorln("Failed")
return
wg := new(sync.WaitGroup)
for i := range cs {
wg.Add(1)
go func(cont *container.Container, set command.CommandSet) {
defer wg.Done()
for _, cmd := range set.Commands {
if err := cmd.Execute(output, fake, set.ID); err != nil {
cont.LogEntry().WithFields(log.Fields{
"error": err,
"action": set.ID,
}).Errorln("Failed")
return
}
}
}(cs[i], getSet(cs[i]))
}
wg.Wait()
if r != 0 {
time.Sleep(2 * time.Second)
}
}
}
// For debugging
func mapNames(conts []container.Container) string {
func mapNames(conts []*container.Container) string {
names := []string{}
for i := range conts {
names = append(names, conts[i].Name)