func AddTriggerToJob(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() vars := mux.Vars(r) job, err := jobList.Get(vars["job"]) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } j := job.(models.Job) payload := unmarshal(r.Body, "trigger", w) j.AppendTrigger(payload["trigger"]) triggerList := models.GetTriggerList() t, err := triggerList.Get(payload["trigger"]) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } executor.AddTrigger(t.(models.Trigger)) jobList.Update(j) w.WriteHeader(201) }
// Walks through each job, seeing if the trigger who's turn it is to execute is attached. Executes those jobs. func findAndRun(t models.Trigger) { jobList := models.GetJobList() jobs := jobList.GetJobsWithTrigger(t.ID()) for _, job := range jobs { println("Executing job " + job.Name) runnit(job) } }
func GetJob(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() vars := mux.Vars(r) job, err := jobList.Get(vars["job"]) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } marshal(job, w) }
func AddJob(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() payload := unmarshal(r.Body, "name", w) err := jobList.Append(models.Job{Name: payload["name"], Status: "New"}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.WriteHeader(201) }
func DeleteJob(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() vars := mux.Vars(r) job, err := jobList.Get(vars["job"]) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } err = jobList.Delete(job.ID()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } }
func AddTaskToJob(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() vars := mux.Vars(r) job, err := jobList.Get(vars["job"]) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } j := job.(models.Job) payload := unmarshal(r.Body, "task", w) j.AppendTask(payload["task"]) jobList.Update(j) w.WriteHeader(201) }
func RemoveTaskFromJob(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() vars := mux.Vars(r) job, err := jobList.Get(vars["job"]) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } j := job.(models.Job) taskPosition, err := strconv.Atoi(vars["task"]) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } j.DeleteTask(taskPosition) jobList.Update(j) }
func AddRun(w http.ResponseWriter, r *http.Request) { runsList := models.GetRunList() jobsList := models.GetJobList() tasksList := models.GetTaskList() payload := unmarshal(r.Body, "job", w) job, err := jobsList.Get(payload["job"]) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } j := job.(models.Job) id, err := uuid.NewV4() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var tasks []models.Task for _, taskName := range j.Tasks { task, err := tasksList.Get(taskName) if err != nil { panic(err) } t := task.(models.Task) tasks = append(tasks, t) } err = runsList.AddRun(id.String(), j, tasks) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } idResponse := make(map[string]string) idResponse["uuid"] = id.String() w.WriteHeader(201) marshal(idResponse, w) }
func RemoveTriggerFromJob(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() vars := mux.Vars(r) job, err := jobList.Get(vars["job"]) if err != nil { http.Error(w, err.Error(), http.StatusNotFound) return } j := job.(models.Job) t := vars["trigger"] j.DeleteTrigger(t) jobList.Update(j) // If Trigger is no longer attached to any Jobs, remove it from Cron to save cycles jobs := jobList.GetJobsWithTrigger(t) if len(jobs) == 0 { executor.RemoveTrigger(t) } }
func ListJobs(w http.ResponseWriter, r *http.Request) { w.Write([]byte(models.GetJobList().Json())) }
func ListJobsForTrigger(w http.ResponseWriter, r *http.Request) { jobList := models.GetJobList() vars := mux.Vars(r) jobs := jobList.GetJobsWithTrigger(vars["trigger"]) marshal(jobs, w) }