|
@@ -17,6 +17,10 @@ import (
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
+// 定义一个全局变量保存节点的历史状态和恢复状态
|
|
|
+var nodeStatusHistory = make(map[int]bool)
|
|
|
+var nodeRecovered = make(map[int]bool)
|
|
|
+
|
|
|
func Ping(ctx context.Context) {
|
|
|
glog.Debug(ctx, "start ping .....")
|
|
|
var wg sync.WaitGroup
|
|
@@ -146,14 +150,37 @@ func CheckNodeStatus(ctx context.Context, nextTime time.Time) {
|
|
|
service.TaskLog().Create(ctx, "task_url", err.Error())
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
+ // 获取节点的历史状态
|
|
|
+ historyStatus, exists := nodeStatusHistory[target.Id]
|
|
|
+ if !exists {
|
|
|
+ historyStatus = true // 假设初始状态为正常
|
|
|
+ }
|
|
|
+
|
|
|
if status {
|
|
|
service.TaskLog().Create(ctx, "CheckNodeStatus", fmt.Sprintf("当前节点不正常开启执行urlAndSSH...%d --- %s\n", target.Id, target.Host))
|
|
|
go URLTaskCommand(ctx, target, &wg)
|
|
|
+ // 如果节点当前不正常,重置恢复标志
|
|
|
+ nodeRecovered[target.Id] = false
|
|
|
} else {
|
|
|
service.TaskLog().Create(ctx, "CheckNodeStatus", fmt.Sprintf("当前节点正常...%d --- %s\n", target.Id, target.Host))
|
|
|
+
|
|
|
+ // 检查节点是否从不正常恢复到正常,并且尚未处理过
|
|
|
+ if !historyStatus && !nodeRecovered[target.Id] {
|
|
|
+ // 节点恢复后执行 SSH 命令
|
|
|
+ go func(target *entity.Node) {
|
|
|
+ defer wg.Done()
|
|
|
+ SSHTaskCommand(ctx, target, target.Host, target.Port, target.Id)
|
|
|
+ // 将节点标记为已处理恢复状态
|
|
|
+ nodeRecovered[target.Id] = true
|
|
|
+ }(target)
|
|
|
+ } else {
|
|
|
+ wg.Done()
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- //go SSHTaskCommand(ctx, target, target.Host, target.Port, target.Id, &wg)
|
|
|
+ // 更新节点的历史状态
|
|
|
+ nodeStatusHistory[target.Id] = status
|
|
|
}
|
|
|
|
|
|
wg.Wait()
|