Add API endpoints to list scheduler enqueue events

This commit is contained in:
Ken Hibino 2020-12-26 10:05:19 -08:00
parent 245b0cb18c
commit 8d531c04cd
3 changed files with 50 additions and 1 deletions

View File

@ -257,3 +257,23 @@ func toSchedulerEntries(in []*asynq.SchedulerEntry) []*SchedulerEntry {
}
return out
}
type SchedulerEnqueueEvent struct {
TaskID string `json:"task_id"`
EnqueuedAt string `json:"enqueued_at"`
}
func toSchedulerEnqueueEvent(e *asynq.SchedulerEnqueueEvent) *SchedulerEnqueueEvent {
return &SchedulerEnqueueEvent{
TaskID: e.TaskID,
EnqueuedAt: e.EnqueuedAt.Format(time.RFC3339),
}
}
func toSchedulerEnqueueEvents(in []*asynq.SchedulerEnqueueEvent) []*SchedulerEnqueueEvent {
out := make([]*SchedulerEnqueueEvent, len(in))
for i, e := range in {
out[i] = toSchedulerEnqueueEvent(e)
}
return out
}

View File

@ -111,6 +111,7 @@ func main() {
// Scheduler Entry endpoints.
api.HandleFunc("/scheduler_entries", newListSchedulerEntriesHandlerFunc(inspector)).Methods("GET")
api.HandleFunc("/scheduler_entries/{entry_id}/enqueue_events", newListSchedulerEnqueueEventsHandlerFunc(inspector)).Methods("GET")
fs := &staticFileServer{staticPath: "ui/build", indexPath: "index.html"}
router.PathPrefix("/").Handler(fs)

View File

@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/hibiken/asynq"
)
@ -26,6 +27,33 @@ func newListSchedulerEntriesHandlerFunc(inspector *asynq.Inspector) http.Handler
} else {
payload["entries"] = toSchedulerEntries(entries)
}
json.NewEncoder(w).Encode(payload)
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) 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: toSchedulerEnqueueEvents(events),
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}