mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-01-18 18:55:54 +08:00
Add API endpoints to list scheduler enqueue events
This commit is contained in:
parent
245b0cb18c
commit
8d531c04cd
@ -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
|
||||
}
|
||||
|
1
main.go
1
main.go
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user