From 66cd459306d13fb4077d927d98e1d50184457718 Mon Sep 17 00:00:00 2001 From: "Urko." Date: Tue, 26 Dec 2023 16:44:37 +0100 Subject: [PATCH] fix: sync create longRunningCtx --- cmd/sync.go | 1 + internal/services/backblaze/sync.go | 10 +++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cmd/sync.go b/cmd/sync.go index 2272ca1..cd5c820 100644 --- a/cmd/sync.go +++ b/cmd/sync.go @@ -18,6 +18,7 @@ var Sync = &cobra.Command{ Short: "Sync files or directories to Backblaze", Long: `A tool to backup files and directories to Backblaze.`, Run: func(cmd *cobra.Command, args []string) { + ctx, cancel := context.WithCancel(signalContext(cmd.Context())) defer cancel() diff --git a/internal/services/backblaze/sync.go b/internal/services/backblaze/sync.go index 9cc735c..b451f10 100644 --- a/internal/services/backblaze/sync.go +++ b/internal/services/backblaze/sync.go @@ -43,7 +43,11 @@ func (b *BackBlaze) Sync(ctx context.Context) error { } if b.options.Dir != "" { - oldFiles, err := b.bucketFiles(ctx, bc) + // Create a separate context for long-running operations + longRunningCtx, cancelLongRunningOps := context.WithCancel(context.Background()) + defer cancelLongRunningOps() + + oldFiles, err := b.bucketFiles(longRunningCtx, bc) if err != nil { return fmt.Errorf("bucketFiles %w", err) } @@ -60,7 +64,7 @@ func (b *BackBlaze) Sync(ctx context.Context) error { go func() { defer wg.Done() for src := range fileChan { - if err := b.copyFile(ctx, bc, src); err != nil { + if err := b.copyFile(longRunningCtx, bc, src); err != nil { b.logger.Errorf("error copying file %s: %v\n", src, err) continue } @@ -87,7 +91,7 @@ func (b *BackBlaze) Sync(ctx context.Context) error { wg.Wait() // Cleanup old files after backup is completed - if err := b.cleanBucket(ctx, bc, oldFiles); err != nil { + if err := b.cleanBucket(longRunningCtx, bc, oldFiles); err != nil { return fmt.Errorf("cleanBucket %w", err) } }