github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/task.go (about)

     1  package v7action
     2  
     3  import (
     4  	"strconv"
     5  	"time"
     6  
     7  	"sort"
     8  
     9  	"code.cloudfoundry.org/cli/actor/actionerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    12  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    13  	"code.cloudfoundry.org/cli/resources"
    14  	log "github.com/sirupsen/logrus"
    15  )
    16  
    17  // Run resources.Task runs the provided command in the application environment associated
    18  // with the provided application GUID.
    19  func (actor Actor) RunTask(appGUID string, task resources.Task) (resources.Task, Warnings, error) {
    20  	createdTask, warnings, err := actor.CloudControllerClient.CreateApplicationTask(appGUID, resources.Task(task))
    21  	if err != nil {
    22  		if e, ok := err.(ccerror.TaskWorkersUnavailableError); ok {
    23  			return resources.Task{}, Warnings(warnings), actionerror.TaskWorkersUnavailableError{Message: e.Error()}
    24  		}
    25  	}
    26  
    27  	return resources.Task(createdTask), Warnings(warnings), err
    28  }
    29  
    30  // GetApplicationTasks returns a list of tasks associated with the provided
    31  // application GUID.
    32  func (actor Actor) GetApplicationTasks(appGUID string, sortOrder SortOrder) ([]resources.Task, Warnings, error) {
    33  	tasks, warnings, err := actor.CloudControllerClient.GetApplicationTasks(appGUID)
    34  	actorWarnings := Warnings(warnings)
    35  	if err != nil {
    36  		return nil, actorWarnings, err
    37  	}
    38  
    39  	allTasks := []resources.Task{}
    40  	for _, task := range tasks {
    41  		allTasks = append(allTasks, resources.Task(task))
    42  	}
    43  
    44  	if sortOrder == Descending {
    45  		sort.Slice(allTasks, func(i int, j int) bool { return allTasks[i].SequenceID > allTasks[j].SequenceID })
    46  	} else {
    47  		sort.Slice(allTasks, func(i int, j int) bool { return allTasks[i].SequenceID < allTasks[j].SequenceID })
    48  	}
    49  
    50  	return allTasks, actorWarnings, nil
    51  }
    52  
    53  func (actor Actor) GetTaskBySequenceIDAndApplication(sequenceID int, appGUID string) (resources.Task, Warnings, error) {
    54  	tasks, warnings, err := actor.CloudControllerClient.GetApplicationTasks(
    55  		appGUID,
    56  		ccv3.Query{Key: ccv3.SequenceIDFilter, Values: []string{strconv.Itoa(sequenceID)}},
    57  		ccv3.Query{Key: ccv3.PerPage, Values: []string{"1"}},
    58  		ccv3.Query{Key: ccv3.Page, Values: []string{"1"}},
    59  	)
    60  	if err != nil {
    61  		return resources.Task{}, Warnings(warnings), err
    62  	}
    63  
    64  	if len(tasks) == 0 {
    65  		return resources.Task{}, Warnings(warnings), actionerror.TaskNotFoundError{SequenceID: sequenceID}
    66  	}
    67  
    68  	return resources.Task(tasks[0]), Warnings(warnings), nil
    69  }
    70  
    71  func (actor Actor) TerminateTask(taskGUID string) (resources.Task, Warnings, error) {
    72  	task, warnings, err := actor.CloudControllerClient.UpdateTaskCancel(taskGUID)
    73  	return resources.Task(task), Warnings(warnings), err
    74  }
    75  
    76  func (actor Actor) PollTask(task resources.Task) (resources.Task, Warnings, error) {
    77  	var allWarnings Warnings
    78  
    79  	for task.State != constant.TaskSucceeded && task.State != constant.TaskFailed {
    80  
    81  		time.Sleep(actor.Config.PollingInterval())
    82  
    83  		ccTask, warnings, err := actor.CloudControllerClient.GetTask(task.GUID)
    84  		log.WithFields(log.Fields{
    85  			"task_guid": task.GUID,
    86  			"state":     task.State,
    87  		}).Debug("polling task state")
    88  
    89  		allWarnings = append(allWarnings, warnings...)
    90  
    91  		if err != nil {
    92  			return resources.Task{}, allWarnings, err
    93  		}
    94  
    95  		task = resources.Task(ccTask)
    96  	}
    97  
    98  	if task.State == constant.TaskFailed {
    99  		return task, allWarnings, actionerror.TaskFailedError{}
   100  	}
   101  
   102  	return task, allWarnings, nil
   103  }