cmd: Fix -P not ending with a new line

Before this fix rclone didn't wait for the stats to be finished before
exiting, so the final new line was never printed.

After this change rclone will wait for the stats routine to cease
before exiting.
This commit is contained in:
Nick Craig-Wood 2018-10-03 21:46:18 +01:00
parent d9037fe2be
commit 06ae4258be
3 changed files with 37 additions and 26 deletions

View file

@ -17,6 +17,7 @@ import (
"runtime/pprof"
"strconv"
"strings"
"sync"
"time"
"github.com/pkg/errors"
@ -293,7 +294,7 @@ func ShowStats() bool {
// Run the function with stats and retries if required
func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) {
var err error
var stopStats chan struct{}
stopStats := func() {}
if !showStats && ShowStats() {
showStats = true
}
@ -331,9 +332,7 @@ func Run(Retry bool, showStats bool, cmd *cobra.Command, f func() error) {
time.Sleep(*retriesInterval)
}
}
if showStats {
close(stopStats)
}
stopStats()
if err != nil {
log.Printf("Failed to %s: %v", cmd.Name(), err)
resolveExitCode(err)
@ -384,24 +383,31 @@ func CheckArgs(MinArgs, MaxArgs int, cmd *cobra.Command, args []string) {
// StartStats prints the stats every statsInterval
//
// It returns a channel which should be closed to stop the stats.
func StartStats() chan struct{} {
stopStats := make(chan struct{})
if *statsInterval > 0 {
go func() {
ticker := time.NewTicker(*statsInterval)
for {
select {
case <-ticker.C:
accounting.Stats.Log()
case <-stopStats:
ticker.Stop()
return
}
}
}()
// It returns a func which should be called to stop the stats.
func StartStats() func() {
if *statsInterval <= 0 {
return func() {}
}
stopStats := make(chan struct{})
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ticker := time.NewTicker(*statsInterval)
for {
select {
case <-ticker.C:
accounting.Stats.Log()
case <-stopStats:
ticker.Stop()
return
}
}
}()
return func() {
close(stopStats)
wg.Wait()
}
return stopStats
}
// initConfig is run by cobra after initialising the flags

View file

@ -226,8 +226,7 @@ be copied to the vfs cache before opening with --vfs-cache-mode full.
// Show stats if the user has specifically requested them
if cmd.ShowStats() {
stopStats := cmd.StartStats()
defer close(stopStats)
defer cmd.StartStats()()
}
// Skip checkMountEmpty if --allow-non-empty flag is used or if

View file

@ -25,8 +25,8 @@ const (
// startProgress starts the progress bar printing
//
// It returns a channel which should be closed to stop the stats.
func startProgress() chan struct{} {
// It returns a func which should be called to stop the stats.
func startProgress() func() {
stopStats := make(chan struct{})
oldLogPrint := fs.LogPrint
if !log.Redirected() {
@ -36,7 +36,10 @@ func startProgress() chan struct{} {
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
progressInterval := defaultProgressInterval
if ShowStats() && *statsInterval > 0 {
progressInterval = *statsInterval
@ -54,7 +57,10 @@ func startProgress() chan struct{} {
}
}
}()
return stopStats
return func() {
close(stopStats)
wg.Wait()
}
}
// VT100 codes