...

Source file src/github.com/redhat-developer/odo/pkg/libdevfile/command_composite_parallel.go

Documentation: github.com/redhat-developer/odo/pkg/libdevfile

     1  package libdevfile
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2"
     9  	"github.com/devfile/library/v2/pkg/devfile/parser"
    10  
    11  	"github.com/redhat-developer/odo/pkg/util"
    12  )
    13  
    14  // parallelCompositeCommand is a command implementation that represents parallel composite commands
    15  type parallelCompositeCommand struct {
    16  	command    v1alpha2.Command
    17  	devfileObj parser.DevfileObj
    18  }
    19  
    20  var _ command = (*parallelCompositeCommand)(nil)
    21  
    22  // newParallelCompositeCommand creates a new command implementation which will execute the provided commands in parallel
    23  func newParallelCompositeCommand(devfileObj parser.DevfileObj, command v1alpha2.Command) *parallelCompositeCommand {
    24  	return &parallelCompositeCommand{
    25  		command:    command,
    26  		devfileObj: devfileObj,
    27  	}
    28  }
    29  
    30  func (o *parallelCompositeCommand) CheckValidity() error {
    31  	allCommands, err := allCommandsMap(o.devfileObj)
    32  	if err != nil {
    33  		return err
    34  	}
    35  	cmds := o.command.Composite.Commands
    36  	for _, cmd := range cmds {
    37  		if _, ok := allCommands[strings.ToLower(cmd)]; !ok {
    38  			return fmt.Errorf("composite command %q has command %v not found in devfile", cmd, o.command.Id)
    39  		}
    40  	}
    41  	return nil
    42  }
    43  
    44  // Execute loops over each command and executes them in parallel
    45  func (o *parallelCompositeCommand) Execute(ctx context.Context, handler Handler, parentGroup *v1alpha2.CommandGroup) error {
    46  	allCommands, err := allCommandsMap(o.devfileObj)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	if parentGroup == nil {
    51  		parentGroup = o.command.Composite.Group
    52  	}
    53  	commandExecs := util.NewConcurrentTasks(len(o.command.Composite.Commands))
    54  	for _, devfileCmd := range o.command.Composite.Commands {
    55  		cmd, err2 := newCommand(o.devfileObj, allCommands[strings.ToLower(devfileCmd)])
    56  		if err2 != nil {
    57  			return err2
    58  		}
    59  		commandExecs.Add(util.ConcurrentTask{
    60  			ToRun: func(errChannel chan error) {
    61  				err3 := cmd.Execute(ctx, handler, parentGroup)
    62  				if err3 != nil {
    63  					errChannel <- err3
    64  				}
    65  			},
    66  		})
    67  	}
    68  	err = commandExecs.Run()
    69  	if err != nil {
    70  		return fmt.Errorf("parallel command execution failed: %w", err)
    71  	}
    72  	return nil
    73  }
    74  

View as plain text