make progress bar POC for pause

This commit is contained in:
Megan Marsh 2021-01-26 14:02:47 -08:00
parent 703b17d47e
commit 77137ef541
2 changed files with 59 additions and 26 deletions

View File

@ -5,7 +5,9 @@ package packer
import ( import (
"io" "io"
"path/filepath" "path/filepath"
"strings"
"sync" "sync"
"time"
pb "github.com/cheggaaa/pb" pb "github.com/cheggaaa/pb"
) )
@ -25,11 +27,66 @@ type UiProgressBar struct {
func (p *UiProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser { func (p *UiProgressBar) TrackProgress(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser {
if p == nil { if p == nil {
return stream return stream
} }
p.lock.Lock() p.lock.Lock()
defer p.lock.Unlock() defer p.lock.Unlock()
// custom prefix used to track configured waits rather than file downloads
// TODO: next time we feel we can justify a breaking interface change, this
// deserves its own method on the progress bar rather than this hacked
// workaround.
if strings.Contains(src, "-packerwaiter-") {
realPrefix := strings.Replace(src, "-packerwaiter-", "", -1)
return p.TrackProgressWait(realPrefix, currentSize, totalSize, stream)
}
return p.TrackProgressFile(src, currentSize, totalSize, stream)
}
func (p *UiProgressBar) TrackProgressWait(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser {
newPb := pb.New64(totalSize)
newPb.SetUnits(pb.U_DURATION)
newPb.ShowPercent = false
newPb.Prefix(src)
if p.pool == nil {
pool := pb.NewPool()
err := pool.Start()
if err != nil {
// here, we probably cannot lock
// stdout, so let's just return
// stream to avoid any error.
return stream
}
p.pool = pool
}
p.pool.Add(newPb)
p.pbs++
return &readCloser{
Reader: nil,
close: func() error {
for i := currentSize; i < totalSize; i++ {
newPb.Increment()
time.Sleep(time.Second)
}
newPb.Finish()
p.lock.Lock()
defer p.lock.Unlock()
p.pbs--
if p.pbs <= 0 {
p.pool.Stop()
p.pool = nil
}
return nil
},
}
}
func (p *UiProgressBar) TrackProgressFile(src string, currentSize, totalSize int64, stream io.ReadCloser) io.ReadCloser {
newPb := pb.New64(totalSize) newPb := pb.New64(totalSize)
newPb.Set64(currentSize) newPb.Set64(currentSize)
ProgressBarConfig(newPb, filepath.Base(src)) ProgressBarConfig(newPb, filepath.Base(src))

View File

@ -167,34 +167,10 @@ func (p *PausedProvisioner) Provision(ctx context.Context, ui packersdk.Ui, comm
} }
func (p *PausedProvisioner) updatesWhilePausing(ctx context.Context, ui packersdk.Ui) error { func (p *PausedProvisioner) updatesWhilePausing(ctx context.Context, ui packersdk.Ui) error {
updateTime := 1
timeTicker := time.NewTicker(time.Duration(updateTime) * time.Second)
TotalTime := int64(math.Round(p.PauseBefore.Seconds())) TotalTime := int64(math.Round(p.PauseBefore.Seconds()))
ElapsedTime := int64(0) ElapsedTime := int64(0)
tickerChannel := make(chan bool) pbCloser := ui.TrackProgress("-packerwaiter-Pausing...", ElapsedTime, TotalTime, ioutil.NopCloser(nil))
var err error pbCloser.Close() // this is blocking
go func() {
for {
select {
case <-timeTicker.C:
ElapsedTime += int64(updateTime)
ui.TrackProgress("Pausing...", ElapsedTime, TotalTime, ioutil.NopCloser(nil))
// ui.Say(fmt.Sprintf("%v seconds left until the next provisioner", TotalTime))
case <-ctx.Done():
err = ctx.Err()
return
case <-tickerChannel:
return
}
}
}()
time.Sleep(p.PauseBefore)
timeTicker.Stop()
if err != nil {
return err
}
tickerChannel <- true
return nil return nil
} }