From 9ec88a5ce8ca12cc5a1bd0e9ed476e0276cadde8 Mon Sep 17 00:00:00 2001 From: bel Date: Mon, 16 Mar 2020 02:10:31 +0000 Subject: [PATCH] implement enable disable with client side display and mod --- TODO.md | 5 ++--- public/js/js.js | 33 ++++++++++++++++++++++++++++++--- scheduler/job.go | 3 +++ server/able.go | 40 ++++++++++++++++++++++++++++++++++++++++ server/delete.go | 32 ++++++++++++++++++++++++++++++++ server/routes.go | 12 ++++++++++++ 6 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 server/able.go create mode 100644 server/delete.go diff --git a/TODO.md b/TODO.md index f69c257..12815f0 100755 --- a/TODO.md +++ b/TODO.md @@ -24,7 +24,6 @@ x JS x load from file 1. interrupt running jobs -1. disable jobs 1. json API 1. list x last run output @@ -32,8 +31,8 @@ x load from file x last run timestamp 1. next run x upsert - 1. delete job - 1. pause/disable job + x delete job + x disable job 1. running job 1. interrupt job 1. force run diff --git a/public/js/js.js b/public/js/js.js index 76b1647..ceec689 100755 --- a/public/js/js.js +++ b/public/js/js.js @@ -20,6 +20,7 @@ function upsert() { console.log("error upserting:", status, body) } } + console.log("to upsert", jsonifyForm("upsert")) http("POST", "/api/job/upsert", cb, jsonifyForm("upsert")) } @@ -27,6 +28,7 @@ function jsonifyForm(id) { var form = document.getElementById(id) var entries = new FormData(form).entries(); var json = Object.assign(...Array.from(entries, ([x,y]) => ({[x]:y}))); + json.disabled = json.disabled == "false" var s = JSON.stringify(json) return s } @@ -67,15 +69,19 @@ function format(job) { ` }) + var strikethrough = "initial" + if (job.disabled) + strikethrough = "line-through" + strikethrough = "text-decoration: "+strikethrough return `
- ${job.title} + ${job.title} ${passing} ${buttons}
- ${job.cron} + ${job.cron} ${job.language}
@@ -126,7 +132,14 @@ function jobmodify(input) { } function jobdelete(input) { - http("DELETE", "/api/job/delete", cb, null) + var job = jobFromInput(input) + if (!confirm(`Delete the job "${job.title}"?`)) + return + function cb(body, status) { + var tr = getJobElement(job.id) + tr.parentNode.removeChild(tr) + } + http("DELETE", "/api/job/delete/"+job.id, cb, null) } function jobrefresh(input) { @@ -154,6 +167,20 @@ function jobrefresh(input) { http("GET", "/api/job/get/"+job.id, cb, null) } +function getJobElement(id) { + var table = getJobsTable() + var summaries = Array.from(table.getElementsByTagName("summary")) + var target = null + summaries.forEach(function(e) { + if (e.getAttribute("name") == id) { + e = e.parentElement + e = e.parentElement.parentElement + target = e + } + }) + return target +} + function jobFromInput(input) { var b64 = input.getAttribute("job") var json = atob(b64) diff --git a/scheduler/job.go b/scheduler/job.go index 251c69a..e593ed9 100755 --- a/scheduler/job.go +++ b/scheduler/job.go @@ -53,6 +53,9 @@ func newBashJob(schedule, sh string, title ...string) (*Job, error) { j.Title = title[0] } j.foo = func() { + if j.Disabled { + return + } cmd := exec.Command("bash", "-c", sh) j.LastRun = time.Now() start := time.Now() diff --git a/server/able.go b/server/able.go new file mode 100644 index 0000000..ce5816d --- /dev/null +++ b/server/able.go @@ -0,0 +1,40 @@ +package server + +import ( + "local/firestormy/config" + "local/firestormy/config/ns" + "local/firestormy/scheduler" + "net/http" + "strings" +) + +func (s *Server) disable(w http.ResponseWriter, r *http.Request) { + s.setDisabled(w, r, true) +} + +func (s *Server) enable(w http.ResponseWriter, r *http.Request) { + s.setDisabled(w, r, false) +} + +func (s *Server) setDisabled(w http.ResponseWriter, r *http.Request, disabled bool) { + keys := strings.Split(r.URL.Path, "/") + key := keys[len(keys)-1] + + j := &scheduler.Job{} + b, err := config.Store.Get(key, ns.Jobs...) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + if err := j.Decode(b); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + j.Disabled = disabled + + if err := scheduler.Schedule.Update(j); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.Write([]byte("{}")) +} diff --git a/server/delete.go b/server/delete.go new file mode 100644 index 0000000..69a57b8 --- /dev/null +++ b/server/delete.go @@ -0,0 +1,32 @@ +package server + +import ( + "local/firestormy/config" + "local/firestormy/config/ns" + "local/firestormy/scheduler" + "net/http" + "strings" +) + +func (s *Server) delete(w http.ResponseWriter, r *http.Request) { + keys := strings.Split(r.URL.Path, "/") + key := keys[len(keys)-1] + + j := &scheduler.Job{} + b, err := config.Store.Get(key, ns.Jobs...) + if err != nil { + http.Error(w, err.Error(), http.StatusNotFound) + return + } + if err := j.Decode(b); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if err := scheduler.Schedule.Remove(j); err != nil { + if err := j.Decode(b); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } + } + w.Write([]byte("{}")) +} diff --git a/server/routes.go b/server/routes.go index c610f3b..187a7ff 100755 --- a/server/routes.go +++ b/server/routes.go @@ -24,6 +24,18 @@ func (s *Server) Routes() error { path: fmt.Sprintf("/api/job/list"), handler: s.gzip(s.authenticate(s.list)), }, + { + path: fmt.Sprintf("/api/job/delete/%s", router.Wildcard), + handler: s.gzip(s.authenticate(s.delete)), + }, + { + path: fmt.Sprintf("/api/job/disable/%s", router.Wildcard), + handler: s.gzip(s.authenticate(s.disable)), + }, + { + path: fmt.Sprintf("/api/job/enable/%s", router.Wildcard), + handler: s.gzip(s.authenticate(s.enable)), + }, { path: fmt.Sprintf("%s%s", wildcard, wildcard), handler: s.gzip(s.authenticate(s.static)),