-
Notifications
You must be signed in to change notification settings - Fork 1
/
interface.go
375 lines (313 loc) · 7.43 KB
/
interface.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package observability
import (
"context"
"github.com/google/uuid"
"github.com/hephbuild/heph/artifacts"
"github.com/hephbuild/heph/graph"
"io"
"sync"
"sync/atomic"
"time"
)
type Span interface {
ID() uint64
End()
ScheduledTime() time.Time
QueuedTime() time.Time
StartTime() time.Time
EndTime() time.Time
SetScheduledTime(time.Time)
SetQueuedTime(time.Time)
SetStartTime(time.Time)
}
type spanInternal interface {
setId(id uint64)
registerHook(SpanHook)
SetStartTime(time.Time)
}
type SpanError interface {
Span
EndError(err error)
EndErrorState(err error, state State)
FinalState() State
Error() error
}
type SpanTarget interface {
Span
Target() *graph.Target
}
type SpanTargetArtifact interface {
SpanTarget
Artifact() artifacts.Artifact
}
type SpanCacheHit interface {
IsCacheHit() *bool
}
type SpanHook interface {
OnSchedule()
OnQueued()
OnStart()
OnEnd()
}
type SpanHookFunc struct {
OnScheduleFunc func()
OnQueuedFunc func()
OnStartFunc func()
OnEndFunc func()
}
func (s SpanHookFunc) OnSchedule() {
if s.OnScheduleFunc != nil {
s.OnScheduleFunc()
}
}
func (s SpanHookFunc) OnQueued() {
if s.OnQueuedFunc != nil {
s.OnQueuedFunc()
}
}
func (s SpanHookFunc) OnStart() {
if s.OnStartFunc != nil {
s.OnStartFunc()
}
}
func (s SpanHookFunc) OnEnd() {
if s.OnEndFunc != nil {
s.OnEndFunc()
}
}
func FinalizerSpanHook(f func()) SpanHook {
return SpanHookFunc{
OnEndFunc: f,
}
}
func AllSpanHook(f func()) SpanHook {
return SpanHookFunc{
OnScheduleFunc: f,
OnQueuedFunc: f,
OnStartFunc: f,
OnEndFunc: f,
}
}
type State int8
const (
StateUnknown State = iota
StateSuccess
StateFailed
StateSkipped
)
type BaseSpan struct {
id uint64
err error
finalized bool
hooks []SpanHook
scheduledTime time.Time
queuedTime time.Time
startTime time.Time
endTime time.Time
state State
m sync.Mutex
}
var idc uint64
func nextID() uint64 {
return atomic.AddUint64(&idc, 1)
}
func (s *BaseSpan) setId(id uint64) {
s.id = id
}
func (s *BaseSpan) ID() uint64 {
return s.id
}
func (s *BaseSpan) ScheduledTime() time.Time {
return s.scheduledTime
}
func (s *BaseSpan) QueuedTime() time.Time {
return s.queuedTime
}
func (s *BaseSpan) StartTime() time.Time {
return s.startTime
}
func (s *BaseSpan) EndTime() time.Time {
return s.endTime
}
func (s *BaseSpan) SetScheduledTime(t time.Time) {
s.scheduledTime = t
s.runHooks(SpanHook.OnSchedule)
}
func (s *BaseSpan) SetQueuedTime(t time.Time) {
s.queuedTime = t
s.runHooks(SpanHook.OnQueued)
}
func (s *BaseSpan) SetStartTime(t time.Time) {
s.startTime = t
s.runHooks(SpanHook.OnStart)
}
func (s *BaseSpan) Error() error {
return s.err
}
func (s *BaseSpan) EndError(err error) {
if err == nil {
s.End()
} else {
s.EndErrorState(err, StateFailed)
}
}
func (s *BaseSpan) EndErrorState(err error, state State) {
s.m.Lock()
defer s.m.Unlock()
if s.finalized {
return
}
s.finalized = true
s.err = err
s.state = state
s.endTime = time.Now()
s.runHooks(SpanHook.OnEnd)
}
func (s *BaseSpan) FinalState() State {
return s.state
}
func (s *BaseSpan) End() {
s.EndErrorState(nil, StateSuccess)
}
func (s *BaseSpan) runHooks(h func(SpanHook)) {
for _, hook := range s.hooks {
h(hook)
}
}
func (s *BaseSpan) registerHook(hook SpanHook) {
if hook != nil {
s.hooks = append(s.hooks, hook)
}
}
type TargetSpan struct {
BaseSpan
target *graph.Target
}
func (s *TargetSpan) spanDedupId() interface{} {
return s.target.Addr
}
func (s *TargetSpan) Target() *graph.Target {
return s.target
}
type TargetExecSpan struct {
TargetSpan
ExecId uuid.UUID
}
func (s *TargetExecSpan) spanDedupId() interface{} {
return struct {
base interface{}
execId uuid.UUID
}{
base: s.TargetSpan.spanDedupId(),
execId: s.ExecId,
}
}
type TargetArtifactSpan struct {
TargetSpan
artifact artifacts.Artifact
}
func (s *TargetArtifactSpan) spanDedupId() interface{} {
return struct {
base interface{}
artifact string
}{
base: s.TargetSpan.spanDedupId(),
artifact: s.artifact.Name(),
}
}
func (s *TargetArtifactSpan) Artifact() artifacts.Artifact {
return s.artifact
}
type TargetArtifactCacheSpan struct {
TargetArtifactSpan
Cache string
cacheHit *bool
}
func (s *TargetArtifactCacheSpan) spanDedupId() interface{} {
return struct {
base interface{}
cache string
}{
base: s.TargetArtifactSpan.spanDedupId(),
cache: s.Cache,
}
}
func (s *TargetArtifactCacheSpan) SetCacheHit(v bool) {
s.cacheHit = &v
}
func (s *TargetArtifactCacheSpan) IsCacheHit() *bool {
return s.cacheHit
}
type ExternalCacheGetSpan struct {
TargetSpan
Cache string
Outputs []string
OnlyMeta bool
cacheHit *bool
}
func (s *ExternalCacheGetSpan) SetCacheHit(v bool) {
s.cacheHit = &v
}
func (s *ExternalCacheGetSpan) IsCacheHit() *bool {
return s.cacheHit
}
type Hook interface {
OnRoot(ctx context.Context, span *BaseSpan) (context.Context, SpanHook)
OnRun(ctx context.Context, span *TargetSpan) (context.Context, SpanHook)
OnGenPass(ctx context.Context, span *BaseSpan) (context.Context, SpanHook)
OnCacheDownload(ctx context.Context, span *TargetArtifactCacheSpan) (context.Context, SpanHook)
OnCacheUpload(ctx context.Context, span *TargetArtifactCacheSpan) (context.Context, SpanHook)
OnRunPrepare(ctx context.Context, span *TargetSpan) (context.Context, SpanHook)
OnRunExec(ctx context.Context, span *TargetExecSpan) (context.Context, SpanHook)
OnCollectOutput(ctx context.Context, span *TargetSpan) (context.Context, SpanHook)
OnLocalCacheStore(ctx context.Context, span *TargetSpan) (context.Context, SpanHook)
OnLocalCacheCheck(ctx context.Context, span *TargetArtifactCacheSpan) (context.Context, SpanHook)
OnExternalCacheGet(ctx context.Context, span *ExternalCacheGetSpan) (context.Context, SpanHook)
OnLogs(ctx context.Context) io.Writer
}
type BaseHook struct{}
var _ Hook = (*BaseHook)(nil)
func (b BaseHook) OnRoot(ctx context.Context, _ *BaseSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnRun(ctx context.Context, _ *TargetSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnGenPass(ctx context.Context, _ *BaseSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnCacheDownload(ctx context.Context, _ *TargetArtifactCacheSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnCacheUpload(ctx context.Context, _ *TargetArtifactCacheSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnRunPrepare(ctx context.Context, _ *TargetSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnRunExec(ctx context.Context, _ *TargetExecSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnCollectOutput(ctx context.Context, _ *TargetSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnLocalCacheStore(ctx context.Context, _ *TargetSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnLocalCacheCheck(ctx context.Context, _ *TargetArtifactCacheSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnExternalCacheGet(ctx context.Context, _ *ExternalCacheGetSpan) (context.Context, SpanHook) {
return ctx, nil
}
func (b BaseHook) OnLogs(ctx context.Context) io.Writer {
return nil
}
func DoVE[T any](span SpanError, f func() (T, error)) (_ T, rerr error) {
defer span.EndError(rerr)
return f()
}
func DoE(span SpanError, f func() error) (rerr error) {
defer span.EndError(rerr)
return f()
}