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
|
package controller
import (
"context"
"sync"
"time"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/solver/pb"
"github.com/moby/buildkit/util/progress"
digest "github.com/opencontainers/go-digest"
)
type Controller struct {
count int64
started *time.Time
writer progress.Writer
mu sync.Mutex
id string
Digest digest.Digest
Name string
WriterFactory progress.WriterFactory
ProgressGroup *pb.ProgressGroup
}
var _ progress.Controller = &Controller{}
func (c *Controller) Start(ctx context.Context) (context.Context, func(error)) {
c.mu.Lock()
defer c.mu.Unlock()
c.count++
if c.count == 1 {
if c.started == nil {
now := time.Now()
c.started = &now
c.writer, _, _ = c.WriterFactory(ctx)
c.id = identity.NewID()
}
if c.Digest != "" {
c.writer.Write(c.id, client.Vertex{
Digest: c.Digest,
Name: c.Name,
Started: c.started,
ProgressGroup: c.ProgressGroup,
})
}
}
return progress.WithProgress(ctx, c.writer), func(err error) {
c.mu.Lock()
defer c.mu.Unlock()
c.count--
if c.count == 0 {
now := time.Now()
var errString string
if err != nil {
errString = err.Error()
}
if c.Digest != "" {
c.writer.Write(c.id, client.Vertex{
Digest: c.Digest,
Name: c.Name,
Started: c.started,
Completed: &now,
Error: errString,
ProgressGroup: c.ProgressGroup,
})
}
c.writer.Close()
c.started = nil
c.id = ""
}
}
}
func (c *Controller) Status(id string, action string) func() {
start := time.Now()
if c.writer != nil {
c.writer.Write(id, progress.Status{
Action: action,
Started: &start,
})
}
return func() {
complete := time.Now()
if c.writer != nil {
c.writer.Write(id, progress.Status{
Action: action,
Started: &start,
Completed: &complete,
})
}
}
}
|