/
controller.go
76 lines (68 loc) · 1.52 KB
/
controller.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
package controller
import (
"context"
"sync/atomic"
"time"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/progress"
"github.com/opencontainers/go-digest"
)
type Controller struct {
count int64
started *time.Time
writer progress.Writer
Digest digest.Digest
Name string
WriterFactory progress.WriterFactory
}
var _ progress.Controller = &Controller{}
func (c *Controller) Start(ctx context.Context) (context.Context, func(error)) {
if atomic.AddInt64(&c.count, 1) == 1 {
if c.started == nil {
now := time.Now()
c.started = &now
c.writer, _, _ = c.WriterFactory(ctx)
}
if c.Digest != "" {
c.writer.Write(c.Digest.String(), client.Vertex{
Digest: c.Digest,
Name: c.Name,
Started: c.started,
})
}
}
return progress.WithProgress(ctx, c.writer), func(err error) {
if atomic.AddInt64(&c.count, -1) == 0 {
now := time.Now()
var errString string
if err != nil {
errString = err.Error()
}
if c.Digest != "" {
c.writer.Write(c.Digest.String(), client.Vertex{
Digest: c.Digest,
Name: c.Name,
Started: c.started,
Completed: &now,
Error: errString,
})
}
c.writer.Close()
}
}
}
func (c *Controller) Status(id string, action string) func() {
start := time.Now()
c.writer.Write(id, progress.Status{
Action: action,
Started: &start,
})
return func() {
complete := time.Now()
c.writer.Write(id, progress.Status{
Action: action,
Started: &start,
Completed: &complete,
})
}
}