asynqmon/queue_handlers.go

139 lines
3.9 KiB
Go
Raw Normal View History

package asynqmon
2020-11-24 22:54:00 +08:00
import (
"encoding/json"
2021-05-29 05:40:09 +08:00
"errors"
2020-11-24 22:54:00 +08:00
"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-11-24 22:54:00 +08:00
)
2020-12-02 23:19:06 +08:00
// ****************************************************************************
// This file defines:
// - http.Handler(s) for queue related endpoints
// ****************************************************************************
func newListQueuesHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 22:54:00 +08:00
return func(w http.ResponseWriter, r *http.Request) {
qnames, err := inspector.Queues()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-04 23:18:00 +08:00
snapshots := make([]*queueStateSnapshot, len(qnames))
2021-03-02 07:13:09 +08:00
for i, qname := range qnames {
2021-05-29 05:40:09 +08:00
qinfo, err := inspector.GetQueueInfo(qname)
2020-11-24 22:54:00 +08:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-01 02:49:41 +08:00
snapshots[i] = toQueueStateSnapshot(qinfo)
2020-11-24 22:54:00 +08:00
}
payload := map[string]interface{}{"queues": snapshots}
json.NewEncoder(w).Encode(payload)
}
}
2021-10-01 02:49:41 +08:00
func newGetQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 22:54:00 +08:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
qname := vars["qname"]
payload := make(map[string]interface{})
2021-05-29 05:40:09 +08:00
qinfo, err := inspector.GetQueueInfo(qname)
2020-11-24 22:54:00 +08:00
if err != nil {
// TODO: Check for queue not found error.
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-01 02:49:41 +08:00
payload["current"] = toQueueStateSnapshot(qinfo)
2020-11-24 22:54:00 +08:00
// TODO: make this n a variable
data, err := inspector.History(qname, 10)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-04 23:18:00 +08:00
var dailyStats []*dailyStats
2020-11-24 22:54:00 +08:00
for _, s := range data {
2021-10-01 02:49:41 +08:00
dailyStats = append(dailyStats, toDailyStats(s))
2020-11-24 22:54:00 +08:00
}
payload["history"] = dailyStats
json.NewEncoder(w).Encode(payload)
}
}
2021-10-01 02:49:41 +08:00
func newDeleteQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-28 14:32:05 +08:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
qname := vars["qname"]
if err := inspector.DeleteQueue(qname, false); err != nil {
2021-05-29 05:40:09 +08:00
if errors.Is(err, asynq.ErrQueueNotFound) {
2020-11-28 14:32:05 +08:00
http.Error(w, err.Error(), http.StatusNotFound)
return
}
2021-05-29 05:40:09 +08:00
if errors.Is(err, asynq.ErrQueueNotEmpty) {
2020-11-28 14:32:05 +08:00
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
}
2021-05-29 05:40:09 +08:00
func newPauseQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 22:54:00 +08:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
qname := vars["qname"]
if err := inspector.PauseQueue(qname); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
}
2021-05-29 05:40:09 +08:00
func newResumeQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 22:54:00 +08:00
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
qname := vars["qname"]
if err := inspector.UnpauseQueue(qname); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
}
2021-10-04 23:18:00 +08:00
type listQueueStatsResponse struct {
Stats map[string][]*dailyStats `json:"stats"`
}
2021-10-01 02:49:41 +08:00
func newListQueueStatsHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
qnames, err := inspector.Queues()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-04 23:18:00 +08:00
resp := listQueueStatsResponse{Stats: make(map[string][]*dailyStats)}
const numdays = 90 // Get stats for the last 90 days.
for _, qname := range qnames {
stats, err := inspector.History(qname, numdays)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-01 02:49:41 +08:00
resp.Stats[qname] = toDailyStatsList(stats)
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}