feat: add cli option to set concurrent jobs count (#2762)

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
Bengt Brodersen
2025-05-23 10:05:42 +02:00
committed by GitHub
parent b5469ac4ca
commit 16b86a64a9
3 changed files with 20 additions and 6 deletions

View File

@@ -62,6 +62,7 @@ type Input struct {
useNewActionCache bool useNewActionCache bool
localRepository []string localRepository []string
listOptions bool listOptions bool
concurrentJobs int
} }
func (i *Input) resolve(path string) string { func (i *Input) resolve(path string) string {

View File

@@ -127,6 +127,7 @@ func createRootCommand(ctx context.Context, input *Input, version string) *cobra
rootCmd.PersistentFlags().BoolVarP(&input.useNewActionCache, "use-new-action-cache", "", false, "Enable using the new Action Cache for storing Actions locally") rootCmd.PersistentFlags().BoolVarP(&input.useNewActionCache, "use-new-action-cache", "", false, "Enable using the new Action Cache for storing Actions locally")
rootCmd.PersistentFlags().StringArrayVarP(&input.localRepository, "local-repository", "", []string{}, "Replaces the specified repository and ref with a local folder (e.g. https://github.com/test/test@v0=/home/act/test or test/test@v0=/home/act/test, the latter matches any hosts or protocols)") rootCmd.PersistentFlags().StringArrayVarP(&input.localRepository, "local-repository", "", []string{}, "Replaces the specified repository and ref with a local folder (e.g. https://github.com/test/test@v0=/home/act/test or test/test@v0=/home/act/test, the latter matches any hosts or protocols)")
rootCmd.PersistentFlags().BoolVar(&input.listOptions, "list-options", false, "Print a json structure of compatible options") rootCmd.PersistentFlags().BoolVar(&input.listOptions, "list-options", false, "Print a json structure of compatible options")
rootCmd.PersistentFlags().IntVar(&input.concurrentJobs, "concurrent-jobs", 0, "Maximum number of concurrent jobs to run. Default is the number of CPUs available.")
rootCmd.SetArgs(args()) rootCmd.SetArgs(args())
return rootCmd return rootCmd
} }
@@ -635,6 +636,7 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str
ReplaceGheActionTokenWithGithubCom: input.replaceGheActionTokenWithGithubCom, ReplaceGheActionTokenWithGithubCom: input.replaceGheActionTokenWithGithubCom,
Matrix: matrixes, Matrix: matrixes,
ContainerNetworkMode: docker_container.NetworkMode(input.networkName), ContainerNetworkMode: docker_container.NetworkMode(input.networkName),
ConcurrentJobs: input.concurrentJobs,
} }
if input.useNewActionCache || len(input.localRepository) > 0 { if input.useNewActionCache || len(input.localRepository) > 0 {
if input.actionOfflineMode { if input.actionOfflineMode {

View File

@@ -61,6 +61,20 @@ type Config struct {
Matrix map[string]map[string]bool // Matrix config to run Matrix map[string]map[string]bool // Matrix config to run
ContainerNetworkMode docker_container.NetworkMode // the network mode of job containers (the value of --network) ContainerNetworkMode docker_container.NetworkMode // the network mode of job containers (the value of --network)
ActionCache ActionCache // Use a custom ActionCache Implementation ActionCache ActionCache // Use a custom ActionCache Implementation
ConcurrentJobs int // Number of max concurrent jobs
}
func (config *Config) GetConcurrentJobs() int {
if config.ConcurrentJobs >= 1 {
return config.ConcurrentJobs
}
ncpu := runtime.NumCPU()
log.Debugf("Detected CPUs: %d", ncpu)
if ncpu > 1 {
return ncpu
}
return 1
} }
type caller struct { type caller struct {
@@ -195,12 +209,9 @@ func (runner *runnerImpl) NewPlanExecutor(plan *model.Plan) common.Executor {
} }
pipeline = append(pipeline, common.NewParallelExecutor(maxParallel, stageExecutor...)) pipeline = append(pipeline, common.NewParallelExecutor(maxParallel, stageExecutor...))
} }
ncpu := runtime.NumCPU()
if 1 > ncpu { log.Debugf("PlanExecutor concurrency: %d", runner.config.GetConcurrentJobs())
ncpu = 1 return common.NewParallelExecutor(runner.config.GetConcurrentJobs(), pipeline...)(ctx)
}
log.Debugf("Detected CPUs: %d", ncpu)
return common.NewParallelExecutor(ncpu, pipeline...)(ctx)
}) })
} }