Skip to content

Commit

Permalink
Clean up rsync progress display (#1789)
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 authored Jun 16, 2023
1 parent 2928cb1 commit 45aa692
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pkg/elemental/elemental_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,8 @@ var _ = Describe("Elemental", Label("elemental"), func() {
runner.SideEffect = func(cmd string, args ...string) ([]byte, error) {
if cmd == "rsync" {
rsyncCount += 1
src = args[0]
dest = args[1]
src = args[len(args)-2]
dest = args[len(args)-1]
}

return []byte{}, nil
Expand Down
38 changes: 25 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(log, 5*time.Second, "Syncing data...")

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

close(done)

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

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

go func() {
for {
select {
case <-done:
ticker.Stop()
return
case <-ticker.C:
log.Debug(message)
}
}
}()

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 45aa692

Please sign in to comment.