/
job_state.go
120 lines (99 loc) · 3.7 KB
/
job_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//go:generate stringer -type=JobStateType --trimprefix=JobState --output job_state_string.go
package model
import (
"time"
"github.com/samber/lo"
)
// JobStateType The state of a job across the whole network that represents an aggregate view across
// the executions and nodes.
type JobStateType int
// these are the states a job can be in against a single node
const (
JobStateUndefined JobStateType = iota
JobStateNew
JobStateInProgress
// Job is canceled by the user.
JobStateCancelled
// Job have failed
JobStateError
// Job completed successfully
JobStateCompleted
// Job is waiting to be scheduled.
JobStateQueued
)
// IsUndefined returns true if the job state is undefined
func (s JobStateType) IsUndefined() bool {
return s == JobStateUndefined
}
// IsTerminal returns true if the given job type signals the end of the lifecycle of
// that job and that no change in the state can be expected.
func (s JobStateType) IsTerminal() bool {
return s == JobStateCompleted || s == JobStateError || s == JobStateCancelled
}
func (s JobStateType) MarshalText() ([]byte, error) {
return []byte(s.String()), nil
}
func (s *JobStateType) UnmarshalText(text []byte) (err error) {
name := string(text)
for typ := JobStateNew; typ <= JobStateQueued; typ++ {
if equal(typ.String(), name) {
*s = typ
return
}
}
return
}
func JobStateTypes() []JobStateType {
var res []JobStateType
for typ := JobStateNew; typ <= JobStateQueued; typ++ {
res = append(res, typ)
}
return res
}
// JobState The state of a job across the whole network that represents an aggregate view across
// the executions and nodes.
type JobState struct {
// JobID is the unique identifier for the job
JobID string `json:"JobID"`
// Executions is a list of executions of the job across the nodes.
// A new execution is created when a node is selected to execute the job, and a node can have multiple executions for the same
// job due to retries, but there can only be a single active execution per node at any given time.
Executions []ExecutionState `json:"Executions"`
// State is the current state of the job
State JobStateType `json:"State"`
// Version is the version of the job state. It is incremented every time the job state is updated.
Version int `json:"Version"`
// CreateTime is the time when the job was created.
CreateTime time.Time `json:"CreateTime"`
// UpdateTime is the time when the job state was last updated.
UpdateTime time.Time `json:"UpdateTime"`
// TimeoutAt is the time when the job will be timed out if it is not completed.
TimeoutAt time.Time `json:"TimeoutAt,omitempty"`
}
// GroupExecutionsByState groups the executions by state
func (s *JobState) GroupExecutionsByState() map[ExecutionStateType][]ExecutionState {
result := make(map[ExecutionStateType][]ExecutionState)
for _, execution := range s.Executions {
result[execution.State] = append(result[execution.State], execution)
}
return result
}
func (s *JobState) NonDiscardedCount() int {
return lo.CountBy(s.Executions, func(item ExecutionState) bool { return !item.State.IsDiscarded() })
}
func (s *JobState) CompletedCount() int {
return lo.CountBy(s.Executions, func(item ExecutionState) bool { return item.State == ExecutionStateCompleted })
}
func (s *JobState) ActiveCount() int {
return lo.CountBy(s.Executions, func(item ExecutionState) bool { return item.State.IsActive() })
}
// NonTerminalExecutions returns the executions that are not in a terminal state.
func (s *JobState) NonTerminalExecutions() []*ExecutionState {
executionStates := make([]*ExecutionState, 0, len(s.Executions))
for i := range s.Executions {
if !s.Executions[i].State.IsTerminal() {
executionStates = append(executionStates, &s.Executions[i])
}
}
return executionStates
}