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