From a99eccb26b0acdf50a77946e5e50f895e6edd977 Mon Sep 17 00:00:00 2001 From: Doflatango Date: Thu, 14 Dec 2017 16:38:10 +0800 Subject: [PATCH] Batch start/pause operation. --- web/job.go | 63 +++++++++++++++++++++++++++++++---- web/routers.go | 3 ++ web/ui/src/components/Job.vue | 33 +++++++++++++++++- 3 files changed, 92 insertions(+), 7 deletions(-) diff --git a/web/job.go b/web/job.go index 3dc473b..5ac5a7e 100644 --- a/web/job.go +++ b/web/job.go @@ -2,6 +2,7 @@ package web import ( "encoding/json" + "fmt" "net/http" "sort" "strings" @@ -56,26 +57,76 @@ func (j *Job) ChangeJobStatus(ctx *Context) { ctx.R.Body.Close() vars := mux.Vars(ctx.R) - originJob, rev, err := cronsun.GetJobAndRev(vars["group"], vars["id"]) + job, err = j.updateJobStatus(vars["group"], vars["id"], job.Pause) if err != nil { outJSONWithCode(ctx.W, http.StatusInternalServerError, err.Error()) return } - originJob.Pause = job.Pause + outJSON(ctx.W, job) +} + +func (j *Job) updateJobStatus(group, id string, isPause bool) (*cronsun.Job, error) { + originJob, rev, err := cronsun.GetJobAndRev(group, id) + if err != nil { + return nil, err + } + + if originJob.Pause == isPause { + return nil, err + } + + originJob.Pause = isPause b, err := json.Marshal(originJob) if err != nil { - outJSONWithCode(ctx.W, http.StatusInternalServerError, err.Error()) - return + return nil, err } _, err = cronsun.DefalutClient.PutWithModRev(originJob.Key(), string(b), rev) if err != nil { - outJSONWithCode(ctx.W, http.StatusInternalServerError, err.Error()) + return nil, err + } + + return originJob, nil +} + +func (j *Job) BatchChangeJobStatus(ctx *Context) { + var jobIds []string + decoder := json.NewDecoder(ctx.R.Body) + err := decoder.Decode(&jobIds) + if err != nil { + outJSONWithCode(ctx.W, http.StatusBadRequest, err.Error()) + return + } + ctx.R.Body.Close() + + vars := mux.Vars(ctx.R) + op := vars["op"] + var isPause bool + switch op { + case "pause": + isPause = true + case "start": + default: + outJSONWithCode(ctx.W, http.StatusBadRequest, "Unknow batch operation.") return } - outJSON(ctx.W, originJob) + var updated int + for i := range jobIds { + id := strings.Split(jobIds[i], "/") // [Group, ID] + if len(id) != 2 || id[0] == "" || id[1] == "" { + continue + } + + _, err = j.updateJobStatus(id[0], id[1], isPause) + if err != nil { + continue + } + updated++ + } + + outJSON(ctx.W, fmt.Sprintf("%d of %d updated.", updated, len(jobIds))) } func (j *Job) UpdateJob(ctx *Context) { diff --git a/web/routers.go b/web/routers.go index 6ef7e62..1f02e11 100644 --- a/web/routers.go +++ b/web/routers.go @@ -56,6 +56,9 @@ func initRouters() (s *http.Server, err error) { // pause/start h = NewAuthHandler(jobHandler.ChangeJobStatus) subrouter.Handle("/job/{group}-{id}", h).Methods("POST") + // batch pause/start + h = NewAuthHandler(jobHandler.BatchChangeJobStatus) + subrouter.Handle("/jobs/{op}", h).Methods("POST") // get a job h = NewAuthHandler(jobHandler.GetJob) subrouter.Handle("/job/{group}-{id}", h).Methods("GET") diff --git a/web/ui/src/components/Job.vue b/web/ui/src/components/Job.vue index bc492b0..dcddbbd 100644 --- a/web/ui/src/components/Job.vue +++ b/web/ui/src/components/Job.vue @@ -1,11 +1,21 @@