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

View file

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