-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #323 from cloudskiff/fea/progress
Show progress while reading resources from provider
- Loading branch information
Showing
16 changed files
with
247 additions
and
29 deletions.
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,98 @@ | ||
package output | ||
|
||
import ( | ||
"time" | ||
|
||
"go.uber.org/atomic" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var spinner = []string{"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷"} | ||
|
||
const ( | ||
progressTimeout = 10 * time.Second | ||
progressRefreshRate = 200 * time.Millisecond | ||
) | ||
|
||
type Progress interface { | ||
Start() | ||
Stop() | ||
Inc() | ||
Val() uint64 | ||
} | ||
|
||
type progress struct { | ||
ticChan chan struct{} | ||
endChan chan struct{} | ||
started *atomic.Bool | ||
count *atomic.Uint64 | ||
} | ||
|
||
func NewProgress() *progress { | ||
return &progress{ | ||
make(chan struct{}), | ||
make(chan struct{}), | ||
atomic.NewBool(false), | ||
atomic.NewUint64(0), | ||
} | ||
} | ||
|
||
func (p *progress) Start() { | ||
if !p.started.Swap(true) { | ||
go p.watch() | ||
go p.render() | ||
} | ||
} | ||
|
||
func (p *progress) Stop() { | ||
if p.started.Swap(false) { | ||
p.endChan <- struct{}{} | ||
Printf("\n") | ||
} | ||
} | ||
|
||
func (p *progress) Inc() { | ||
if p.started.Load() { | ||
p.ticChan <- struct{}{} | ||
} | ||
} | ||
|
||
func (p *progress) Val() uint64 { | ||
return p.count.Load() | ||
} | ||
|
||
func (p *progress) render() { | ||
i := -1 | ||
Printf("Scanning resources:\r") | ||
for { | ||
select { | ||
case <-p.endChan: | ||
return | ||
case <-time.After(progressRefreshRate): | ||
i++ | ||
if i >= len(spinner) { | ||
i = 0 | ||
} | ||
Printf("Scanning resources: %s (%d)\r", spinner[i], p.count.Load()) | ||
} | ||
} | ||
} | ||
|
||
func (p *progress) watch() { | ||
Loop: | ||
for { | ||
select { | ||
case <-p.ticChan: | ||
p.count.Inc() | ||
continue Loop | ||
case <-time.After(progressTimeout): | ||
p.started.Store(false) | ||
break Loop | ||
case <-p.endChan: | ||
return | ||
} | ||
} | ||
logrus.Debug("Progress did not receive any tic. Stopping...") | ||
p.endChan <- struct{}{} | ||
} |
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,27 @@ | ||
package output | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestProgressTimeout(t *testing.T) { | ||
progress := NewProgress() | ||
progress.Start() | ||
time.Sleep(progressTimeout + 1) | ||
progress.Inc() // should not hang | ||
progress.Stop() // should not hang | ||
assert.Equal(t, uint64(0), progress.Val()) | ||
} | ||
|
||
func TestProgress(t *testing.T) { | ||
progress := NewProgress() | ||
progress.Start() | ||
progress.Inc() | ||
progress.Inc() | ||
progress.Inc() | ||
progress.Stop() | ||
assert.Equal(t, uint64(3), progress.Val()) | ||
} |
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
Oops, something went wrong.