-
Notifications
You must be signed in to change notification settings - Fork 173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added upload progress bar tracker for ISO images. #7320
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
96a8c14
Added upload progress bar tracker for ISO images.
vburenin 676c37e
Format as goimports
vburenin 881c0a5
Make uploadLogger to be a public one
vburenin f66a6f9
Add file headers.
vburenin ec6ad6b
Organize imports
vburenin f31ae1a
Improved comments
vburenin e6b95d5
Better error message
vburenin 9119f94
Uppercases are better
vburenin 471c6b5
Merge branch 'master' into upload-progress
vburenin 50dd727
Merge branch 'master' into upload-progress
vburenin edb794f
Merge branch 'upload-progress' of github.com:vburenin/vic into upload…
vburenin 2af566a
better log message
vburenin 829b0bb
Moved retry config out of the loop.
vburenin 4fa21ea
Merge branch 'master' into upload-progress
vburenin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2018 VMware, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package progresslog | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/vmware/govmomi/vim25/progress" | ||
"github.com/vmware/govmomi/vim25/soap" | ||
) | ||
|
||
// UploadParams uses default upload settings as initial input and set UploadLogger as a logger. | ||
func UploadParams(ul *UploadLogger) *soap.Upload { | ||
params := soap.DefaultUpload | ||
params.Progress = ul | ||
return ¶ms | ||
} | ||
|
||
// UploadLogger is used to track upload progress to ESXi/VC of a specific file. | ||
type UploadLogger struct { | ||
wg sync.WaitGroup | ||
filename string | ||
interval time.Duration | ||
logTo func(format string, args ...interface{}) | ||
} | ||
|
||
// NewUploadLogger returns a new instance of UploadLogger. User can provide a logger interface | ||
// that will be used to dump output of the current upload status. | ||
func NewUploadLogger(logTo func(format string, args ...interface{}), | ||
filename string, progressInterval time.Duration) *UploadLogger { | ||
|
||
return &UploadLogger{ | ||
logTo: logTo, | ||
filename: filename, | ||
interval: progressInterval, | ||
} | ||
} | ||
|
||
// Sink returns a channel that receives current upload progress status. | ||
// Channel has to be closed by the caller. | ||
func (ul *UploadLogger) Sink() chan<- progress.Report { | ||
ul.wg.Add(1) | ||
ch := make(chan progress.Report) | ||
fmtStr := "Uploading %s. Progress: %.2f%%" | ||
|
||
go func() { | ||
var curProgress float32 | ||
var lastProgress float32 | ||
ul.logTo(fmtStr, ul.filename, curProgress) | ||
|
||
mu := sync.Mutex{} | ||
ticker := time.NewTicker(ul.interval) | ||
|
||
// Print progress every ul.interval seconds. | ||
go func() { | ||
for range ticker.C { | ||
mu.Lock() | ||
lastProgress = curProgress | ||
mu.Unlock() | ||
ul.logTo(fmtStr, ul.filename, lastProgress) | ||
} | ||
}() | ||
|
||
for v := range ch { | ||
mu.Lock() | ||
curProgress = v.Percentage() | ||
mu.Unlock() | ||
} | ||
|
||
ticker.Stop() | ||
|
||
if lastProgress != curProgress { | ||
ul.logTo(fmtStr, ul.filename, curProgress) | ||
} | ||
|
||
if curProgress == 100.0 { | ||
ul.logTo("Uploading of %s has been completed", ul.filename) | ||
} | ||
ul.wg.Done() | ||
}() | ||
return ch | ||
} | ||
|
||
// Wait waits for Sink to complete its work, due to its async nature logging messages may be not sequential. | ||
func (ul *UploadLogger) Wait() { | ||
ul.wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2018 VMware, Inc. All Rights Reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package progresslog | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/vmware/govmomi/vim25/progress" | ||
) | ||
|
||
type ProgressResults struct { | ||
percentage float32 | ||
} | ||
|
||
func (pr *ProgressResults) Percentage() float32 { | ||
return pr.percentage | ||
} | ||
|
||
func (pr *ProgressResults) Detail() string { | ||
return "" | ||
} | ||
|
||
func (pr *ProgressResults) Error() error { | ||
return nil | ||
} | ||
|
||
var _ progress.Report = &ProgressResults{} | ||
|
||
func TestNewUploadLoggerComplete(t *testing.T) { | ||
var logs []string | ||
logTo := func(format string, args ...interface{}) { | ||
logs = append(logs, fmt.Sprintf(format, args...)) | ||
} | ||
pl := NewUploadLogger(logTo, "unittest", time.Millisecond*10) | ||
progressChan := pl.Sink() | ||
for i := 0; i <= 10; i++ { | ||
res := &ProgressResults{percentage: float32(i * 10)} | ||
progressChan <- res | ||
time.Sleep(time.Duration(time.Millisecond * 5)) | ||
} | ||
close(progressChan) | ||
pl.Wait() | ||
|
||
if assert.True(t, len(logs) > 3) { | ||
last := len(logs) - 1 | ||
assert.Contains(t, logs[0], "unittest") | ||
assert.Contains(t, logs[0], "0.00%") | ||
assert.Contains(t, logs[1], ".00%") | ||
assert.Contains(t, logs[last-1], "100.00%") | ||
assert.Contains(t, logs[last], "complete") | ||
} | ||
} | ||
|
||
func TestNewUploadLoggerNotComplete(t *testing.T) { | ||
var logs []string | ||
logTo := func(format string, args ...interface{}) { | ||
logs = append(logs, fmt.Sprintf(format, args...)) | ||
} | ||
pl := NewUploadLogger(logTo, "unittest", time.Millisecond*10) | ||
progressChan := pl.Sink() | ||
for i := 0; i < 10; i++ { | ||
res := &ProgressResults{percentage: float32(i * 10)} | ||
progressChan <- res | ||
time.Sleep(time.Duration(time.Millisecond * 5)) | ||
} | ||
close(progressChan) | ||
pl.Wait() | ||
|
||
if assert.True(t, len(logs) > 3) { | ||
last := len(logs) - 1 | ||
assert.Contains(t, logs[0], "unittest") | ||
assert.Contains(t, logs[0], "0.00%") | ||
assert.Contains(t, logs[1], ".00%") | ||
assert.NotContains(t, logs[last], "100.00%") | ||
assert.NotContains(t, logs[last], "complete") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there multiple calls to this function per download?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never mind, I missed the
range ticker.C
:)