fix: sync create longRunningCtx
This commit is contained in:
parent
cd88fd2668
commit
66cd459306
|
@ -18,6 +18,7 @@ var Sync = &cobra.Command{
|
||||||
Short: "Sync files or directories to Backblaze",
|
Short: "Sync files or directories to Backblaze",
|
||||||
Long: `A tool to backup files and directories to Backblaze.`,
|
Long: `A tool to backup files and directories to Backblaze.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
|
ctx, cancel := context.WithCancel(signalContext(cmd.Context()))
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
|
|
|
@ -43,7 +43,11 @@ func (b *BackBlaze) Sync(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.options.Dir != "" {
|
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 {
|
if err != nil {
|
||||||
return fmt.Errorf("bucketFiles %w", err)
|
return fmt.Errorf("bucketFiles %w", err)
|
||||||
}
|
}
|
||||||
|
@ -60,7 +64,7 @@ func (b *BackBlaze) Sync(ctx context.Context) error {
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
for src := range fileChan {
|
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)
|
b.logger.Errorf("error copying file %s: %v\n", src, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -87,7 +91,7 @@ func (b *BackBlaze) Sync(ctx context.Context) error {
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
// Cleanup old files after backup is completed
|
// 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)
|
return fmt.Errorf("cleanBucket %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue