Files
atlantis/server/events/project_command_pool_executor.go
Sarvar Muminov 90e92e3a13 chore: move CommandContext and CommandResult to models (#193) (#2093)
* Moved CommandContext and CommandResult to models (#193)

* Moved CommandContext and CommandResult to models

* move from models to command

rename CommandContext -> Context
rename CommandResult -> Result

* moved command related helpers into command package

* move ProjectCommandContext and ProjectResult to command/project package

* move project command context and project result

* revert unrelated code

* move tests

* fix left over

* fix linting

* fix tests

* remove unused import

* fix project context dependencies

* fix depenedecies

* fix typo
2022-03-21 10:36:13 -07:00

53 lines
1003 B
Go

package events
import (
"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}
}