-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime_state.go
More file actions
167 lines (138 loc) · 3.25 KB
/
Copy pathruntime_state.go
File metadata and controls
167 lines (138 loc) · 3.25 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package scheduler
import (
"context"
"sort"
"sync"
"time"
"github.com/google/uuid"
)
type JobEventType string
const (
JobStarted JobEventType = "job_started"
JobSucceeded JobEventType = "job_succeeded"
JobFailed JobEventType = "job_failed"
JobSkipped JobEventType = "job_skipped"
)
type JobSkipReason string
const (
JobSkipPaused JobSkipReason = "paused"
)
type JobEvent struct {
Type JobEventType
JobID uuid.UUID
Name string
TargetKind string
Attempt int
Duration time.Duration
Error error
Reason string
OccurredAt time.Time
}
type JobObserver interface {
OnJobEvent(ctx context.Context, event JobEvent)
}
type JobObserverFunc func(ctx context.Context, event JobEvent)
func (f JobObserverFunc) OnJobEvent(ctx context.Context, event JobEvent) { f(ctx, event) }
type runtimeState struct {
mu sync.RWMutex
pausedAll bool
pausedJobs map[uuid.UUID]bool
metadata map[uuid.UUID]JobMetadata
attempts map[uuid.UUID]int
observers []JobObserver
}
func newRuntimeState() *runtimeState {
return &runtimeState{
pausedJobs: make(map[uuid.UUID]bool),
metadata: make(map[uuid.UUID]JobMetadata),
attempts: make(map[uuid.UUID]int),
}
}
func (s *runtimeState) pauseAll() {
s.mu.Lock()
s.pausedAll = true
s.mu.Unlock()
}
func (s *runtimeState) resumeAll() {
s.mu.Lock()
s.pausedAll = false
s.mu.Unlock()
}
func (s *runtimeState) isPausedAll() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.pausedAll
}
func (s *runtimeState) pauseJob(id uuid.UUID) {
s.mu.Lock()
s.pausedJobs[id] = true
s.mu.Unlock()
}
func (s *runtimeState) resumeJob(id uuid.UUID) {
s.mu.Lock()
delete(s.pausedJobs, id)
s.mu.Unlock()
}
func (s *runtimeState) isJobPaused(id uuid.UUID) bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.pausedJobs[id]
}
func (s *runtimeState) isExecutionPaused(id uuid.UUID) bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.pausedAll || s.pausedJobs[id]
}
func (s *runtimeState) setMetadata(meta JobMetadata) {
s.mu.Lock()
s.metadata[meta.ID] = meta
s.mu.Unlock()
}
func (s *runtimeState) metadataCopy() map[uuid.UUID]JobMetadata {
s.mu.RLock()
defer s.mu.RUnlock()
out := make(map[uuid.UUID]JobMetadata, len(s.metadata))
for id, meta := range s.metadata {
meta.Paused = s.pausedAll || s.pausedJobs[id]
out[id] = meta
}
return out
}
func (s *runtimeState) metadataList() []JobMetadata {
s.mu.RLock()
defer s.mu.RUnlock()
out := make([]JobMetadata, 0, len(s.metadata))
for id, meta := range s.metadata {
meta.Paused = s.pausedAll || s.pausedJobs[id]
out = append(out, meta)
}
sort.Slice(out, func(i, j int) bool {
if out[i].Name == out[j].Name {
return out[i].ID.String() < out[j].ID.String()
}
return out[i].Name < out[j].Name
})
return out
}
func (s *runtimeState) nextAttempt(id uuid.UUID) int {
s.mu.Lock()
defer s.mu.Unlock()
s.attempts[id]++
return s.attempts[id]
}
func (s *runtimeState) addObserver(observer JobObserver) {
if observer == nil {
return
}
s.mu.Lock()
s.observers = append(s.observers, observer)
s.mu.Unlock()
}
func (s *runtimeState) emit(ctx context.Context, event JobEvent) {
s.mu.RLock()
observers := append([]JobObserver(nil), s.observers...)
s.mu.RUnlock()
for _, observer := range observers {
observer.OnJobEvent(ctx, event)
}
}