2017-05-30 22:01:02 +00:00
|
|
|
package progress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-06-16 22:49:03 +00:00
|
|
|
"io"
|
|
|
|
"sort"
|
2017-05-31 00:45:58 +00:00
|
|
|
"sync"
|
2017-05-30 22:01:02 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
// Progress package provides utility functions for using the context to capture
|
|
|
|
// progress of a running function. All progress items written contain an ID
|
|
|
|
// that is used to collapse unread messages.
|
|
|
|
|
2017-05-31 00:45:58 +00:00
|
|
|
type contextKeyT string
|
|
|
|
|
|
|
|
var contextKey = contextKeyT("buildkit/util/progress")
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
// FromContext returns a progress writer from a context.
|
|
|
|
func FromContext(ctx context.Context, opts ...WriterOption) (Writer, bool, context.Context) {
|
2017-05-31 00:45:58 +00:00
|
|
|
pw, ok := ctx.Value(contextKey).(*progressWriter)
|
|
|
|
if !ok {
|
|
|
|
return &noOpWriter{}, false, ctx
|
|
|
|
}
|
2017-06-17 05:12:27 +00:00
|
|
|
pw = newWriter(pw)
|
|
|
|
for _, o := range opts {
|
|
|
|
o(pw)
|
|
|
|
}
|
2017-05-31 00:45:58 +00:00
|
|
|
ctx = context.WithValue(ctx, contextKey, pw)
|
2017-06-13 21:42:51 +00:00
|
|
|
return pw, true, ctx
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
type WriterOption func(Writer)
|
|
|
|
|
|
|
|
// NewContext returns a new context and a progress reader that captures all
|
|
|
|
// progress items writtern to this context. Last returned parameter is a closer
|
|
|
|
// function to signal that no new writes will happen to this context.
|
|
|
|
func NewContext(ctx context.Context) (Reader, context.Context, func()) {
|
2017-05-31 00:45:58 +00:00
|
|
|
pr, pw, cancel := pipe()
|
|
|
|
ctx = context.WithValue(ctx, contextKey, pw)
|
|
|
|
return pr, ctx, cancel
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func WithMetadata(key string, val interface{}) WriterOption {
|
|
|
|
return func(w Writer) {
|
|
|
|
if pw, ok := w.(*progressWriter); ok {
|
|
|
|
pw.meta[key] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Writer interface {
|
|
|
|
Write(id string, value interface{}) error
|
|
|
|
Close() error
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
type Reader interface {
|
2017-06-16 22:49:03 +00:00
|
|
|
Read(context.Context) ([]*Progress, error)
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Progress struct {
|
2017-06-13 21:42:51 +00:00
|
|
|
ID string
|
2017-05-30 22:01:02 +00:00
|
|
|
Timestamp time.Time
|
2017-06-13 21:42:51 +00:00
|
|
|
Sys interface{}
|
2017-06-17 05:12:27 +00:00
|
|
|
meta map[string]interface{}
|
2017-06-13 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Status struct {
|
2017-06-15 23:08:20 +00:00
|
|
|
Action string
|
|
|
|
Current int
|
|
|
|
Total int
|
|
|
|
Started *time.Time
|
|
|
|
Completed *time.Time
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 00:45:58 +00:00
|
|
|
type progressReader struct {
|
|
|
|
ctx context.Context
|
|
|
|
cond *sync.Cond
|
|
|
|
mu sync.Mutex
|
2017-06-16 22:49:03 +00:00
|
|
|
writers map[*progressWriter]struct{}
|
|
|
|
dirty map[string]*Progress
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-16 22:49:03 +00:00
|
|
|
func (pr *progressReader) Read(ctx context.Context) ([]*Progress, error) {
|
2017-05-31 00:45:58 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
case <-ctx.Done():
|
|
|
|
pr.cond.Broadcast()
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
pr.mu.Lock()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
pr.mu.Unlock()
|
|
|
|
return nil, ctx.Err()
|
|
|
|
default:
|
|
|
|
}
|
2017-06-16 22:49:03 +00:00
|
|
|
dmap := pr.dirty
|
|
|
|
if len(dmap) == 0 {
|
2017-06-17 05:12:27 +00:00
|
|
|
select {
|
|
|
|
case <-pr.ctx.Done():
|
|
|
|
if len(pr.writers) == 0 {
|
|
|
|
pr.mu.Unlock()
|
|
|
|
return nil, io.EOF
|
|
|
|
}
|
|
|
|
default:
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
|
|
|
pr.cond.Wait()
|
2017-06-16 22:49:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
pr.dirty = make(map[string]*Progress)
|
|
|
|
pr.mu.Unlock()
|
|
|
|
|
|
|
|
out := make([]*Progress, 0, len(dmap))
|
|
|
|
for _, p := range dmap {
|
|
|
|
out = append(out, p)
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
2017-06-16 22:49:03 +00:00
|
|
|
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].Timestamp.Before(out[j].Timestamp)
|
|
|
|
})
|
|
|
|
|
|
|
|
return out, nil
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 00:45:58 +00:00
|
|
|
func (pr *progressReader) append(pw *progressWriter) {
|
|
|
|
pr.mu.Lock()
|
|
|
|
defer pr.mu.Unlock()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-pr.ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
2017-06-16 22:49:03 +00:00
|
|
|
pr.writers[pw] = struct{}{}
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func pipe() (*progressReader, *progressWriter, func()) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
pr := &progressReader{
|
2017-06-16 22:49:03 +00:00
|
|
|
ctx: ctx,
|
|
|
|
writers: make(map[*progressWriter]struct{}),
|
|
|
|
dirty: make(map[string]*Progress),
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
|
|
|
pr.cond = sync.NewCond(&pr.mu)
|
2017-05-31 17:01:28 +00:00
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
pr.cond.Broadcast()
|
|
|
|
}()
|
2017-05-31 00:45:58 +00:00
|
|
|
pw := &progressWriter{
|
|
|
|
reader: pr,
|
|
|
|
}
|
|
|
|
return pr, pw, cancel
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func newWriter(pw *progressWriter) *progressWriter {
|
|
|
|
meta := make(map[string]interface{})
|
|
|
|
for k, v := range pw.meta {
|
|
|
|
meta[k] = v
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
|
|
|
pw = &progressWriter{
|
|
|
|
reader: pw.reader,
|
2017-06-17 05:12:27 +00:00
|
|
|
meta: meta,
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
|
|
|
pw.reader.append(pw)
|
|
|
|
return pw
|
|
|
|
}
|
|
|
|
|
|
|
|
type progressWriter struct {
|
|
|
|
done bool
|
|
|
|
reader *progressReader
|
2017-06-17 05:12:27 +00:00
|
|
|
meta map[string]interface{}
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
2017-05-30 22:01:02 +00:00
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func (pw *progressWriter) Write(id string, v interface{}) error {
|
2017-05-31 00:45:58 +00:00
|
|
|
if pw.done {
|
2017-06-17 05:12:27 +00:00
|
|
|
return errors.Errorf("writing %s to closed progress writer", id)
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
2017-06-28 20:59:07 +00:00
|
|
|
return pw.writeRawProgress(&Progress{
|
2017-06-17 05:12:27 +00:00
|
|
|
ID: id,
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Sys: v,
|
|
|
|
meta: pw.meta,
|
|
|
|
})
|
2017-06-13 21:42:51 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func (pw *progressWriter) WriteRawProgress(p *Progress) error {
|
2017-06-28 20:59:07 +00:00
|
|
|
p.meta = pw.meta
|
|
|
|
return pw.writeRawProgress(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pw *progressWriter) writeRawProgress(p *Progress) error {
|
2017-06-16 22:49:03 +00:00
|
|
|
pw.reader.mu.Lock()
|
2017-06-17 05:12:27 +00:00
|
|
|
pw.reader.dirty[p.ID] = p
|
2017-06-16 22:49:03 +00:00
|
|
|
pw.reader.mu.Unlock()
|
2017-05-31 00:45:58 +00:00
|
|
|
pw.reader.cond.Broadcast()
|
|
|
|
return nil
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func (pw *progressWriter) Close() error {
|
2017-06-16 22:49:03 +00:00
|
|
|
pw.reader.mu.Lock()
|
|
|
|
delete(pw.reader.writers, pw)
|
|
|
|
pw.reader.mu.Unlock()
|
|
|
|
pw.reader.cond.Broadcast()
|
2017-06-13 21:42:51 +00:00
|
|
|
pw.done = true
|
2017-06-16 22:49:03 +00:00
|
|
|
return nil
|
2017-05-31 00:45:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func (p *Progress) Meta(key string) (interface{}, bool) {
|
|
|
|
v, ok := p.meta[key]
|
|
|
|
return v, ok
|
|
|
|
}
|
|
|
|
|
2017-05-31 00:45:58 +00:00
|
|
|
type noOpWriter struct{}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func (pw *noOpWriter) Write(_ string, _ interface{}) error {
|
2017-05-31 00:45:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-17 05:12:27 +00:00
|
|
|
func (pw *noOpWriter) Close() error {
|
2017-05-31 00:45:58 +00:00
|
|
|
return nil
|
2017-05-30 22:01:02 +00:00
|
|
|
}
|