github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/actor/v7action/process_instance.go (about)

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  )
    11  
    12  type ProcessInstance ccv3.ProcessInstance
    13  
    14  // Running will return true if the instance is running.
    15  func (instance ProcessInstance) Running() bool {
    16  	return instance.State == constant.ProcessInstanceRunning
    17  }
    18  
    19  // StartTime returns the time that the instance started.
    20  func (instance *ProcessInstance) StartTime() time.Time {
    21  	uptimeDuration := time.Duration(instance.Uptime) * time.Second
    22  
    23  	return time.Now().Add(-uptimeDuration)
    24  }
    25  
    26  type ProcessInstances []ccv3.ProcessInstance
    27  
    28  func (pi ProcessInstances) AllCrashed() bool {
    29  	for _, instance := range pi {
    30  		if instance.State != constant.ProcessInstanceCrashed {
    31  			return false
    32  		}
    33  	}
    34  	return true
    35  }
    36  
    37  func (pi ProcessInstances) AnyRunning() bool {
    38  	for _, instance := range pi {
    39  		if instance.State == constant.ProcessInstanceRunning {
    40  			return true
    41  		}
    42  	}
    43  	return false
    44  }
    45  
    46  func (pi ProcessInstances) Empty() bool {
    47  	return len(pi) == 0
    48  }
    49  
    50  func (actor Actor) DeleteInstanceByApplicationNameSpaceProcessTypeAndIndex(appName string, spaceGUID string, processType string, instanceIndex int) (Warnings, error) {
    51  	var allWarnings Warnings
    52  	app, appWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    53  	allWarnings = append(allWarnings, appWarnings...)
    54  	if err != nil {
    55  		return allWarnings, err
    56  	}
    57  
    58  	deleteWarnings, err := actor.CloudControllerClient.DeleteApplicationProcessInstance(app.GUID, processType, instanceIndex)
    59  	allWarnings = append(allWarnings, deleteWarnings...)
    60  
    61  	if err != nil {
    62  		switch err.(type) {
    63  		case ccerror.ProcessNotFoundError:
    64  			return allWarnings, actionerror.ProcessNotFoundError{
    65  				ProcessType: processType,
    66  			}
    67  		case ccerror.InstanceNotFoundError:
    68  			return allWarnings, actionerror.ProcessInstanceNotFoundError{
    69  				ProcessType:   processType,
    70  				InstanceIndex: uint(instanceIndex),
    71  			}
    72  		default:
    73  			return allWarnings, err
    74  		}
    75  	}
    76  
    77  	return allWarnings, nil
    78  }