func (m *Master) recHeartbeatEvt() { var nextDur time.Duration nowSec := time.Now().Second() if nowSec > 30 { nextDur = time.Duration(90-nowSec) * time.Second } else { nextDur = time.Duration(30-nowSec) * time.Second } t1 := time.NewTimer(nextDur) for { select { case <-t1.C: catInst := cat.Instance() tran := catInst.NewTransaction("System", "Status") h := catInst.NewHeartbeat("HeartBeat", util.GetIP()) combineHeartbeat(h, strconv.Itoa(m.port), heartbeatValues{mapValues: util.GetStatus(), urlValues: nil}) for _, w := range m.workers { combineHeartbeat(h, strconv.Itoa(w.host.GetPort()), w.heartbeatInfo) } h.SetStatus("0") h.Complete() tran.SetStatus("0") tran.Complete() t1.Reset(time.Duration(time.Minute)) } } }
func LogHeartbeat() { defer func() { if err := recover(); err != nil { //log.Error(fmt.Sprintf("%v", err)) LogHeartbeat() } }() ip := util.GetIP() second := time.Now().Second() if second < 29 { sleep := time.Duration((29 - second) * 1000000000) time.Sleep(sleep) } catinstance := cat.Instance() for { //log.Debug("send cat heartbeat") stats := util.GetStatus() tran := catinstance.NewTransaction("System", "Status") h := catinstance.NewHeartbeat("HeartBeat", ip) for key, value := range stats { switch key { case "Alloc", "TotalAlloc", "Sys", "Mallocs", "Frees", "OtherSys", "PauseNs": h.Set("System", key, value) case "HeapAlloc", "HeapSys", "HeapIdle", "HeapInuse", "HeapReleased", "HeapObjects": h.Set("HeapUsage", key, value) case "NextGC", "LastGC", "NumGC": h.Set("GC", key, value) } } h.SetStatus("0") h.Complete() tran.SetStatus("0") tran.Complete() second = time.Now().Second() sleep := time.Duration((90 - second) * 1000000000) time.Sleep(sleep) } }
func (this *SubProcessor) sendStatus() { defer func() { if p := recover(); p != nil { log.WithFields(log.Fields{ "port": this.Port, "type": "Worker.SendStatusPanic", }).Error(fmt.Sprintf("%v", p)) LogErrorEvent(CatInstance, "Worker.SendStatusPanic", fmt.Sprintf("%v", p)) this.sendStatus() } }() for { time.Sleep(10 * time.Second) uri := JoinString("http://localhost:", this.HostPort, "/heartbeat/") log.WithFields(log.Fields{ "port": this.Port, "uri": uri, }).Debug("begin send status") status := util.GetStatus() data := url.Values{} data.Add("port", this.Port) for k, v := range status { data.Add(k, v) } _, err := PostHttp(uri, data) if err != nil { // handle error log.WithFields(log.Fields{ "port": this.Port, "type": "Worker.SendStatusError", }).Error(err.Error()) LogErrorEvent(CatInstance, "Worker.SendStatusError", err.Error()) } } }
func (this *HostProcessor) sendCatHeartBeat() { defer func() { if err := recover(); err != nil { log.Error(fmt.Sprintf("%v", err)) this.sendCatHeartBeat() } }() ip := GetIP() second := time.Now().Second() if second < 29 { sleep := time.Duration((29 - second) * 1000000000) time.Sleep(sleep) } catinstance := cat.Instance() for { log.Debug("send cat heartbeat") stats1 := util.GetStatus() data := url.Values{} data.Add("port", hostPort) for k, v := range stats1 { data.Add(k, v) } portstats[hostPort] = data tran := catinstance.NewTransaction("System", "Status") h := catinstance.NewHeartbeat("HeartBeat", ip) for _, heart := range portstats { if heart == nil { continue } port := heart.Get("port") if port == "" { continue } h.Set("System", JoinString("Alloc_", port), heart.Get("Alloc")) h.Set("System", JoinString("TotalAlloc_", port), heart.Get("TotalAlloc")) h.Set("System", JoinString("Sys_", port), heart.Get("Sys")) h.Set("System", JoinString("Mallocs_", port), heart.Get("Mallocs")) h.Set("System", JoinString("Frees_", port), heart.Get("Frees")) h.Set("System", JoinString("OtherSys_", port), heart.Get("OtherSys")) h.Set("System", JoinString("PauseNs_", port), heart.Get("PauseNs")) h.Set("HeapUsage", JoinString("HeapAlloc_", port), heart.Get("HeapAlloc")) h.Set("HeapUsage", JoinString("HeapSys_", port), heart.Get("HeapSys")) h.Set("HeapUsage", JoinString("HeapIdle_", port), heart.Get("HeapIdle")) h.Set("HeapUsage", JoinString("HeapInuse_", port), heart.Get("HeapInuse")) h.Set("HeapUsage", JoinString("HeapReleased_", port), heart.Get("HeapReleased")) h.Set("HeapUsage", JoinString("HeapObjects_", port), heart.Get("HeapObjects")) h.Set("GC", JoinString("NextGC_", port), heart.Get("NextGC")) h.Set("GC", JoinString("LastGC_", port), heart.Get("LastGC")) h.Set("GC", JoinString("NumGC_", port), heart.Get("NumGC")) portstats[port] = nil } h.SetStatus("0") h.Complete() tran.SetStatus("0") tran.Complete() second = time.Now().Second() sleep := time.Duration((90 - second) * 1000000000) time.Sleep(sleep) } }