MCPcopy Create free account
hub / github.com/cronitorio/cronitor-cli / handleDeleteJob

Function handleDeleteJob

cmd/dash.go:2213–2269  ·  view source on GitHub ↗

handleDeleteJob handles DELETE requests to delete a job

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

2211
2212// handleDeleteJob handles DELETE requests to delete a job
2213func handleDeleteJob(w http.ResponseWriter, r *http.Request) {
2214 if r.Method != "DELETE" {
2215 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
2216 return
2217 }
2218
2219 var job Job
2220 if err := json.NewDecoder(r.Body).Decode(&job); err != nil {
2221 http.Error(w, "Invalid request body", http.StatusBadRequest)
2222 return
2223 }
2224
2225 crontab, err := lib.GetCrontab(job.CrontabFilename)
2226 if err != nil {
2227 http.Error(w, err.Error(), http.StatusInternalServerError)
2228 return
2229 }
2230
2231 var foundLine *lib.Line
2232 var foundLineIndex int
2233
2234 // Find the matching line
2235 for i, line := range crontab.Lines {
2236 if (job.Code != "" && line.Code == job.Code) || (job.Key != "" && line.Key(crontab.CanonicalName()) == job.Key) {
2237 foundLine = line
2238 foundLineIndex = i
2239 break
2240 }
2241 }
2242
2243 if foundLine == nil {
2244 http.Error(w, "Job not found", http.StatusNotFound)
2245 return
2246 }
2247
2248 // If the job is monitored, pause it indefinitely
2249 if job.Monitored {
2250 if err := getCronitorApi().PauseMonitor(job.Code, ""); err != nil {
2251 http.Error(w, fmt.Sprintf("Failed to pause monitor: %v", err), http.StatusInternalServerError)
2252 return
2253 }
2254 }
2255
2256 // Remove the line from the crontab
2257 crontab.Lines = append(crontab.Lines[:foundLineIndex], crontab.Lines[foundLineIndex+1:]...)
2258
2259 // Save the crontab
2260 if err := crontab.Save(crontab.Write()); err != nil {
2261 http.Error(w, "Failed to save crontab", http.StatusInternalServerError)
2262 return
2263 }
2264
2265 // Invalidate cache since we modified a crontab
2266 invalidateCrontabCache()
2267
2268 w.WriteHeader(http.StatusOK)
2269}
2270

Callers 1

handleJobsFunction · 0.85

Calls 9

GetCrontabFunction · 0.92
getCronitorApiFunction · 0.85
invalidateCrontabCacheFunction · 0.85
ErrorMethod · 0.80
KeyMethod · 0.80
CanonicalNameMethod · 0.80
PauseMonitorMethod · 0.80
SaveMethod · 0.80
WriteMethod · 0.45

Tested by

no test coverage detected