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

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

func (t *Agent) heartbeat() {

	t.registerCache.Range(func(key, value interface{}) bool {
H
heyanlong 已提交
13
		log.Infoln("heartbeat")
H
heyanlong 已提交
14 15 16 17 18 19
		bind := value.(registerCache)

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

H
heyanlong 已提交
20
			_, err := t.grpcClient.pingClient5.Heartbeat(ctx, &agent.ApplicationInstanceHeartbeat{
H
heyanlong 已提交
21 22 23 24 25
				ApplicationInstanceId: bind.InstanceId,
				HeartbeatTime:         time.Now().UnixNano() / 1000000,
			})

			if err != nil {
H
heyanlong 已提交
26
				log.Error("heartbeat:", err)
H
heyanlong 已提交
27
			} else {
H
heyanlong 已提交
28
				log.Infof("heartbeat appId %d appInsId %d", bind.AppId, bind.InstanceId)
H
heyanlong 已提交
29 30 31 32 33 34
			}

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

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