Skip to content

Commit

Permalink
feat(run): parallel installs
Browse files Browse the repository at this point in the history
  • Loading branch information
ekristen committed Jan 23, 2025
1 parent 9cb98d6 commit d94e647
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions pkg/commands/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"os"
"path/filepath"
"sync"

"github.com/apex/log"
"github.com/urfave/cli/v2"
Expand Down Expand Up @@ -69,16 +70,36 @@ func Execute(c *cli.Context) error {

instCmd := common.GetCommand("install")

didError := false
parallel := c.Int("parallel")

if parallel > 1 {
log.Warn("experimental feature: you are using parallel installs, it might not work as expected")
log.Warn("experimental feature: all logging output will be mixed together")
}

var wg sync.WaitGroup
errCh := make(chan error, len(commands))

sem := make(chan struct{}, parallel)

for _, command := range commands {
if command.Action == "install" {
ctx := cli.NewContext(c.App, nil, nil)
a := []string{"install"}
a = append(a, command.Args...)
if installErr := instCmd.Run(ctx, a...); installErr != nil {
didError = true
log.WithError(installErr).Error("error running install command")
}
wg.Add(1)
go func(command distfile.Command) {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()

ctx := cli.NewContext(c.App, nil, nil)
args := append([]string{"install"}, command.Args...)
if installErr := instCmd.Run(ctx, args...); installErr != nil {
errCh <- installErr
log.WithError(installErr).Error("error running install command")
}
}(command)
} else {
// this is for any other action that's detected that we don't support right now
wg.Add(-1)

Check failure on line 102 in pkg/commands/run/run.go

View workflow job for this annotation

GitHub Actions / golangci-lint

wrapperFunc: use WaitGroup.Done method in `wg.Add(-1)` (gocritic)
}

select {
Expand All @@ -89,6 +110,16 @@ func Execute(c *cli.Context) error {
}
}

wg.Wait()
close(errCh)

var didError bool
for err := range errCh {
if err != nil {
didError = true
}
}

if didError {
return errors.New("one or more install commands failed")
}
Expand All @@ -97,13 +128,22 @@ func Execute(c *cli.Context) error {
}

func init() {
flags := []cli.Flag{
&cli.IntFlag{
Name: "parallel",
Aliases: []string{"p"},
Usage: "EXPERIMENTAL FEATURE: number of parallel installs to run",
Value: 1,
},
}

cmd := &cli.Command{
Name: "run",
Usage: "run [Distfile]",
Description: `run a Distfile to install binaries`,
Action: Execute,
Before: common.Before,
Flags: common.Flags(),
Flags: append(flags, common.Flags()...),
}

common.RegisterCommand(cmd)
Expand Down

0 comments on commit d94e647

Please sign in to comment.