asynqmon/queue_handlers.go

139 lines
3.9 KiB
Go
Raw Permalink Normal View History

package asynqmon
2020-11-24 06:54:00 -08:00
import (
"encoding/json"
2021-05-28 14:40:09 -07:00
"errors"
2020-11-24 06:54:00 -08:00
"net/http"
"github.com/gorilla/mux"
2021-09-18 19:53:42 +05:30
2021-05-28 14:40:09 -07:00
"github.com/hibiken/asynq"
2020-11-24 06:54:00 -08:00
)
2020-12-02 07:19:06 -08:00
// ****************************************************************************
// This file defines:
// - http.Handler(s) for queue related endpoints
// ****************************************************************************
func newListQueuesHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 06: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 20:48:00 +05:30
snapshots := make([]*queueStateSnapshot, len(qnames))
2021-03-01 15:13:09 -08:00
for i, qname := range qnames {
2021-05-28 14:40:09 -07:00
qinfo, err := inspector.GetQueueInfo(qname)
2020-11-24 06:54:00 -08:00
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
2021-10-01 00:19:41 +05:30
snapshots[i] = toQueueStateSnapshot(qinfo)
2020-11-24 06:54:00 -08:00
}
payload := map[string]interface{}{"queues": snapshots}
json.NewEncoder(w).Encode(payload)
}
}
2021-10-01 00:19:41 +05:30
func newGetQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 06: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-28 14:40:09 -07:00
qinfo, err := inspector.GetQueueInfo(qname)
2020-11-24 06: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 00:19:41 +05:30
payload["current"] = toQueueStateSnapshot(qinfo)
2020-11-24 06: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 20:48:00 +05:30
var dailyStats []*dailyStats
2020-11-24 06:54:00 -08:00
for _, s := range data {
2021-10-01 00:19:41 +05:30
dailyStats = append(dailyStats, toDailyStats(s))
2020-11-24 06:54:00 -08:00
}
payload["history"] = dailyStats
json.NewEncoder(w).Encode(payload)
}
}
2021-10-01 00:19:41 +05:30
func newDeleteQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-27 22: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-28 14:40:09 -07:00
if errors.Is(err, asynq.ErrQueueNotFound) {
2020-11-27 22:32:05 -08:00
http.Error(w, err.Error(), http.StatusNotFound)
return
}
2021-05-28 14:40:09 -07:00
if errors.Is(err, asynq.ErrQueueNotEmpty) {
2020-11-27 22: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-28 14:40:09 -07:00
func newPauseQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 06: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-28 14:40:09 -07:00
func newResumeQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
2020-11-24 06: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 20:48:00 +05:30
type listQueueStatsResponse struct {
Stats map[string][]*dailyStats `json:"stats"`
}
2021-10-01 00:19:41 +05:30
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 20:48:00 +05:30
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 00:19:41 +05:30
resp.Stats[qname] = toDailyStatsList(stats)
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
}