github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/command/v7/run_task_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/command/flag"
     7  	"code.cloudfoundry.org/cli/resources"
     8  )
     9  
    10  type RunTaskCommand struct {
    11  	BaseCommand
    12  
    13  	RequiredArgs    flag.RunTaskArgsV7      `positional-args:"yes"`
    14  	Command         string                  `long:"command" short:"c" description:"The command to execute"`
    15  	Disk            flag.Megabytes          `short:"k" description:"Disk limit (e.g. 256M, 1024M, 1G)"`
    16  	LogRateLimit    flag.BytesWithUnlimited `short:"l" description:"Log rate limit per second, in bytes (e.g. 128B, 4K, 1M). -l=-1 represents unlimited"`
    17  	Memory          flag.Megabytes          `short:"m" description:"Memory limit (e.g. 256M, 1024M, 1G)"`
    18  	Name            string                  `long:"name" description:"Name to give the task (generated if omitted)"`
    19  	Process         string                  `long:"process" description:"Process type to use as a template for command, memory, and disk for the created task."`
    20  	Wait            bool                    `long:"wait" short:"w" description:"Wait for the task to complete before exiting"`
    21  	usage           interface{}             `usage:"CF_NAME run-task APP_NAME [--command COMMAND] [-k DISK] [-m MEMORY] [-l LOG_RATE_LIMIT] [--name TASK_NAME] [--process PROCESS_TYPE]\n\nTIP:\n   Use 'cf logs' to display the logs of the app and all its tasks. If your task name is unique, grep this command's output for the task name to view task-specific logs.\n\nEXAMPLES:\n   CF_NAME run-task my-app --command \"bundle exec rake db:migrate\" --name migrate\n\n   CF_NAME run-task my-app --process batch_job\n\n   CF_NAME run-task my-app"`
    22  	relatedCommands interface{}             `related_commands:"logs, tasks, terminate-task"`
    23  }
    24  
    25  func (cmd RunTaskCommand) Execute(args []string) error {
    26  	err := cmd.SharedActor.CheckTarget(true, true)
    27  	if err != nil {
    28  		return err
    29  	}
    30  
    31  	space := cmd.Config.TargetedSpace()
    32  
    33  	user, err := cmd.Actor.GetCurrentUser()
    34  	if err != nil {
    35  		return err
    36  	}
    37  
    38  	application, warnings, err := cmd.Actor.GetApplicationByNameAndSpace(cmd.RequiredArgs.AppName, space.GUID)
    39  	cmd.UI.DisplayWarnings(warnings)
    40  	if err != nil {
    41  		return err
    42  	}
    43  
    44  	cmd.UI.DisplayTextWithFlavor("Creating task for app {{.AppName}} in org {{.OrgName}} / space {{.SpaceName}} as {{.CurrentUser}}...", map[string]interface{}{
    45  		"AppName":     cmd.RequiredArgs.AppName,
    46  		"OrgName":     cmd.Config.TargetedOrganization().Name,
    47  		"SpaceName":   space.Name,
    48  		"CurrentUser": user.Name,
    49  	})
    50  
    51  	inputTask := resources.Task{
    52  		Command: cmd.Command,
    53  	}
    54  
    55  	if cmd.Name != "" {
    56  		inputTask.Name = cmd.Name
    57  	}
    58  	if cmd.Disk.IsSet {
    59  		inputTask.DiskInMB = cmd.Disk.Value
    60  	}
    61  	if cmd.Memory.IsSet {
    62  		inputTask.MemoryInMB = cmd.Memory.Value
    63  	}
    64  	if cmd.LogRateLimit.IsSet {
    65  		inputTask.LogRateLimitInBPS = cmd.LogRateLimit.Value
    66  	}
    67  	if cmd.Command == "" && cmd.Process == "" {
    68  		cmd.Process = "task"
    69  	}
    70  	if cmd.Process != "" {
    71  		process, warnings, err := cmd.Actor.GetProcessByTypeAndApplication(cmd.Process, application.GUID)
    72  		cmd.UI.DisplayWarnings(warnings)
    73  		if err != nil {
    74  			return err
    75  		}
    76  
    77  		inputTask.Template = &resources.TaskTemplate{
    78  			Process: resources.TaskProcessTemplate{
    79  				Guid: process.GUID,
    80  			},
    81  		}
    82  	}
    83  
    84  	task, warnings, err := cmd.Actor.RunTask(application.GUID, inputTask)
    85  	cmd.UI.DisplayWarnings(warnings)
    86  	if err != nil {
    87  		return err
    88  	}
    89  
    90  	cmd.UI.DisplayText("Task has been submitted successfully for execution.")
    91  	cmd.UI.DisplayOK()
    92  
    93  	cmd.UI.DisplayKeyValueTable("", [][]string{
    94  		{cmd.UI.TranslateText("task name:"), task.Name},
    95  		{cmd.UI.TranslateText("task id:"), fmt.Sprint(task.SequenceID)},
    96  	}, 3)
    97  
    98  	if cmd.Wait {
    99  		cmd.UI.DisplayNewline()
   100  		cmd.UI.DisplayText("Waiting for task to complete execution...")
   101  
   102  		_, pollWarnings, err := cmd.Actor.PollTask(task)
   103  		cmd.UI.DisplayWarnings(pollWarnings)
   104  		if err != nil {
   105  			return err
   106  		}
   107  
   108  		cmd.UI.DisplayNewline()
   109  		cmd.UI.DisplayText("Task has completed successfully.")
   110  	}
   111  
   112  	cmd.UI.DisplayOK()
   113  
   114  	return nil
   115  }