asynqmon/scheduler_entry_handlers.go

61 lines
1.8 KiB
Go
Raw Normal View History

package asynqmon
2020-12-02 23:19:06 +08:00
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
2021-09-18 22:23:42 +08:00
2021-05-29 05:40:09 +08:00
"github.com/hibiken/asynq"
2020-12-02 23:19:06 +08:00
)
// ****************************************************************************
// This file defines:
// - http.Handler(s) for scheduler entry related endpoints
// ****************************************************************************
func newListSchedulerEntriesHandlerFunc(inspector *asynq.Inspector, pf PayloadFormatter) http.HandlerFunc {
2020-12-02 23:19:06 +08:00
return func(w http.ResponseWriter, r *http.Request) {
entries, err := inspector.SchedulerEntries()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
payload := make(map[string]interface{})
if len(entries) == 0 {
// avoid nil for the entries field in json output.
2021-10-04 23:18:00 +08:00
payload["entries"] = make([]*schedulerEntry, 0)
2020-12-02 23:19:06 +08:00
} else {
payload["entries"] = toSchedulerEntries(entries, pf)
2020-12-02 23:19:06 +08:00
}
if err := json.NewEncoder(w).Encode(payload); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}
2021-10-04 23:18:00 +08:00
type listSchedulerEnqueueEventsResponse struct {
Events []*schedulerEnqueueEvent `json:"events"`
}
2021-10-01 02:49:41 +08:00
func newListSchedulerEnqueueEventsHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
entryID := mux.Vars(r)["entry_id"]
pageSize, pageNum := getPageOptions(r)
events, err := inspector.ListSchedulerEnqueueEvents(
2021-05-29 05:40:09 +08:00
entryID, asynq.PageSize(pageSize), asynq.Page(pageNum))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-04 23:18:00 +08:00
resp := listSchedulerEnqueueEventsResponse{
2021-10-01 02:49:41 +08:00
Events: toSchedulerEnqueueEvents(events),
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2020-12-02 23:19:06 +08:00
}
}