From 8365c0596118739c374c7e2c98b926eb15d10fb6 Mon Sep 17 00:00:00 2001 From: Gregory Eremin Date: Wed, 24 Sep 2014 14:13:08 +0400 Subject: [PATCH] Restore debug handler --- hub/hub.go | 6 +++++- main.go | 6 +----- server/server.go | 37 ++++++++++++------------------------- storage/storage.go | 24 +++++++++++++++++++++++- 4 files changed, 41 insertions(+), 32 deletions(-) diff --git a/hub/hub.go b/hub/hub.go index 39813cd..ab4a3cf 100644 --- a/hub/hub.go +++ b/hub/hub.go @@ -62,7 +62,7 @@ func (h *Hub) Sub(s *Subscription) { func (h *Hub) Info() map[string]map[string]uint { info := make(map[string]map[string]uint) - for queue, size := range h.storage.Info() { + for queue, size := range h.storage.QueueSizes() { info[queue] = map[string]uint{ "messages": size, "subscriptions": 0, @@ -83,6 +83,10 @@ func (h *Hub) Info() map[string]map[string]uint { return info } +func (h *Hub) StorageInfo() map[string]interface{} { + return h.storage.Info() +} + func (h *Hub) cleanupEverySecond() { t := time.NewTicker(1 * time.Second) diff --git a/main.go b/main.go index 22e6f6f..cc82328 100644 --- a/main.go +++ b/main.go @@ -13,10 +13,6 @@ import ( "github.com/KosyanMedia/burlesque/storage" ) -const ( - version = "0.2.0" -) - func main() { var ( storagePath string @@ -40,7 +36,7 @@ func main() { os.Exit(0) }() - fmt.Printf("Burlesque v%s started\n", version) + fmt.Printf("Burlesque v%s started\n", server.Version) fmt.Printf("GOMAXPROCS is set to %d\n", runtime.GOMAXPROCS(-1)) fmt.Printf("Storage path: %s\n", storagePath) fmt.Printf("Server is running at http://127.0.0.1:%d\n", port) diff --git a/server/server.go b/server/server.go index 73288f6..2d80619 100644 --- a/server/server.go +++ b/server/server.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "net/http" + "runtime" "strings" "github.com/KosyanMedia/burlesque/hub" @@ -17,6 +18,10 @@ type ( } ) +const ( + Version = "0.2.0" +) + func New(port int, h *hub.Hub) *Server { s := Server{ port: port, @@ -45,35 +50,17 @@ func (s *Server) statusHandler(w http.ResponseWriter, r *http.Request) { } func (s *Server) debugHandler(w http.ResponseWriter, r *http.Request) { - // info := make(map[string]interface{}) - // info["version"] = version - // info["goroutines"] = runtime.NumGoroutine() + info := make(map[string]interface{}) + info["version"] = Version + info["gomaxprocs"] = runtime.GOMAXPROCS(-1) + info["goroutines"] = runtime.NumGoroutine() + info["kyoto_cabinet"] = s.hub.StorageInfo() - // s, err := storage.Status() - // if err != nil { - // alert(err, "Failed to get Kyoto Cabinet status") - // } - // s = s[:len(s)-1] // Removing trailing new line - - // ks := make(map[string]interface{}) - // tokens := strings.Split(s, "\n") - // for _, t := range tokens { - // tt := strings.Split(t, "\t") - // num, err := strconv.Atoi(tt[1]) - // if err != nil { - // ks[tt[0]] = tt[1] - // } else { - // ks[tt[0]] = num - // } - // } - // info["kyoto_cabinet"] = ks - - // jsn, _ := json.Marshal(info) - // w.Write(jsn) + jsn, _ := json.Marshal(info) + w.Write(jsn) } func (s *Server) pubHandler(w http.ResponseWriter, r *http.Request) { - defer r.Body.Close() msg, _ := ioutil.ReadAll(r.Body) if len(msg) == 0 { msg = []byte(r.FormValue("msg")) diff --git a/storage/storage.go b/storage/storage.go index 5c22623..f2f8beb 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -78,7 +78,7 @@ func (s *Storage) Put(queue string, message []byte) (err error) { return } -func (s *Storage) Info() map[string]uint { +func (s *Storage) QueueSizes() map[string]uint { info := make(map[string]uint) for queue, c := range s.counters { @@ -88,6 +88,28 @@ func (s *Storage) Info() map[string]uint { return info } +func (s *Storage) Info() map[string]interface{} { + info := make(map[string]interface{}) + status, err := s.kyoto.Status() + if err != nil { + panic(err) + } + + status = status[:len(status)-1] // Removing trailing new line + tokens := strings.Split(status, "\n") + for _, t := range tokens { + tt := strings.Split(t, "\t") + num, err := strconv.Atoi(tt[1]) + if err != nil { + info[tt[0]] = tt[1] + } else { + info[tt[0]] = num + } + } + + return info +} + func (s *Storage) Close() (err error) { if err = s.kyoto.Sync(true); err != nil { return