package server import ( "context" "errors" "fmt" "strings" "time" "scm.bstein.dev/bstein/soteria/internal/k8s" ) var ( longhornBackupWaitTimeout = 2 * time.Minute longhornBackupPollInterval = 5 * time.Second ) func (s *Server) waitForLonghornBackup(ctx context.Context, snapshotName string) (k8s.LonghornBackupSummary, string, error) { waitCtx, cancel := context.WithTimeout(ctx, longhornBackupWaitTimeout) defer cancel() ticker := time.NewTicker(longhornBackupPollInterval) defer ticker.Stop() for { backup, found, err := s.client.GetLonghornBackupBySnapshot(waitCtx, snapshotName) if err != nil { return k8s.LonghornBackupSummary{}, "backend_error", err } if found { switch strings.ToLower(strings.TrimSpace(backup.State)) { case "completed": return backup, "success", nil case "error", "failed": message := strings.TrimSpace(backup.Error) if message == "" { message = fmt.Sprintf("Longhorn backup %s failed", backup.Name) } return backup, "backend_error", errors.New(message) } } select { case <-waitCtx.Done(): if found { return backup, "in_progress", fmt.Errorf("Longhorn backup %s for snapshot %s is still %s at %d%%", backup.Name, snapshotName, backup.State, backup.Progress) } return k8s.LonghornBackupSummary{}, "in_progress", fmt.Errorf("Longhorn backup for snapshot %s did not appear before timeout", snapshotName) case <-ticker.C: } } }