Skip to content

Commit

Permalink
fix:Concurrency issues with multiple project creation
Browse files Browse the repository at this point in the history
  • Loading branch information
tanzhuo committed Nov 23, 2023
1 parent 1300f8f commit 5197d9a
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions cmd/lynx/internal/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path/filepath"
"strings"
"sync"
"time"

"github.com/AlecAivazis/survey/v2"
Expand Down Expand Up @@ -68,26 +69,31 @@ func run(_ *cobra.Command, args []string) {
}

// creation of multiple projects
done := make(chan error, 1)
done := make(chan error, len(names))
var wg sync.WaitGroup
for _, name := range names {
wg.Add(1)
projectName, workingDir := processProjectParams(name, wd)
p := &Project{Name: projectName}
go func() {
done <- p.New(ctx, workingDir, repoURL, branch)
wg.Done()
}()
}
select {
case <-ctx.Done():
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
_, _ = fmt.Fprint(os.Stderr, "\033[31mERROR: project creation timed out\033[m\n")
return
}
_, _ = fmt.Fprintf(os.Stderr, "\033[31mERROR: failed to create project(%s)\033[m\n", ctx.Err().Error())
case err = <-done:

wg.Wait()
close(done)

// Read errors from the done channel until it's closed
for err := range done {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "\033[31mERROR: Failed to create project(%s)\033[m\n", err.Error())
}
}
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
_, _ = fmt.Fprint(os.Stderr, "\033[31mERROR: project creation timed out\033[m\n")
return
}
}

func processProjectParams(projectName string, workingDir string) (projectNameResult, workingDirResult string) {
Expand Down

0 comments on commit 5197d9a

Please sign in to comment.