mirror of
https://gitea.elkins.co/Networking/ccl.git
synced 2025-03-09 20:51:39 -05:00
execute: use goroutines + mutex to more efficiently serialize non-async cases
This commit is contained in:
parent
46ad4e6d87
commit
288dc71490
@ -22,7 +22,9 @@ THE SOFTWARE.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/command"
|
||||||
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
|
"gitea.elkins.co/Networking/ccl/internal/pkg/container"
|
||||||
@ -31,12 +33,27 @@ import (
|
|||||||
|
|
||||||
func execForEach(tgts []container.Container, getSet func(*container.Container) command.CommandSet) {
|
func execForEach(tgts []container.Container, getSet func(*container.Container) command.CommandSet) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
var ser sync.Mutex // serialize non-async containers
|
||||||
for i := range tgts {
|
for i := range tgts {
|
||||||
async := !tgts[i].StartOrder.Valid
|
async := tgts[i].StartOrder.ValueOrZero() == 0
|
||||||
runSet := func(cont *container.Container) {
|
wg.Add(1)
|
||||||
if async {
|
if async {
|
||||||
defer wg.Done()
|
// TODO: need to log errors on this branch
|
||||||
|
go runSet(&tgts[i], getSet, &wg)
|
||||||
|
} else {
|
||||||
|
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)
|
set := getSet(cont)
|
||||||
for _, cmd := range set.Commands {
|
for _, cmd := range set.Commands {
|
||||||
if err := cmd.Execute(output, fake, set.ID); err != nil {
|
if err := cmd.Execute(output, fake, set.ID); err != nil {
|
||||||
@ -48,13 +65,13 @@ func execForEach(tgts []container.Container, getSet func(*container.Container) c
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if async {
|
|
||||||
wg.Add(1)
|
// For debugging
|
||||||
go runSet(&tgts[i])
|
func printConts(conts []container.Container) {
|
||||||
} else {
|
names := []string{}
|
||||||
runSet(&tgts[i])
|
for i := range conts {
|
||||||
|
names = append(names, fmt.Sprintf("[%s %d]", conts[i].Name, conts[i].StartOrder.ValueOrZero()))
|
||||||
}
|
}
|
||||||
}
|
fmt.Printf("%v\n", names)
|
||||||
wg.Wait()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user