|
@@ -1,6 +1,7 @@
|
|
|
package pinglog
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
"github.com/gogf/gf/v2/os/glog"
|
|
@@ -106,29 +107,27 @@ func (c *sPing) GetStatus(ctx context.Context, serverid int) (consts.NodeStatus,
|
|
|
return consts.Normal, err
|
|
|
}
|
|
|
|
|
|
- // 如果节点当前是离线状态
|
|
|
+ // 如果节点当前是离线状态,记录下离线时间并返回 Down
|
|
|
if failCount >= pingConfig.NodeDie {
|
|
|
- // Node is down
|
|
|
+ lastDownTime := gtime.Now()
|
|
|
+ err := service.Node().UpdateNodeLasDowTime(ctx, serverid, lastDownTime)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debug(ctx, err.Error())
|
|
|
+ return consts.Normal, err
|
|
|
+ }
|
|
|
return consts.Down, nil
|
|
|
}
|
|
|
|
|
|
// 查询节点上一次有离线记录的时间
|
|
|
- var lastDownTime *gtime.Time
|
|
|
- err = dao.Pinglog.Ctx(ctx).
|
|
|
- Where("avgdelay = 0").
|
|
|
- Where("losspk > ?", pingConfig.NodeLoos).
|
|
|
- Where("maxdelay = 0").
|
|
|
- Where("serverid = ?", serverid).
|
|
|
- OrderDesc("create_at").
|
|
|
- Limit(1).
|
|
|
- Scan(&lastDownTime)
|
|
|
+ lastDownTime, err := service.Node().GetLasDowTime(ctx, serverid)
|
|
|
if err != nil {
|
|
|
glog.Debug(ctx, err.Error())
|
|
|
return consts.Normal, err
|
|
|
}
|
|
|
-
|
|
|
+ glog.Info(ctx, fmt.Sprintf("当前节点ID%d 查询上次离线记录成功", serverid))
|
|
|
// 如果没有找到上一次的离线记录,返回 Normal
|
|
|
if lastDownTime == nil {
|
|
|
+ glog.Info(ctx, fmt.Sprintf("当前节点ID%d 离线记录不为空 返回 Normal", serverid))
|
|
|
return consts.Normal, nil
|
|
|
}
|
|
|
|
|
@@ -142,9 +141,16 @@ func (c *sPing) GetStatus(ctx context.Context, serverid int) (consts.NodeStatus,
|
|
|
glog.Debug(ctx, err.Error())
|
|
|
return consts.Normal, err
|
|
|
}
|
|
|
-
|
|
|
+ glog.Info(ctx, fmt.Sprintf("当前节点ID%d 查询 节点是否恢复成功", serverid))
|
|
|
// 如果节点之前是离线状态且现在恢复了,返回 Recovered
|
|
|
+ // 如果节点之前是离线状态且现在恢复了,清除离线记录并返回 Recovered
|
|
|
if successCount >= pingConfig.NodeRecover {
|
|
|
+ glog.Info(ctx, fmt.Sprintf("当前节点ID%d 查询 之前是离线状态且现在恢复了返回 清除离线记录并返回 Recovered", serverid))
|
|
|
+ err := service.Node().UpdateNodeLasDowTime(ctx, serverid, nil)
|
|
|
+ if err != nil {
|
|
|
+ glog.Debug(ctx, err.Error())
|
|
|
+ return consts.Normal, err
|
|
|
+ }
|
|
|
return consts.Recovered, nil
|
|
|
}
|
|
|
|