heartbeat.go 1.3 KB
Newer Older
H
heyanlong 已提交
1 2 3 4 5 6 7 8 9 10 11
package service

import (
	"agent/agent/pb/agent"
	"agent/agent/pb/register2"
	"context"
	"time"
)

func (t *Agent) heartbeat() {

H
heyanlong 已提交
12 13
	var heartList []registerCache
	t.registerCacheLock.RLock()
H
heyanlong 已提交
14
	for _, bind := range t.registerCache {
H
heyanlong 已提交
15 16 17 18 19
		heartList = append(heartList, bind)
	}
	t.registerCacheLock.RUnlock()

	for _, bind := range heartList {
H
heyanlong 已提交
20
		log.Infoln("heartbeat")
H
heyanlong 已提交
21 22 23 24
		if bind.Version == 5 {
			ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
			defer cancel()

H
heyanlong 已提交
25
			_, err := t.grpcClient.pingClient5.Heartbeat(ctx, &agent.ApplicationInstanceHeartbeat{
H
heyanlong 已提交
26 27 28 29 30
				ApplicationInstanceId: bind.InstanceId,
				HeartbeatTime:         time.Now().UnixNano() / 1000000,
			})

			if err != nil {
H
heyanlong 已提交
31
				log.Error("heartbeat:", err)
H
heyanlong 已提交
32
			} else {
H
heyanlong 已提交
33
				log.Infof("heartbeat appId %d appInsId %d", bind.AppId, bind.InstanceId)
H
heyanlong 已提交
34 35 36 37 38 39
			}

		} else if bind.Version == 6 {
			ctx, cancel := context.WithTimeout(context.Background(), time.Second*3)
			defer cancel()

H
heyanlong 已提交
40
			_, err := t.grpcClient.pintClient6.DoPing(ctx, &register2.ServiceInstancePingPkg{
H
heyanlong 已提交
41 42 43 44 45
				ServiceInstanceId:   bind.InstanceId,
				Time:                time.Now().UnixNano() / 1000000,
				ServiceInstanceUUID: bind.Uuid,
			})
			if err != nil {
H
heyanlong 已提交
46
				log.Error("heartbeat:", err)
H
heyanlong 已提交
47
			} else {
H
heyanlong 已提交
48
				log.Infof("heartbeat appId %d appInsId %d", bind.AppId, bind.InstanceId)
H
heyanlong 已提交
49 50
			}
		}
H
heyanlong 已提交
51
	}
H
heyanlong 已提交
52
}