forked from earthly/earthly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver_monitor.go
262 lines (245 loc) · 6.37 KB
/
solver_monitor.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
package builder
import (
"context"
"regexp"
"strings"
"sync"
"time"
"github.com/armon/circbuf"
"github.com/cheggaaa/pb/v3"
"github.com/earthly/earthly/conslogging"
"github.com/earthly/earthly/logging"
"github.com/moby/buildkit/client"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
const (
durationBetweenProgressUpdate = time.Second * 5
tailErrorBufferSizeBytes = 80 * 1024 // About as much as 1024 lines of 80 chars each.
)
type vertexMonitor struct {
vertex *client.Vertex
targetStr string
salt string
operation string
lastOutput time.Time
lastPercentage int
logger logging.Logger
console conslogging.ConsoleLogger
headerPrinted bool
isInternal bool
isError bool
tailOutput *circbuf.Buffer
}
func (vm *vertexMonitor) printHeader() {
vm.headerPrinted = true
if vm.operation == "" {
return
}
out := []string{"-->"}
out = append(out, vm.operation)
c := vm.console
if vm.vertex.Cached {
c = c.WithCached(true)
}
c.Printf("%s\n", strings.Join(out, " "))
}
func (vm *vertexMonitor) shouldPrintProgress(percent int) bool {
if !vm.headerPrinted {
return false
}
if vm.targetStr == "" {
return false
}
now := time.Now()
if now.Sub(vm.lastOutput) < durationBetweenProgressUpdate && percent < 100 {
return false
}
if vm.lastPercentage >= percent {
return false
}
vm.lastOutput = now
vm.lastPercentage = percent
return true
}
func (vm *vertexMonitor) printOutput(output []byte) error {
vm.console.PrintBytes(output)
if vm.tailOutput == nil {
var err error
vm.tailOutput, err = circbuf.NewBuffer(tailErrorBufferSizeBytes)
if err != nil {
return errors.Wrap(err, "allocate buffer for output")
}
}
_, err := vm.tailOutput.Write(output)
if err != nil {
return errors.Wrap(err, "write to in-memory output buffer")
}
return nil
}
func (vm *vertexMonitor) printError() {
if strings.Contains(vm.vertex.Error, "executor failed running") {
vm.console.Warnf("ERROR: Command exited with non-zero code: %s\n", vm.operation)
} else {
vm.console.Warnf("ERROR: (%s) %s\n", vm.operation, vm.vertex.Error)
}
}
type solverMonitor struct {
console conslogging.ConsoleLogger
vertices map[digest.Digest]*vertexMonitor
}
func newSolverMonitor(console conslogging.ConsoleLogger) *solverMonitor {
return &solverMonitor{
console: console,
vertices: make(map[digest.Digest]*vertexMonitor),
}
}
func (sm *solverMonitor) monitorProgress(ctx context.Context, ch chan *client.SolveStatus) error {
var errVertex *vertexMonitor
Loop:
for {
select {
case ss, ok := <-ch:
if !ok {
break Loop
}
for _, vertex := range ss.Vertexes {
vm, ok := sm.vertices[vertex.Digest]
if !ok {
targetStr, salt, operation := parseVertexName(vertex.Name)
vertexLogger := logging.GetLogger(ctx).
With("target", targetStr).
With("vertex", shortDigest(vertex.Digest)).
With("cached", vertex.Cached).
With("operation", operation)
vm = &vertexMonitor{
vertex: vertex,
targetStr: targetStr,
salt: salt,
operation: operation,
logger: vertexLogger,
isInternal: (targetStr == "internal"),
console: sm.console.WithPrefixAndSalt(targetStr, salt),
}
sm.vertices[vertex.Digest] = vm
}
vm.vertex = vertex
if !vm.headerPrinted &&
((!vm.isInternal && (vertex.Cached || vertex.Started != nil)) || vertex.Error != "") {
vm.printHeader()
vm.logger.Info("Vertex started or cached")
}
if vertex.Error != "" {
if strings.Contains(vertex.Error, "context canceled") {
if !vm.isInternal {
vm.console.Printf("WARN: Canceled\n")
}
} else {
vm.isError = true
if errVertex == nil {
errVertex = vm
}
vm.printError()
}
vm.logger.Error(errors.New(vertex.Error))
}
}
var on sync.Once
progressBar := pb.New(100)
for _, vs := range ss.Statuses {
vm, ok := sm.vertices[vs.Vertex]
if !ok || vm.isInternal {
// No logging for internal operations.
continue
}
progress := int(0)
if vs.Total != 0 {
progress = int(100.0 * float32(vs.Current) / float32(vs.Total))
}
if vs.Completed != nil {
progress = 100
}
if vm.shouldPrintProgress(progress) {
logger := vm.logger.
With("progress", progress).
With("name", vs.Name)
if !vm.headerPrinted {
vm.printHeader()
}
logger.Info(vs.ID)
on.Do(func() {
vm.console.Printf("%s\n", vs.ID)
progressBar.Start()
})
progressBar.SetCurrent(int64(progress))
}
}
progressBar.Finish()
for _, logLine := range ss.Logs {
vm, ok := sm.vertices[logLine.Vertex]
if !ok || vm.isInternal {
// No logging for internal operations.
continue
}
if !vm.headerPrinted {
vm.printHeader()
}
vm.logger.Info(string(logLine.Data))
err := vm.printOutput(logLine.Data)
if err != nil {
return err
}
}
}
}
if errVertex != nil {
sm.reprintFailure(errVertex)
}
return nil
}
func (sm *solverMonitor) reprintFailure(errVertex *vertexMonitor) {
sm.console.Warnf("Repeating the output of the command that caused the failure\n")
sm.console.PrintFailure()
errVertex.console = errVertex.console.WithFailed(true)
errVertex.printHeader()
if errVertex.tailOutput != nil {
isTruncated := (errVertex.tailOutput.TotalWritten() > errVertex.tailOutput.Size())
if errVertex.tailOutput.TotalWritten() == 0 {
errVertex.console.Printf("[no output]\n")
} else {
if isTruncated {
errVertex.console.Printf("[...]\n")
}
errVertex.console.PrintBytes(errVertex.tailOutput.Bytes())
}
} else {
errVertex.console.Printf("[no output]\n")
}
errVertex.printError()
}
var bracketsRegexp = regexp.MustCompile("^\\[([^\\]]*)\\] (.*)$")
func parseVertexName(vertexName string) (string, string, string) {
target := ""
operation := ""
salt := ""
match := bracketsRegexp.FindStringSubmatch(vertexName)
if len(match) < 2 {
return target, salt, operation
}
targetAndSalt := match[1]
targetAndSaltSlice := strings.SplitN(targetAndSalt, " ", 2)
if len(targetAndSaltSlice) == 2 {
target = targetAndSaltSlice[0]
salt = targetAndSaltSlice[1]
} else {
target = targetAndSalt
}
if len(match) < 3 {
return target, salt, operation
}
operation = match[2]
return target, salt, operation
}
func shortDigest(d digest.Digest) string {
return d.Hex()[:12]
}