-
Notifications
You must be signed in to change notification settings - Fork 303
/
pipeline_state.go
111 lines (91 loc) · 2.83 KB
/
pipeline_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
package build
import (
"context"
"fmt"
"strings"
"time"
"github.com/windmilleng/tilt/pkg/logger"
)
type PipelineState struct {
curPipelineStep int
curBuildStep int
totalPipelineStepCount int
pipelineStepDurations []time.Duration
curPipelineStart time.Time
curPipelineStepStart time.Time
c Clock
}
type Clock interface {
Now() time.Time
}
type realClock struct{}
func (realClock) Now() time.Time { return time.Now() }
func ProvideClock() Clock {
return realClock{}
}
const buildStepOutputPrefix = " ╎ "
func NewPipelineState(ctx context.Context, totalStepCount int, c Clock) *PipelineState {
return &PipelineState{
curPipelineStep: 1,
totalPipelineStepCount: totalStepCount,
curPipelineStart: c.Now(),
c: c,
}
}
// NOTE(maia): this func should always be deferred in a closure, so that the `err` arg
// is bound at the time of calling rather than at the time of deferring. I.e., do:
// defer func() { ps.End(ctx, err) }()
// and NOT:
// defer ps.End(ctx, err)
func (ps *PipelineState) End(ctx context.Context, err error) {
ps.curPipelineStep = 0
ps.curBuildStep = 0
if err != nil {
return
}
l := logger.Get(ctx)
prefix := logger.Blue(l).Sprint(" │ ")
elapsed := ps.c.Now().Sub(ps.curPipelineStart)
for i, duration := range ps.pipelineStepDurations {
l.Infof("%sStep %d - %.3fs", prefix, i+1, duration.Seconds())
}
l.Infof("%sDone in: %.3fs \n", prefix, elapsed.Seconds())
}
func (ps *PipelineState) StartPipelineStep(ctx context.Context, format string, a ...interface{}) {
l := logger.Get(ctx)
line := logger.Blue(l).Sprintf("STEP %d/%d — ", ps.curPipelineStep, ps.totalPipelineStepCount)
l.Infof("%s%s", line, fmt.Sprintf(format, a...))
ps.curPipelineStep++
ps.curBuildStep = 1
ps.curPipelineStepStart = ps.c.Now()
}
func (ps *PipelineState) EndPipelineStep(ctx context.Context) {
elapsed := ps.c.Now().Sub(ps.curPipelineStepStart)
logger.Get(ctx).Infof("")
ps.pipelineStepDurations = append(ps.pipelineStepDurations, elapsed)
}
func (ps *PipelineState) StartBuildStep(ctx context.Context, format string, a ...interface{}) {
l := logger.Get(ctx)
p := logger.Blue(l).Sprint(" │ ")
l.Infof("%s%s", p, fmt.Sprintf(format, a...))
ps.curBuildStep++
}
func (ps *PipelineState) Printf(ctx context.Context, format string, a ...interface{}) {
l := logger.Get(ctx)
if ps.curBuildStep == 0 {
l.Infof(format, a...)
} else {
message := fmt.Sprintf(format, a...)
message = strings.Replace(message, "\n", "\n"+buildStepOutputPrefix, -1)
l.Infof("%s%s", buildStepOutputPrefix, message)
}
}
func (ps *PipelineState) AttachLogger(ctx context.Context) context.Context {
l := logger.Get(ctx)
if ps.curBuildStep == 0 {
return ctx
} else {
return logger.WithLogger(ctx,
logger.NewPrefixedLogger(buildStepOutputPrefix, l))
}
}