-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
67 lines (56 loc) · 1.4 KB
/
status.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
package lib
import "sync"
// JobStatus represents the status of a job
type JobStatus int
func (s JobStatus) String() string {
switch s {
case StatusCreated:
return "CREATED"
case StatusRunning:
return "RUNNING"
case StatusCompleted:
return "COMPLETED"
case StatusTimedOut:
return "TIMEDOUT"
case StatusStopped:
return "STOPPED"
}
return "UNKNOWN"
}
const (
// StatusCreated denotes a newly created job that's not yet started
StatusCreated JobStatus = iota - 1
// StatusRunning denotes a job that's in running state
StatusRunning
// StatusCompleted denotes a job that ran to its completion
StatusCompleted
// StatusStopped denotes a job that was stopped by the caller
StatusStopped
// StatusTimedOut denotes a job that was killed due to timeout expiration
StatusTimedOut
)
// safeJobStatus provides a safer way to use JobStatus protecting it with a lock
type safeJobStatus struct {
value JobStatus
sync.RWMutex
}
// Set sets the JobStatus to new value
func (s *safeJobStatus) Set(new JobStatus) {
s.Lock()
defer s.Unlock()
s.value = new
}
// Get returns the current JobStatus
func (s *safeJobStatus) Get() JobStatus {
s.RLock()
defer s.RUnlock()
return s.value
}
// UpdateIf updates the JobStatus to new value only if the existing value is set to old
func (s *safeJobStatus) UpdateIf(old JobStatus, new JobStatus) {
s.Lock()
defer s.Unlock()
if s.value == old {
s.value = new
}
}