Skip to content

Commit 7cc022f

Browse files
committed
Add --max-concurrent-tasks to exec
1 parent 130b6bd commit 7cc022f

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

cmd/exec.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import (
88
"os/exec"
99
"path/filepath"
1010
"strings"
11-
"sync"
1211
"time"
1312

1413
"github.com/creack/pty"
1514
"github.com/gookit/color"
1615
"github.com/segmentio/textio"
1716
log "github.com/sirupsen/logrus"
1817
"github.com/spf13/cobra"
18+
"golang.org/x/sync/errgroup"
1919

2020
"github.com/gitpod-io/leeway/pkg/leeway"
2121
)
@@ -60,6 +60,7 @@ Example use:
6060
parallel, _ = cmd.Flags().GetBool("parallel")
6161
rawOutput, _ = cmd.Flags().GetBool("raw-output")
6262
cacheKey, _ = cmd.Flags().GetString("cache-key")
63+
maxConcurrentTasks, _ = cmd.Flags().GetUint("max-concurrent-tasks")
6364
)
6465

6566
ws, err := getWorkspace()
@@ -162,8 +163,18 @@ Example use:
162163
}
163164
}
164165

166+
var parallelism int
167+
if parallel {
168+
parallelism = int(maxConcurrentTasks)
169+
} else {
170+
parallelism = -1
171+
if maxConcurrentTasks > 0 {
172+
log.Warn("max-concurrent-tasks is ignored when not running in parallel")
173+
}
174+
}
175+
165176
if watch {
166-
err := executeCommandInLocations(args, locs, noExecCache{}, parallel, rawOutput)
177+
err := executeCommandInLocations(args, locs, noExecCache{}, parallelism, rawOutput)
167178
if err != nil {
168179
log.Error(err)
169180
}
@@ -172,7 +183,7 @@ Example use:
172183
for {
173184
select {
174185
case <-evt:
175-
err := executeCommandInLocations(args, locs, noExecCache{}, parallel, rawOutput)
186+
err := executeCommandInLocations(args, locs, noExecCache{}, parallelism, rawOutput)
176187
if err != nil {
177188
log.Error(err)
178189
}
@@ -194,7 +205,7 @@ Example use:
194205
cache = filesystemExecCache(loc)
195206
}
196207

197-
err = executeCommandInLocations(args, locs, cache, parallel, rawOutput)
208+
err = executeCommandInLocations(args, locs, cache, parallelism, rawOutput)
198209
if err != nil {
199210
log.WithError(err).Fatal("cannot execut command")
200211
}
@@ -208,8 +219,12 @@ type commandExecLocation struct {
208219
Name string
209220
}
210221

211-
func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation, cache execCache, parallel, rawOutput bool) error {
212-
var wg sync.WaitGroup
222+
func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation, cache execCache, parallelism int, rawOutput bool) error {
223+
var eg errgroup.Group
224+
if parallelism > 0 {
225+
eg.SetLimit(parallelism)
226+
}
227+
213228
for _, loc := range locs {
214229
if ok, _ := cache.NeedsExecution(context.Background(), loc.Package); !ok {
215230
continue
@@ -247,11 +262,9 @@ func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation,
247262
go io.Copy(textio.NewPrefixWriter(os.Stdout, prefix), ptmx)
248263
//nolint:errcheck
249264
go io.Copy(ptmx, os.Stdin)
250-
if parallel {
251-
wg.Add(1)
252-
go func(loc commandExecLocation) {
253-
defer wg.Done()
254-
265+
if parallelism > -1 {
266+
loc := loc
267+
eg.Go(func() error {
255268
err := cmd.Wait()
256269
if err == nil {
257270
err = cache.MarkExecuted(context.Background(), loc.Package)
@@ -261,7 +274,8 @@ func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation,
261274
} else {
262275
log.Errorf("execution failed in %s (%s): %v", loc.Name, loc.Dir, err)
263276
}
264-
}(loc)
277+
return nil
278+
})
265279
} else {
266280
err = cmd.Wait()
267281
if err == nil {
@@ -274,8 +288,8 @@ func executeCommandInLocations(rawExecCmd []string, locs []commandExecLocation,
274288
}
275289
}
276290
}
277-
if parallel {
278-
wg.Wait()
291+
if parallelism > -1 {
292+
_ = eg.Wait()
279293
}
280294

281295
return nil
@@ -295,6 +309,7 @@ func init() {
295309
execCmd.Flags().Bool("parallel", false, "Start all executions in parallel independent of their order")
296310
execCmd.Flags().Bool("raw-output", false, "Produce output without package prefix")
297311
execCmd.Flags().String("cache-key", "", "Specify a cache key to provide package-cache like execution behaviour")
312+
execCmd.Flags().UintP("max-concurrent-tasks", "j", 0, "Maximum number of concurrent tasks - 0 means unlimited")
298313
execCmd.Flags().SetInterspersed(true)
299314
}
300315

0 commit comments

Comments
 (0)