func (s *Supervisor) wsLog(w http.ResponseWriter, r *http.Request) { name := mux.Vars(r)["name"] log.Println(name) proc, ok := s.procMap[name] if !ok { log.Println("No such process") // TODO: raise error here? return } c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) return } defer c.Close() for data := range proc.Output.NewChanString(r.RemoteAddr) { err := c.WriteMessage(1, []byte(data)) if err != nil { proc.Output.CloseWriter(r.RemoteAddr) break } } }
func (s *Supervisor) wsEvents(w http.ResponseWriter, r *http.Request) { c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) return } defer c.Close() ch := make(chan string, 0) s.addStatusChangeListener(ch) go func() { _, _ = <-ch // ignore the history messages for message := range ch { // Question: type 1 ? c.WriteMessage(1, []byte(message)) } // s.eventB.RemoveListener(ch) }() for { mt, message, err := c.ReadMessage() if err != nil { log.Println("read:", mt, err) break } log.Printf("recv: %v %s", mt, message) err = c.WriteMessage(mt, message) if err != nil { log.Println("write:", err) break } } }
// Performance func (s *Supervisor) wsPerf(w http.ResponseWriter, r *http.Request) { c, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Print("upgrade:", err) return } defer c.Close() name := mux.Vars(r)["name"] proc, ok := s.procMap[name] if !ok { log.Println("No such process") // TODO: raise error here? return } for { // c.SetWriteDeadline(time.Now().Add(3 * time.Second)) if proc.cmd == nil || proc.cmd.Process == nil { log.Println("process not running") return } pid := proc.cmd.Process.Pid ps, err := gops.NewProcess(pid) if err != nil { break } mainPinfo, err := ps.ProcInfo() if err != nil { break } pi := ps.ChildrenProcInfo(true) pi.Add(mainPinfo) err = c.WriteJSON(pi) if err != nil { break } time.Sleep(700 * time.Millisecond) } }