2021-09-18 20:25:59 +08:00
|
|
|
package asynqmon
|
2020-12-02 23:19:06 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2020-12-27 02:05:19 +08:00
|
|
|
"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
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2021-10-02 15:27:41 +08:00
|
|
|
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 {
|
2021-10-02 15:27:41 +08:00
|
|
|
payload["entries"] = toSchedulerEntries(entries, pf)
|
2020-12-02 23:19:06 +08:00
|
|
|
}
|
2020-12-27 02:05:19 +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"`
|
2020-12-27 02:05:19 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:49:41 +08:00
|
|
|
func newListSchedulerEnqueueEventsHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
|
2020-12-27 02:05:19 +08:00
|
|
|
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))
|
2020-12-27 02:05:19 +08:00
|
|
|
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),
|
2020-12-27 02:05:19 +08:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|