2023-07-03 20:23:48 +08:00
|
|
|
package dockerutil
|
2019-04-17 08:11:43 +08:00
|
|
|
|
|
|
|
import (
|
2023-08-08 14:38:52 +08:00
|
|
|
"encoding/json"
|
2019-04-17 08:11:43 +08:00
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
2023-07-03 20:23:48 +08:00
|
|
|
"github.com/docker/buildx/util/progress"
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
2019-04-17 08:11:43 +08:00
|
|
|
"github.com/moby/buildkit/client"
|
|
|
|
)
|
|
|
|
|
2023-08-08 14:38:52 +08:00
|
|
|
const minTimeDelta = 2 * time.Second
|
2019-04-17 08:11:43 +08:00
|
|
|
|
2023-08-08 14:38:52 +08:00
|
|
|
func fromReader(l progress.SubLogger, rc io.ReadCloser) error {
|
|
|
|
started := map[string]client.VertexStatus{}
|
2019-04-17 08:11:43 +08:00
|
|
|
|
2023-08-08 14:38:52 +08:00
|
|
|
defer func() {
|
|
|
|
for _, st := range started {
|
|
|
|
st := st
|
|
|
|
if st.Completed == nil {
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
|
|
|
l.SetStatus(&st)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2019-04-17 08:11:43 +08:00
|
|
|
|
2023-08-08 14:38:52 +08:00
|
|
|
dec := json.NewDecoder(rc)
|
|
|
|
var parsedErr error
|
|
|
|
var jm jsonmessage.JSONMessage
|
|
|
|
for {
|
|
|
|
if err := dec.Decode(&jm); err != nil {
|
|
|
|
if parsedErr != nil {
|
|
|
|
return parsedErr
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if jm.Error != nil {
|
|
|
|
parsedErr = jm.Error
|
|
|
|
}
|
|
|
|
if jm.ID == "" || jm.Progress == nil {
|
|
|
|
continue
|
2023-07-03 20:23:48 +08:00
|
|
|
}
|
2019-04-17 08:11:43 +08:00
|
|
|
|
2023-08-08 14:38:52 +08:00
|
|
|
id := "loading layer " + jm.ID
|
|
|
|
st, ok := started[id]
|
|
|
|
if !ok {
|
|
|
|
now := time.Now()
|
|
|
|
st = client.VertexStatus{
|
|
|
|
ID: id,
|
|
|
|
Started: &now,
|
|
|
|
}
|
|
|
|
}
|
2024-11-20 09:39:22 +08:00
|
|
|
timeDelta := time.Since(st.Timestamp)
|
2023-08-08 14:38:52 +08:00
|
|
|
if timeDelta < minTimeDelta {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
st.Timestamp = time.Now()
|
|
|
|
if jm.Status == "Loading layer" {
|
|
|
|
st.Current = jm.Progress.Current
|
|
|
|
st.Total = jm.Progress.Total
|
|
|
|
}
|
|
|
|
if jm.Error != nil {
|
|
|
|
now := time.Now()
|
|
|
|
st.Completed = &now
|
|
|
|
}
|
|
|
|
started[id] = st
|
|
|
|
l.SetStatus(&st)
|
2019-04-17 08:11:43 +08:00
|
|
|
}
|
2023-08-08 14:38:52 +08:00
|
|
|
|
|
|
|
return nil
|
2019-04-17 08:11:43 +08:00
|
|
|
}
|