firestormy/server/list.go

50 lines
1.2 KiB
Go
Executable File

package server
import (
"encoding/json"
"local/firestormy/config"
"local/firestormy/config/ns"
"local/firestormy/scheduler"
"net/http"
"sort"
"time"
)
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
jobs, err := config.Store.List(ns.Jobs)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
sort.Strings(jobs)
out := make([]map[string]interface{}, len(jobs))
for i, job := range jobs {
out[i] = make(map[string]interface{})
b, err := config.Store.Get(job, ns.Jobs...)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
j := &scheduler.Job{}
if err := j.Decode(b); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tz, err := time.LoadLocation("America/Denver")
if err != nil {
panic(err)
}
out[i]["id"] = j.Name
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,
}
}
json.NewEncoder(w).Encode(out)
}