Skip to content

Commit

Permalink
Clean up rsync progress display
Browse files Browse the repository at this point in the history
The current implementation has a memory leak, use Ticker instead.

Also add `--partial`, `--progress`, `--human-readable` flags to rsync
call.

Signed-off-by: Fredrik Lönnegren <fredrik.lonnegren@suse.com>
  • Loading branch information
frelon committed Jun 16, 2023
1 parent 2928cb1 commit e7a9ff0
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,18 @@ func SyncData(log v1.Logger, runner v1.Runner, fs v1.FS, source string, target s

log.Infof("Starting rsync...")

args := []string{source, target, "--archive", "--xattrs", "--acls"}
args := []string{"--progress", "--partial", "--human-readable", "--archive", "--xattrs", "--acls"}
for _, e := range excludes {
args = append(args, fmt.Sprintf("--exclude=%s", e))
}

stop := make(chan bool)
go func(ch <-chan bool) {
for {
select {
case <-ch:
break
case <-time.After(5 * time.Second):
log.Debugf("Syncing files...")
}
}
}(stop)
args = append(args, source, target)

done := displayProgress(5*time.Second, "Syncing data...", ".")

_, err := runner.Run(constants.Rsync, args...)
close(stop)

done <- true

if err != nil {
log.Errorf("rsync finished with errors: %s", err.Error())
Expand All @@ -207,6 +200,27 @@ func SyncData(log v1.Logger, runner v1.Runner, fs v1.FS, source string, target s
return nil
}

func displayProgress(tick time.Duration, message, tickMessage string) chan bool {
ticker := time.NewTicker(tick)
done := make(chan bool)

fmt.Print(message)

go func() {
for {
select {
case <-done:
ticker.Stop()
return
case <-ticker.C:
fmt.Print(tickMessage)
}
}
}()

return done
}

// Reboot reboots the system after the given delay (in seconds) time passed.
func Reboot(runner v1.Runner, delay time.Duration) error {
time.Sleep(delay * time.Second)
Expand Down

0 comments on commit e7a9ff0

Please sign in to comment.