Files
odo/pkg/libdevfile/command_composite_parallel.go
Armel Soro 00d39889b7 Make sure to run parallel commands part of a composite command in parallel (#7075)
* Make sure to run parallel commands part of a composite command in parallel

* Display warnings in case there are errors when executing pre-stop events

* Fix the command_composite_parallel.go implementation by lowering the case of the sub-command names

Since this passed the Devfile validation logic, we should use the same logic as in command_composite.go
2023-09-06 21:15:01 +02:00

74 lines
2.1 KiB
Go

package libdevfile
import (
"context"
"fmt"
"strings"
"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
"github.com/devfile/library/v2/pkg/devfile/parser"
"github.com/redhat-developer/odo/pkg/util"
)
// parallelCompositeCommand is a command implementation that represents parallel composite commands
type parallelCompositeCommand struct {
command v1alpha2.Command
devfileObj parser.DevfileObj
}
var _ command = (*parallelCompositeCommand)(nil)
// newParallelCompositeCommand creates a new command implementation which will execute the provided commands in parallel
func newParallelCompositeCommand(devfileObj parser.DevfileObj, command v1alpha2.Command) *parallelCompositeCommand {
return &parallelCompositeCommand{
command: command,
devfileObj: devfileObj,
}
}
func (o *parallelCompositeCommand) CheckValidity() error {
allCommands, err := allCommandsMap(o.devfileObj)
if err != nil {
return err
}
cmds := o.command.Composite.Commands
for _, cmd := range cmds {
if _, ok := allCommands[strings.ToLower(cmd)]; !ok {
return fmt.Errorf("composite command %q has command %v not found in devfile", cmd, o.command.Id)
}
}
return nil
}
// Execute loops over each command and executes them in parallel
func (o *parallelCompositeCommand) Execute(ctx context.Context, handler Handler, parentGroup *v1alpha2.CommandGroup) error {
allCommands, err := allCommandsMap(o.devfileObj)
if err != nil {
return err
}
if parentGroup == nil {
parentGroup = o.command.Composite.Group
}
commandExecs := util.NewConcurrentTasks(len(o.command.Composite.Commands))
for _, devfileCmd := range o.command.Composite.Commands {
cmd, err2 := newCommand(o.devfileObj, allCommands[strings.ToLower(devfileCmd)])
if err2 != nil {
return err2
}
commandExecs.Add(util.ConcurrentTask{
ToRun: func(errChannel chan error) {
err3 := cmd.Execute(ctx, handler, parentGroup)
if err3 != nil {
errChannel <- err3
}
},
})
}
err = commandExecs.Run()
if err != nil {
return fmt.Errorf("parallel command execution failed: %w", err)
}
return nil
}