implement enable disable with client side display and mod

master
bel 2020-03-16 02:10:31 +00:00
parent bdfdb41bba
commit 9ec88a5ce8
6 changed files with 119 additions and 6 deletions

View File

@ -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

View File

@ -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) {
</span>
`
})
var strikethrough = "initial"
if (job.disabled)
strikethrough = "line-through"
strikethrough = "text-decoration: "+strikethrough
return `<tr><td><details>
<summary name="${job.id}">
<span>${job.title}</span>
<span style="${strikethrough}">${job.title}</span>
<span>${passing}</span>
${buttons}
</summary>
<table>
<tr><td>
<code>${job.cron}</code>
<code style="${strikethrough}">${job.cron}</code>
<code>${job.language}</code>
</td></tr>
<tr><td>
@ -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)

View File

@ -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()

40
server/able.go Normal file
View File

@ -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("{}"))
}

32
server/delete.go Normal file
View File

@ -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("{}"))
}

View File

@ -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)),