Skip to content

Commit fb51ffc

Browse files
Adds an api/schedule/status route
1 parent a4ad7ff commit fb51ffc

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

Diff for: Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ tools:
4141
fi
4242
build:
4343
$(GO) mod tidy
44-
$(GO) build
44+
$(GO) build -buildvcs=false

Diff for: pkg/http.go

+31
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func setupRouter(s *Schedule) *httprouter.Router {
5050
router.GET("/api/jobs/:jobId/runs/:jobRunId", getJobRun(s))
5151
router.POST("/api/jobs/:jobId/trigger", postTrigger(s))
5252
router.GET("/api/core/logs", getCoreLogs(s))
53+
router.GET("/api/schedule/status", getScheduleStatus(s))
5354

5455
fileServer := http.FileServer(http.FS(fsys()))
5556
router.GET("/static/*filepath", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
@@ -140,6 +141,36 @@ func getJobs(s *Schedule) httprouter.Handle {
140141
}
141142
}
142143

144+
type ScheduleStatusResponse struct {
145+
Status map[string]int `json:"status,omitempty"`
146+
FailedRunCount int `json:"failed_run_count,omitempty"`
147+
HasFailedRuns bool `json:"has_failed_runs,omitempty"`
148+
}
149+
150+
func getScheduleStatus(s *Schedule) httprouter.Handle {
151+
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
152+
w.Header().Set("Content-Type", "application/json")
153+
154+
ssr := ScheduleStatusResponse{
155+
Status: make(map[string]int, len(s.Jobs)),
156+
}
157+
for _, j := range s.Jobs {
158+
j.loadRunsFromDb(1, false)
159+
lastRunStatus := j.Runs[0].Status
160+
ssr.Status[j.Name] = lastRunStatus
161+
if lastRunStatus == 1 {
162+
ssr.FailedRunCount++
163+
}
164+
}
165+
166+
ssr.HasFailedRuns = ssr.FailedRunCount > 0
167+
168+
if err := json.NewEncoder(w).Encode(ssr); err != nil {
169+
http.Error(w, err.Error(), http.StatusInternalServerError)
170+
}
171+
}
172+
}
173+
143174
func getCoreLogs(s *Schedule) httprouter.Handle {
144175
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
145176
w.Header().Set("Content-Type", "application/json")

Diff for: pkg/schedule.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -208,20 +208,20 @@ func (n *notify) EventLeader(f bool) {
208208
func elector(s *Schedule) *election.Election {
209209

210210
conf := api.DefaultConfig()
211-
if len(s.ConsulAclToken) > 0 {
212-
conf.Token = s.ConsulAclToken
213-
}
211+
if len(s.ConsulAclToken) > 0 {
212+
conf.Token = s.ConsulAclToken
213+
}
214214

215-
consul, _ := api.NewClient(conf)
215+
consul, _ := api.NewClient(conf)
216216
n := &notify{
217217
T: "cheek-turner",
218218
}
219219

220-
sessionKey := "service/cheek-turner-election"
220+
sessionKey := "service/cheek-turner-election"
221221

222-
if len(s.ConsulSessionKey) > 0 {
223-
sessionKey = s.ConsulSessionKey
224-
}
222+
if len(s.ConsulSessionKey) > 0 {
223+
sessionKey = s.ConsulSessionKey
224+
}
225225

226226
elconf := &election.ElectionConfig{
227227
CheckTimeout: 5 * time.Second,

0 commit comments

Comments
 (0)