Skip to content

Commit

Permalink
delete asynchronously
Browse files Browse the repository at this point in the history
  • Loading branch information
korotkov-aerospike committed Jan 9, 2025
1 parent 687b08c commit cd1fadd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
13 changes: 9 additions & 4 deletions pkg/service/backup_routine_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,15 @@ func (h *BackupRoutineHandler) runFullBackupInternal(ctx context.Context, now ti

h.lastRun.SetFullBackupTime(&now)

err = h.retentionManager.deleteOldBackups(ctx)
if err != nil {
return fmt.Errorf("failed to clean up old backups: %w", err)
}
go func() {
// Cleanup old backups asynchronously.
// At this moment backup is already completed, but backupJob.isRunning flag is still set,
// we don't want to block other backup execution.
err = h.retentionManager.deleteOldBackups(ctx)
if err != nil {
h.logger.Error("failed to clean up old backups", slog.Any("error", err))
}
}()

return nil
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/service/retention_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"slices"
"sync"
"time"

"github.com/aerospike/aerospike-backup-service/v3/pkg/model"
Expand All @@ -18,6 +19,7 @@ type RetentionManager interface {
}

type RetentionManagerImpl struct {
mu sync.Mutex
backend BackupListReader
storage model.Storage
routineName string
Expand All @@ -39,6 +41,11 @@ func NewBackupRetentionManager(
}

func (e *RetentionManagerImpl) deleteOldBackups(ctx context.Context) error {
if !e.mu.TryLock() { // If delete operation already in progress, skip this iteration.
return nil
}
defer e.mu.Unlock()

if e.policy == nil || (e.policy.FullBackups == nil && e.policy.IncrBackups == nil) {
return nil // Retention policy is not enabled, do nothing.
}
Expand Down

0 comments on commit cd1fadd

Please sign in to comment.