Limit number of concurrent goroutines
This commit is contained in:
parent
5fd9bc851b
commit
62dadc53bf
12 changed files with 206 additions and 103 deletions
58
app/app.go
58
app/app.go
|
|
@ -4,7 +4,7 @@ import (
|
|||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"gitlab.com/revalus/grm/commands"
|
||||
"gitlab.com/revalus/grm/config"
|
||||
|
|
@ -24,28 +24,34 @@ type GitRepositoryManager struct {
|
|||
configuration config.Configuration
|
||||
}
|
||||
|
||||
func (g *GitRepositoryManager) Parse(args []string) {
|
||||
checkCriticalError := func(err error) {
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v", err.Error())
|
||||
os.Exit(2)
|
||||
}
|
||||
func (g *GitRepositoryManager) Parse(args []string) error {
|
||||
arguments, err := config.ParseCliArguments(APP_NAME, APP_DESCRIPTION, args)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
arguments, err := config.ParseCliArguments(APP_NAME, APP_DESCRIPTION, args)
|
||||
checkCriticalError(err)
|
||||
|
||||
configFileContent, err := getFileContent(arguments.ConfigurationFile)
|
||||
checkCriticalError(err)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
fileExcension, err := getFileExcension(arguments.ConfigurationFile)
|
||||
checkCriticalError(err)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
configuration, err := config.GetRepositoryConfig(configFileContent, fileExcension)
|
||||
checkCriticalError(err)
|
||||
if err != nil {
|
||||
fmt.Printf("Error: %v", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
g.cliArguments = arguments
|
||||
g.configuration = configuration
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g *GitRepositoryManager) Run(w io.Writer) int {
|
||||
|
|
@ -55,7 +61,7 @@ func (g *GitRepositoryManager) Run(w io.Writer) int {
|
|||
|
||||
exitCode := 0
|
||||
|
||||
if len(g.cliArguments.LimitTags) != 0 {
|
||||
if len(g.cliArguments.LimitToTags) != 0 {
|
||||
err := g.limitTags()
|
||||
if err != nil {
|
||||
echo.ErrorfMsg(err.Error())
|
||||
|
|
@ -63,7 +69,7 @@ func (g *GitRepositoryManager) Run(w io.Writer) int {
|
|||
}
|
||||
}
|
||||
|
||||
if g.cliArguments.LimitName != "" {
|
||||
if g.cliArguments.LimitToName != "" {
|
||||
err := g.limitName()
|
||||
if err != nil {
|
||||
echo.ErrorfMsg(err.Error())
|
||||
|
|
@ -90,7 +96,7 @@ func (g *GitRepositoryManager) Run(w io.Writer) int {
|
|||
return exitCode
|
||||
}
|
||||
|
||||
func (g GitRepositoryManager) describeStatus(status commands.CommandStatus) {
|
||||
func describeStatus(status commands.CommandStatus) {
|
||||
if status.Error {
|
||||
echo.RedMessageF("Repository \"%v\": an error occurred: %v", status.Name, status.Message)
|
||||
return
|
||||
|
|
@ -107,7 +113,7 @@ func (g *GitRepositoryManager) limitTags() error {
|
|||
limitedTagsTmp := []config.RepositoryConfig{}
|
||||
|
||||
for _, item := range g.configuration.Repositories {
|
||||
if checkAnyOfItemInSlice(item.Tags, g.cliArguments.LimitTags) {
|
||||
if checkAnyOfItemInSlice(item.Tags, g.cliArguments.LimitToTags) {
|
||||
limitedTagsTmp = append(limitedTagsTmp, item)
|
||||
}
|
||||
}
|
||||
|
|
@ -120,7 +126,7 @@ func (g *GitRepositoryManager) limitTags() error {
|
|||
|
||||
func (g *GitRepositoryManager) limitName() error {
|
||||
for _, item := range g.configuration.Repositories {
|
||||
if g.cliArguments.LimitName == item.Name {
|
||||
if g.cliArguments.LimitToName == item.Name {
|
||||
g.configuration.Repositories = []config.RepositoryConfig{item}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -129,13 +135,19 @@ func (g *GitRepositoryManager) limitName() error {
|
|||
}
|
||||
|
||||
func (g *GitRepositoryManager) runCommand(cmd commands.Command) {
|
||||
statusChan := make(chan commands.CommandStatus)
|
||||
routines := make(chan struct{}, g.cliArguments.Routines)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for _, repo := range g.configuration.Repositories {
|
||||
go cmd.Command(repo, statusChan)
|
||||
}
|
||||
wg.Add(1)
|
||||
|
||||
for range g.configuration.Repositories {
|
||||
g.describeStatus(<-statusChan)
|
||||
go func(r config.RepositoryConfig) {
|
||||
defer wg.Done()
|
||||
routines <- struct{}{}
|
||||
describeStatus(cmd.Command(r))
|
||||
|
||||
<-routines
|
||||
}(repo)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue