From f80f19fe93a6b4d6c9b14a37cd4641d2bc1b6ce3 Mon Sep 17 00:00:00 2001 From: Renz Date: Tue, 18 Nov 2025 12:13:32 +0000 Subject: [PATCH] Add Ctrl+C interrupt handling for cluster backups - Check context.Done() before starting each database backup - Gracefully cancel ongoing backups on Ctrl+C/SIGTERM - Log cancellation and exit with proper error message - Signal handling already exists in main.go (signal.NotifyContext) --- internal/backup/engine.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/internal/backup/engine.go b/internal/backup/engine.go index 8e14697..c80ee1b 100644 --- a/internal/backup/engine.go +++ b/internal/backup/engine.go @@ -360,6 +360,16 @@ func (e *Engine) BackupCluster(ctx context.Context) error { var wg sync.WaitGroup for i, dbName := range databases { + // Check if context is cancelled before starting new backup + select { + case <-ctx.Done(): + e.log.Info("Backup cancelled by user") + quietProgress.Fail("Backup cancelled by user (Ctrl+C)") + operation.Fail("Backup cancelled") + return fmt.Errorf("backup cancelled: %w", ctx.Err()) + default: + } + wg.Add(1) semaphore <- struct{}{} // Acquire @@ -367,6 +377,15 @@ func (e *Engine) BackupCluster(ctx context.Context) error { defer wg.Done() defer func() { <-semaphore }() // Release + // Check for cancellation at start of goroutine + select { + case <-ctx.Done(): + e.log.Info("Database backup cancelled", "database", name) + atomic.AddInt32(&failCount, 1) + return + default: + } + // Update estimator progress (thread-safe) mu.Lock() estimator.UpdateProgress(idx)