(w http.ResponseWriter, r *http.Request)
| 2712 | } |
| 2713 | |
| 2714 | func handlePutJob(w http.ResponseWriter, r *http.Request) { |
| 2715 | var job Job |
| 2716 | if err := json.NewDecoder(r.Body).Decode(&job); err != nil { |
| 2717 | http.Error(w, "Invalid request body", http.StatusBadRequest) |
| 2718 | return |
| 2719 | } |
| 2720 | |
| 2721 | if job.CrontabFilename == "" { |
| 2722 | http.Error(w, "Crontab filename is required", http.StatusBadRequest) |
| 2723 | return |
| 2724 | } |
| 2725 | |
| 2726 | crontab, err := lib.GetCrontab(job.CrontabFilename) |
| 2727 | if err != nil { |
| 2728 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 2729 | return |
| 2730 | } |
| 2731 | |
| 2732 | var foundLine *lib.Line |
| 2733 | var foundLineIndex int |
| 2734 | |
| 2735 | // Find the matching line |
| 2736 | for i, line := range crontab.Lines { |
| 2737 | if (job.Code != "" && line.Code == job.Code) || (job.Key != "" && line.Key(crontab.CanonicalName()) == job.Key) { |
| 2738 | foundLine = line |
| 2739 | foundLineIndex = i |
| 2740 | break |
| 2741 | } |
| 2742 | } |
| 2743 | |
| 2744 | if foundLine == nil { |
| 2745 | http.Error(w, "Job not found", http.StatusNotFound) |
| 2746 | return |
| 2747 | } |
| 2748 | |
| 2749 | // Update the line |
| 2750 | hasChanges := false |
| 2751 | |
| 2752 | // Handle suspended status |
| 2753 | if job.Suspended != nil && *job.Suspended != foundLine.IsComment { |
| 2754 | foundLine.IsComment = *job.Suspended |
| 2755 | hasChanges = true |
| 2756 | |
| 2757 | // If the job is monitored, handle pause/unpause |
| 2758 | if job.Monitored { |
| 2759 | if *job.Suspended { |
| 2760 | // Pause the monitor when suspending |
| 2761 | if err := getCronitorApi().PauseMonitor(job.Code, job.PauseHours); err != nil { |
| 2762 | http.Error(w, fmt.Sprintf("Failed to pause monitor: %v", err), http.StatusInternalServerError) |
| 2763 | return |
| 2764 | } |
| 2765 | } else { |
| 2766 | // Unpause the monitor when unsuspending |
| 2767 | if err := getCronitorApi().PauseMonitor(job.Code, "0"); err != nil { |
| 2768 | http.Error(w, fmt.Sprintf("Failed to unpause monitor: %v", err), http.StatusInternalServerError) |
| 2769 | return |
| 2770 | } |
| 2771 | } |
no test coverage detected