Skip to content

Commit

Permalink
client/driver: use go-utils package for printing bytes size
Browse files Browse the repository at this point in the history
  • Loading branch information
nickethier committed Apr 26, 2018
1 parent ddc1c87 commit c946d6d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
8 changes: 4 additions & 4 deletions client/driver/docker_coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var (
)

const (
// dockerPullInactivityDeadline is the length of time we wait for docker to
// progress in pulling an image, after which the inactivtiy handler is run.
dockerPullInactivityDeadline = 2 * time.Minute
// dockerPullProgressEmitInterval is the interval at which the pull progress
// is emited to the allocation
dockerPullProgressEmitInterval = 2 * time.Minute
)

// pullFuture is a sharable future for retrieving a pulled images ID and any
Expand Down Expand Up @@ -403,7 +403,7 @@ func (d *dockerCoordinator) handlePullInactivity(image, msg string, timestamp, p
func (d *dockerCoordinator) handlePullProgressReport(image, msg string, timestamp, pullStart time.Time) {
d.logger.Printf("[DEBUG] driver.docker: image %s pull progress: %s", image, msg)

if timestamp.Sub(pullStart) > dockerPullInactivityDeadline {
if timestamp.Sub(pullStart) > dockerPullProgressEmitInterval {
d.emitEvent(image, "Docker image %s pull progress: %s", image, msg)
}
}
Expand Down
18 changes: 12 additions & 6 deletions client/driver/docker_progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/docker/docker/pkg/jsonmessage"
units "github.com/docker/go-units"
)

const (
Expand Down Expand Up @@ -78,6 +79,7 @@ type imageProgress struct {
pullStart time.Time
}

// get returns a status message and the timestamp of the last status update
func (p *imageProgress) get() (string, time.Time) {
p.RLock()
defer p.RUnlock()
Expand All @@ -103,11 +105,13 @@ func (p *imageProgress) get() (string, time.Time) {
est = (elapsed.Nanoseconds() / cur * total) - elapsed.Nanoseconds()
}

return fmt.Sprintf("Pulled %d/%d (%d/%dMB) pulling %d layers - est %.1fs remaining",
pulled, len(p.layers), cur/1000/1000, total/1000/1000, pulling,
return fmt.Sprintf("Pulled %d/%d (%s/%s) pulling %d layers - est %.1fs remaining",
pulled, len(p.layers), units.BytesSize(float64(cur)), units.BytesSize(float64(total)), pulling,
time.Duration(est).Seconds()), p.timestamp
}

// set takes a status message recieved from the docker engine api during an image
// pull and updates the status of the coorisponding layer
func (p *imageProgress) set(msg *jsonmessage.JSONMessage) {
p.Lock()
defer p.Unlock()
Expand All @@ -134,19 +138,21 @@ func (p *imageProgress) set(msg *jsonmessage.JSONMessage) {
}
}

// currentBytes iterates through all image layers and sums the total of
// current bytes. The caller is responsible for aquiring a read lock on the
// imageProgress struct
func (p *imageProgress) currentBytes() int64 {
p.RLock()
defer p.RUnlock()
var b int64
for _, l := range p.layers {
b += l.currentBytes
}
return b
}

// totalBytes iterates through all image layers and sums the total of
// total bytes. The caller is responsible for aquiring a read lock on the
// imageProgress struct
func (p *imageProgress) totalBytes() int64 {
p.RLock()
defer p.RUnlock()
var b int64
for _, l := range p.layers {
b += l.totalBytes
Expand Down

0 comments on commit c946d6d

Please sign in to comment.