-
Notifications
You must be signed in to change notification settings - Fork 947
/
image_push.go
98 lines (85 loc) · 1.81 KB
/
image_push.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package jsonstream
import (
"context"
"sync"
"time"
"github.com/containerd/containerd/remotes/docker"
)
// PushJobs defines a job in upload progress
type PushJobs struct {
jobs map[string]struct{}
ordered []string
tracker docker.StatusTracker
mu sync.Mutex
}
// NewPushJobs news a PushJobs
func NewPushJobs(tracker docker.StatusTracker) *PushJobs {
return &PushJobs{
jobs: make(map[string]struct{}),
tracker: tracker,
}
}
// Add adds a ref in upload job
func (j *PushJobs) Add(ref string) {
j.mu.Lock()
defer j.mu.Unlock()
if _, ok := j.jobs[ref]; ok {
return
}
j.ordered = append(j.ordered, ref)
j.jobs[ref] = struct{}{}
}
// Status gets PushJobs statuses
func (j *PushJobs) Status() []JSONMessage {
j.mu.Lock()
defer j.mu.Unlock()
statuses := make([]JSONMessage, 0, len(j.jobs))
for _, name := range j.ordered {
si := JSONMessage{
ID: name,
}
status, err := j.tracker.GetStatus(name)
if err != nil {
si.Status = "waiting"
} else {
si.Detail = &ProgressDetail{
Current: status.Offset,
Total: status.Total,
}
si.StartedAt = status.StartedAt
si.UpdatedAt = status.UpdatedAt
if status.Offset >= status.Total {
if status.UploadUUID == "" {
si.Status = "done"
} else {
si.Status = "committing"
}
} else {
si.Status = "uploading"
}
}
statuses = append(statuses, si)
}
return statuses
}
// PushProcess translates upload progress to json stream
func PushProcess(ctx context.Context, ongoing *PushJobs, stream *JSONStream) {
var (
ticker = time.NewTicker(100 * time.Millisecond)
done bool
)
defer ticker.Stop()
for {
select {
case <-ticker.C:
for _, si := range ongoing.Status() {
stream.WriteObject(si)
}
if done {
return
}
case <-ctx.Done():
done = true // allow ui to update once more
}
}
}