-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
project_command_pool_executor.go
87 lines (72 loc) · 1.86 KB
/
project_command_pool_executor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package events
import (
"sort"
"sync"
"github.com/remeh/sizedwaitgroup"
"github.com/runatlantis/atlantis/server/events/command"
)
type prjCmdRunnerFunc func(ctx command.ProjectContext) command.ProjectResult
func runProjectCmdsParallel(
cmds []command.ProjectContext,
runnerFunc prjCmdRunnerFunc,
poolSize int,
) command.Result {
var results []command.ProjectResult
mux := &sync.Mutex{}
wg := sizedwaitgroup.New(poolSize)
for _, pCmd := range cmds {
pCmd := pCmd
var execute func()
wg.Add()
execute = func() {
defer wg.Done()
res := runnerFunc(pCmd)
mux.Lock()
results = append(results, res)
mux.Unlock()
}
go execute()
}
wg.Wait()
return command.Result{ProjectResults: results}
}
func runProjectCmds(
cmds []command.ProjectContext,
runnerFunc prjCmdRunnerFunc,
) command.Result {
var results []command.ProjectResult
for _, pCmd := range cmds {
res := runnerFunc(pCmd)
results = append(results, res)
}
return command.Result{ProjectResults: results}
}
func splitByExecutionOrderGroup(cmds []command.ProjectContext) [][]command.ProjectContext {
groups := make(map[int][]command.ProjectContext)
for _, cmd := range cmds {
groups[cmd.ExecutionOrderGroup] = append(groups[cmd.ExecutionOrderGroup], cmd)
}
var groupKeys []int
for k := range groups {
groupKeys = append(groupKeys, k)
}
sort.Ints(groupKeys)
var res [][]command.ProjectContext
for _, group := range groupKeys {
res = append(res, groups[group])
}
return res
}
func runProjectCmdsParallelGroups(
cmds []command.ProjectContext,
runnerFunc prjCmdRunnerFunc,
poolSize int,
) command.Result {
var results []command.ProjectResult
groups := splitByExecutionOrderGroup(cmds)
for _, group := range groups {
res := runProjectCmdsParallel(group, runnerFunc, poolSize)
results = append(results, res.ProjectResults...)
}
return command.Result{ProjectResults: results}
}