forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
153 lines (141 loc) · 3.51 KB
/
progress.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
package solver
import (
"context"
"io"
"sort"
"time"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/progress"
digest "github.com/opencontainers/go-digest"
)
func (j *Job) Status(ctx context.Context, ch chan *client.SolveStatus) error {
vs := &vertexStream{cache: map[digest.Digest]*client.Vertex{}, wasCached: make(map[digest.Digest]struct{})}
pr := j.pr.Reader(ctx)
defer func() {
if enc := vs.encore(); len(enc) > 0 {
ch <- &client.SolveStatus{Vertexes: enc}
}
close(ch)
}()
for {
p, err := pr.Read(ctx)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
ss := &client.SolveStatus{}
for _, p := range p {
switch v := p.Sys.(type) {
case client.Vertex:
ss.Vertexes = append(ss.Vertexes, vs.append(v)...)
case progress.Status:
vtx, ok := p.Meta("vertex")
if !ok {
bklog.G(ctx).Warnf("progress %s status without vertex info", p.ID)
continue
}
vs := &client.VertexStatus{
ID: p.ID,
Vertex: vtx.(digest.Digest),
Name: v.Action,
Total: int64(v.Total),
Current: int64(v.Current),
Timestamp: p.Timestamp,
Started: v.Started,
Completed: v.Completed,
}
ss.Statuses = append(ss.Statuses, vs)
case client.VertexLog:
vtx, ok := p.Meta("vertex")
if !ok {
bklog.G(ctx).Warnf("progress %s log without vertex info", p.ID)
continue
}
v.Vertex = vtx.(digest.Digest)
v.Timestamp = p.Timestamp
ss.Logs = append(ss.Logs, &v)
case client.VertexWarning:
vtx, ok := p.Meta("vertex")
if !ok {
bklog.G(ctx).Warnf("progress %s warning without vertex info", p.ID)
continue
}
v.Vertex = vtx.(digest.Digest)
ss.Warnings = append(ss.Warnings, &v)
}
}
sort.Slice(ss.Vertexes, func(i, j int) bool {
if ss.Vertexes[i].Started == nil {
return true
}
if ss.Vertexes[j].Started == nil {
return false
}
return ss.Vertexes[i].Started.Before(*ss.Vertexes[j].Started)
})
sort.Slice(ss.Statuses, func(i, j int) bool {
return ss.Statuses[i].Timestamp.Before(ss.Statuses[j].Timestamp)
})
sort.Slice(ss.Logs, func(i, j int) bool {
return ss.Logs[i].Timestamp.Before(ss.Logs[j].Timestamp)
})
select {
case <-ctx.Done():
return context.Cause(ctx)
case ch <- ss:
}
}
}
type vertexStream struct {
cache map[digest.Digest]*client.Vertex
wasCached map[digest.Digest]struct{}
}
func (vs *vertexStream) append(v client.Vertex) []*client.Vertex {
var out []*client.Vertex
vs.cache[v.Digest] = &v
if v.Started != nil {
for _, inp := range v.Inputs {
if inpv, ok := vs.cache[inp]; ok {
if !inpv.Cached && inpv.Completed == nil {
inpv.Cached = true
inpv.Started = v.Started
inpv.Completed = v.Started
out = append(out, vs.append(*inpv)...)
delete(vs.cache, inp)
}
}
}
}
if v.Cached {
vs.markCached(v.Digest)
}
vcopy := v
return append(out, &vcopy)
}
func (vs *vertexStream) markCached(dgst digest.Digest) {
if v, ok := vs.cache[dgst]; ok {
if _, ok := vs.wasCached[dgst]; !ok {
for _, inp := range v.Inputs {
vs.markCached(inp)
}
}
vs.wasCached[dgst] = struct{}{}
}
}
func (vs *vertexStream) encore() []*client.Vertex {
var out []*client.Vertex
for _, v := range vs.cache {
if v.Started != nil && v.Completed == nil {
now := time.Now()
v.Completed = &now
if _, ok := vs.wasCached[v.Digest]; !ok && v.Error == "" {
v.Error = context.Canceled.Error()
}
out = append(out, v)
}
}
return out
}