From 13128a48616c0a5e3c0ba7c6293883e6c98bdf0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ma=CC=88der?= Date: Mon, 29 Mar 2021 14:46:51 +0200 Subject: [PATCH] Reduce Restic Progress FPS --- logging/logging.go | 7 +------ restic/command.go | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/logging/logging.go b/logging/logging.go index 5246cc15..7193d311 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -55,7 +55,6 @@ type PercentageFunc func(logr.InfoLogger, float64) type BackupOutputParser struct { log logr.Logger errorCount int - lineCounter int summaryFunc SummaryFunc percentageFunc PercentageFunc folder string @@ -162,11 +161,7 @@ func (b *BackupOutputParser) out(s string) error { b.errorCount++ b.log.Error(fmt.Errorf("error occurred during backup"), envelope.Item+" during "+envelope.During+" "+envelope.Error.Op) case "status": - // Restic does the json output with 60hz, which is a bit much... - if b.lineCounter%60 == 0 { - b.percentageFunc(b.log, envelope.PercentDone) - } - b.lineCounter++ + b.percentageFunc(b.log, envelope.PercentDone) case "summary": b.log.Info("backup finished", "new files", envelope.FilesNew, "changed files", envelope.FilesChanged, "errors", b.errorCount) b.log.Info("stats", "time", envelope.TotalDuration, "bytes added", envelope.DataAdded, "bytes processed", envelope.TotalBytesProcessed) diff --git a/restic/command.go b/restic/command.go index b270010c..95db899a 100644 --- a/restic/command.go +++ b/restic/command.go @@ -6,6 +6,7 @@ import ( "io" "os" "os/exec" + "strings" "github.com/go-logr/logr" ) @@ -54,7 +55,8 @@ func (c *Command) Run() { // Mainly set the env vars and wire the right stdins/outs. func (c *Command) Configure() { c.cmd = exec.CommandContext(c.ctx, c.options.Path, c.options.Args...) - c.cmd.Env = os.Environ() + osEnv := os.Environ() + c.cmd.Env = c.setResticProgressFPSIfNotDefined(osEnv) if c.options.StdIn != nil { c.cmd.Stdin = c.options.StdIn @@ -69,6 +71,18 @@ func (c *Command) Configure() { } } +func (c *Command) setResticProgressFPSIfNotDefined(givenEnv []string) []string { + for _, envVar := range givenEnv { + if strings.HasPrefix("RESTIC_PROGRESS_FPS=", envVar) { + return givenEnv + } + } + + const frequency = 1.0 / 60.0 + c.cmdLogger.Info("Defining RESTIC_PROGRESS_FPS", "frequency", frequency) + return append(givenEnv, fmt.Sprintf("RESTIC_PROGRESS_FPS=%f", frequency)) +} + // Start starts the specified command but does not wait for it to complete. func (c *Command) Start() { if c.cmd == nil {