progress.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package plugin
  2. import (
  3. "sync"
  4. "time"
  5. "github.com/containerd/containerd/remotes/docker"
  6. )
  7. func newPushJobs(tracker docker.StatusTracker) *pushJobs {
  8. return &pushJobs{
  9. names: make(map[string]string),
  10. t: tracker,
  11. }
  12. }
  13. type pushJobs struct {
  14. t docker.StatusTracker
  15. mu sync.Mutex
  16. jobs []string
  17. // maps job ref to a name
  18. names map[string]string
  19. }
  20. func (p *pushJobs) add(id, name string) {
  21. p.mu.Lock()
  22. defer p.mu.Unlock()
  23. if _, ok := p.names[id]; ok {
  24. return
  25. }
  26. p.jobs = append(p.jobs, id)
  27. p.names[id] = name
  28. }
  29. func (p *pushJobs) status() []contentStatus {
  30. statuses := make([]contentStatus, 0, len(p.jobs))
  31. p.mu.Lock()
  32. defer p.mu.Unlock()
  33. for _, j := range p.jobs {
  34. var s contentStatus
  35. s.Ref = p.names[j]
  36. status, err := p.t.GetStatus(j)
  37. if err != nil {
  38. s.Status = "Waiting"
  39. } else {
  40. s.Total = status.Total
  41. s.Offset = status.Offset
  42. s.StartedAt = status.StartedAt
  43. s.UpdatedAt = status.UpdatedAt
  44. if status.UploadUUID == "" {
  45. s.Status = "Upload complete"
  46. } else {
  47. s.Status = "Uploading"
  48. }
  49. }
  50. statuses = append(statuses, s)
  51. }
  52. return statuses
  53. }
  54. type contentStatus struct {
  55. Status string
  56. Total int64
  57. Offset int64
  58. StartedAt time.Time
  59. UpdatedAt time.Time
  60. Ref string
  61. }