github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/client/allocrunner/state/state.go (about)

     1  package state
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/hashicorp/nomad/nomad/structs"
     7  )
     8  
     9  // State captures the state of the allocation runner.
    10  type State struct {
    11  	// ClientStatus captures the overall state of the allocation
    12  	ClientStatus string
    13  
    14  	// ClientDescription is an optional human readable description of the
    15  	// allocations client state
    16  	ClientDescription string
    17  
    18  	// DeploymentStatus captures the status of the deployment
    19  	DeploymentStatus *structs.AllocDeploymentStatus
    20  
    21  	// TaskStates is a snapshot of task states.
    22  	TaskStates map[string]*structs.TaskState
    23  }
    24  
    25  // SetDeploymentStatus is a helper for updating the client-controlled
    26  // DeploymentStatus fields: Healthy and Timestamp. The Canary and ModifyIndex
    27  // fields should only be updated by the server.
    28  func (s *State) SetDeploymentStatus(timestamp time.Time, healthy bool) {
    29  	if s.DeploymentStatus == nil {
    30  		s.DeploymentStatus = &structs.AllocDeploymentStatus{}
    31  	}
    32  
    33  	s.DeploymentStatus.Healthy = &healthy
    34  	s.DeploymentStatus.Timestamp = timestamp
    35  }
    36  
    37  // ClearDeploymentStatus is a helper to clear the client-controlled
    38  // DeploymentStatus fields: Healthy and Timestamp. The Canary and ModifyIndex
    39  // fields should only be updated by the server.
    40  func (s *State) ClearDeploymentStatus() {
    41  	if s.DeploymentStatus == nil {
    42  		return
    43  	}
    44  
    45  	s.DeploymentStatus.Healthy = nil
    46  	s.DeploymentStatus.Timestamp = time.Time{}
    47  }
    48  
    49  // Copy returns a deep copy of State.
    50  func (s *State) Copy() *State {
    51  	taskStates := make(map[string]*structs.TaskState, len(s.TaskStates))
    52  	for k, v := range s.TaskStates {
    53  		taskStates[k] = v.Copy()
    54  	}
    55  	return &State{
    56  		ClientStatus:      s.ClientStatus,
    57  		ClientDescription: s.ClientDescription,
    58  		DeploymentStatus:  s.DeploymentStatus.Copy(),
    59  		TaskStates:        taskStates,
    60  	}
    61  }
    62  
    63  // ClientTerminalStatus returns if the client status is terminal and will no longer transition
    64  func (a *State) ClientTerminalStatus() bool {
    65  	switch a.ClientStatus {
    66  	case structs.AllocClientStatusComplete, structs.AllocClientStatusFailed, structs.AllocClientStatusLost:
    67  		return true
    68  	default:
    69  		return false
    70  	}
    71  }