mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-01-19 03:05:53 +08:00
61 lines
1.8 KiB
Go
61 lines
1.8 KiB
Go
package asynqmon
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/hibiken/asynq"
|
|
)
|
|
|
|
// ****************************************************************************
|
|
// This file defines:
|
|
// - http.Handler(s) for scheduler entry related endpoints
|
|
// ****************************************************************************
|
|
|
|
func newListSchedulerEntriesHandlerFunc(inspector *asynq.Inspector, t *transformer) http.HandlerFunc {
|
|
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.
|
|
payload["entries"] = make([]*SchedulerEntry, 0)
|
|
} else {
|
|
payload["entries"] = t.toSchedulerEntries(entries)
|
|
}
|
|
if err := json.NewEncoder(w).Encode(payload); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
type ListSchedulerEnqueueEventsResponse struct {
|
|
Events []*SchedulerEnqueueEvent `json:"events"`
|
|
}
|
|
|
|
func newListSchedulerEnqueueEventsHandlerFunc(inspector *asynq.Inspector, t *transformer) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
entryID := mux.Vars(r)["entry_id"]
|
|
pageSize, pageNum := getPageOptions(r)
|
|
events, err := inspector.ListSchedulerEnqueueEvents(
|
|
entryID, asynq.PageSize(pageSize), asynq.Page(pageNum))
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
resp := ListSchedulerEnqueueEventsResponse{
|
|
Events: t.toSchedulerEnqueueEvents(events),
|
|
}
|
|
if err := json.NewEncoder(w).Encode(resp); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
}
|
|
}
|