util: simplify progress syncronization

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
This commit is contained in:
Tonis Tiigi
2020-09-20 19:46:39 -07:00
parent 290e25917c
commit 1496ac9b55
10 changed files with 110 additions and 196 deletions

View File

@ -9,13 +9,10 @@ import (
)
type Writer interface {
Done() <-chan struct{}
Err() error
Status() chan *client.SolveStatus
Write(*client.SolveStatus)
}
func Write(w Writer, name string, f func() error) {
status := w.Status()
dgst := digest.FromBytes([]byte(identity.NewID()))
tm := time.Now()
@ -25,9 +22,9 @@ func Write(w Writer, name string, f func() error) {
Started: &tm,
}
status <- &client.SolveStatus{
w.Write(&client.SolveStatus{
Vertexes: []*client.Vertex{&vtx},
}
})
err := f()
@ -37,7 +34,21 @@ func Write(w Writer, name string, f func() error) {
if err != nil {
vtx2.Error = err.Error()
}
status <- &client.SolveStatus{
w.Write(&client.SolveStatus{
Vertexes: []*client.Vertex{&vtx2},
}
})
}
func NewChannel(w Writer) chan *client.SolveStatus {
ch := make(chan *client.SolveStatus)
go func() {
for {
v, ok := <-ch
if !ok {
return
}
w.Write(v)
}
}()
return ch
}