mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-09-22 06:46:34 +08:00
Initial commit
This commit is contained in:
83
queue_handlers.go
Normal file
83
queue_handlers.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/hibiken/asynq"
|
||||
)
|
||||
|
||||
func newListQueuesHandlerFunc(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
|
||||
}
|
||||
var snapshots []*QueueStateSnapshot
|
||||
for _, qname := range qnames {
|
||||
s, err := inspector.CurrentStats(qname)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
snapshots = append(snapshots, toQueueStateSnapshot(s))
|
||||
}
|
||||
payload := map[string]interface{}{"queues": snapshots}
|
||||
json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
}
|
||||
|
||||
func newGetQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
qname := vars["qname"]
|
||||
|
||||
payload := make(map[string]interface{})
|
||||
stats, err := inspector.CurrentStats(qname)
|
||||
if err != nil {
|
||||
// TODO: Check for queue not found error.
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
payload["current"] = toQueueStateSnapshot(stats)
|
||||
|
||||
// TODO: make this n a variable
|
||||
data, err := inspector.History(qname, 10)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
var dailyStats []*DailyStats
|
||||
for _, s := range data {
|
||||
dailyStats = append(dailyStats, toDailyStats(s))
|
||||
}
|
||||
payload["history"] = dailyStats
|
||||
json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
}
|
||||
|
||||
func newPauseQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
func newResumeQueueHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
|
||||
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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user