github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/model/v1beta1/job_state.go (about) 1 package v1beta1 2 3 import ( 4 "fmt" 5 ) 6 7 // JobStateType is the state of a job on a particular node. Note that the job 8 // will typically have different states on different nodes. 9 // 10 //go:generate stringer -type=JobStateType --trimprefix=JobState 11 type JobStateType int 12 13 // these are the states a job can be in against a single node 14 const ( 15 jobStateUnknown JobStateType = iota // must be first 16 17 // a compute node has selected a job and has bid on it 18 // we are currently waiting to hear back from the requester 19 // node whether our bid was accepted or not 20 JobStateBidding 21 22 // the bid has been accepted but we have not yet started the job 23 JobStateWaiting 24 25 // the job is in the process of running 26 JobStateRunning 27 28 // the compute node has finished execution and has communicated the ResultsProposal 29 JobStateVerifying 30 31 // a requester node has either rejected the bid or the compute node has canceled the bid 32 // either way - this node will not progress with this job any more 33 JobStateCancelled 34 35 // the job had an error - this is an end state 36 JobStateError 37 38 // our results have been processed and published 39 JobStateCompleted 40 41 jobStateDone // must be last 42 ) 43 44 // IsTerminal returns true if the given job type signals the end of the 45 // lifecycle of that job on a particular node. After this, the job can be 46 // safely ignored by the node. 47 func (s JobStateType) IsTerminal() bool { 48 return s == JobStateCompleted || s == JobStateError || s == JobStateCancelled 49 } 50 51 // IsComplete returns true if the given job has succeeded at the bid stage 52 // and has finished running the job - this is used to calculate if a job 53 // has completed across all nodes because a cancelation does not count 54 // towards actually "running" the job whereas an error does (even though it failed 55 // it still "ran") 56 func (s JobStateType) IsComplete() bool { 57 return s == JobStateCompleted || s == JobStateError 58 } 59 60 func (s JobStateType) HasPassedBidAcceptedStage() bool { 61 return s == JobStateWaiting || s == JobStateRunning || s == JobStateVerifying || s == JobStateError || s == JobStateCompleted 62 } 63 64 func (s JobStateType) IsError() bool { 65 return s == JobStateError 66 } 67 68 // tells you if this event is a valid one 69 func IsValidJobState(s JobStateType) bool { 70 return s > jobStateUnknown && s < jobStateDone 71 } 72 73 func ParseJobStateType(str string) (JobStateType, error) { 74 for typ := jobStateUnknown + 1; typ < jobStateDone; typ++ { 75 if equal(typ.String(), str) { 76 return typ, nil 77 } 78 } 79 80 return jobStateUnknown, fmt.Errorf( 81 "executor: unknown job typ type '%s'", str) 82 } 83 84 func JobStateTypes() []JobStateType { 85 var res []JobStateType 86 for typ := jobStateUnknown + 1; typ < jobStateDone; typ++ { 87 res = append(res, typ) 88 } 89 90 return res 91 } 92 93 func JobStateTypeNames() []string { 94 var names []string 95 for _, typ := range JobStateTypes() { 96 names = append(names, typ.String()) 97 } 98 return names 99 } 100 101 func (s JobStateType) MarshalText() ([]byte, error) { 102 return []byte(s.String()), nil 103 } 104 105 func (s *JobStateType) UnmarshalText(text []byte) (err error) { 106 name := string(text) 107 *s, err = ParseJobStateType(name) 108 return 109 } 110 111 // given an event name - return a job state 112 func GetStateFromEvent(eventType JobEventType) JobStateType { 113 switch eventType { 114 // we have bid and are waiting to hear if that has been accepted 115 case JobEventBid: 116 return JobStateBidding 117 118 // our bid has been accepted but we've not yet started the job 119 case JobEventBidAccepted: 120 return JobStateWaiting 121 122 // out bid got rejected so we are canceled 123 case JobEventBidRejected: 124 return JobStateCancelled 125 126 // we canceled our bid so we are canceled 127 case JobEventBidCancelled: 128 return JobStateCancelled 129 130 // we are running 131 case JobEventRunning: 132 return JobStateRunning 133 134 // yikes 135 case JobEventError, JobEventComputeError, JobEventInvalidRequest: 136 return JobStateError 137 138 // we are complete 139 case JobEventResultsProposed: 140 return JobStateVerifying 141 142 // both of these are "finalized" 143 case JobEventResultsAccepted: 144 return JobStateVerifying 145 146 case JobEventResultsRejected: 147 return JobStateError 148 149 case JobEventResultsPublished: 150 return JobStateCompleted 151 152 default: 153 return jobStateUnknown 154 } 155 }