Reload job in ui

This commit is contained in:
bel
2020-03-15 23:04:48 +00:00
parent 19d4b645b8
commit 8138e31e53
12 changed files with 275 additions and 93 deletions

28
server/get.go Normal file
View File

@@ -0,0 +1,28 @@
package server
import (
"encoding/json"
"local/firestormy/config"
"local/firestormy/config/ns"
"local/firestormy/scheduler"
"net/http"
"strings"
)
func (s *Server) get(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
}
json.NewEncoder(w).Encode(toMap(j))
}

28
server/job.go Normal file
View File

@@ -0,0 +1,28 @@
package server
import (
"local/firestormy/scheduler"
"strings"
"time"
)
func toMap(j *scheduler.Job) map[string]interface{} {
tz, err := time.LoadLocation("America/Denver")
if err != nil {
panic(err)
}
out := make(map[string]interface{})
out["disabled"] = j.Disabled
out["id"] = j.Name
out["title"] = j.Title
out["cron"] = j.Schedule
out["language"] = j.Runner.String()
out["script"] = j.Raw
out["last"] = map[string]interface{}{
"run": j.LastRun.In(tz).Format(`2006-01-02 15:04:05 MST`),
"runtime": j.LastRuntime.String(),
"output": strings.ReplaceAll(j.LastOutput, "\n", "<br>"),
"status": j.LastStatus,
}
return out
}

View File

@@ -7,7 +7,6 @@ import (
"local/firestormy/scheduler"
"net/http"
"sort"
"time"
)
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
@@ -29,22 +28,7 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tz, err := time.LoadLocation("America/Denver")
if err != nil {
panic(err)
}
out[i]["disabled"] = j.Disabled
out[i]["id"] = j.Name
out[i]["title"] = j.Title
out[i]["cron"] = j.Schedule
out[i]["language"] = j.Runner.String()
out[i]["script"] = j.Raw
out[i]["last"] = map[string]interface{}{
"run": j.LastRun.In(tz).Format(`2006-01-02 15:04:05 MST`),
"runtime": j.LastRuntime.String(),
"output": string(j.LastOutput),
"status": j.LastStatus,
}
out[i] = toMap(j)
}
sort.Slice(out, func(i, j int) bool {
return out[i]["title"].(string) < out[j]["title"].(string)

View File

@@ -12,6 +12,10 @@ func (s *Server) Routes() error {
path string
handler http.HandlerFunc
}{
{
path: fmt.Sprintf("/api/job/get/%s", router.Wildcard),
handler: s.gzip(s.authenticate(s.get)),
},
{
path: fmt.Sprintf("/api/job/upsert"),
handler: s.gzip(s.authenticate(s.upsert)),

View File

@@ -7,7 +7,7 @@ import (
"io"
"local/firestormy/config"
"local/firestormy/config/ns"
"log"
"local/firestormy/scheduler"
"net/http"
"github.com/google/uuid"
@@ -52,12 +52,31 @@ func (u *upsertRequest) validate() error {
return nil
}
func (u *upsertRequest) toJob() (*scheduler.Job, error) {
j, err := scheduler.NewJob(scheduler.Bash, u.Cron, u.Script)
if err != nil {
return nil, err
}
j.Title = u.Title
j.Name = u.ID
j.Disabled = u.Disabled
return j, err
}
func (s *Server) upsert(w http.ResponseWriter, r *http.Request) {
upsert, err := newUpsertRequest(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Println("received", upsert)
http.Error(w, "not impl", http.StatusNotImplemented)
job, err := upsert.toJob()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if err := scheduler.Schedule.Update(job); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(map[string]interface{}{})
}