mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-25 06:26:35 +08:00
节点根据健康检查自动上下线
This commit is contained in:
@@ -604,6 +604,7 @@ func (this *NodeDAO) FindAllEnabledNodesDNSWithClusterId(clusterId int64) (resul
|
||||
State(NodeStateEnabled).
|
||||
Attr("clusterId", clusterId).
|
||||
Attr("isOn", true).
|
||||
Attr("isUp", true).
|
||||
Result("id", "name", "dnsRoutes", "isOn").
|
||||
DescPk().
|
||||
Slice(&result).
|
||||
@@ -644,6 +645,61 @@ func (this *NodeDAO) UpdateNodeDNS(nodeId int64, routes map[int64]string) error
|
||||
return err
|
||||
}
|
||||
|
||||
// 计算节点上线|下线状态
|
||||
func (this *NodeDAO) UpdateNodeUp(nodeId int64, isUp bool, maxUp int, maxDown int) (changed bool, err error) {
|
||||
if nodeId <= 0 {
|
||||
return false, errors.New("invalid nodeId")
|
||||
}
|
||||
one, err := this.Query().
|
||||
Pk(nodeId).
|
||||
Result("isUp", "countUp", "countDown").
|
||||
Find()
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if one == nil {
|
||||
return false, nil
|
||||
}
|
||||
oldIsUp := one.(*Node).IsUp == 1
|
||||
|
||||
// 如果新老状态一致,则不做任何事情
|
||||
if oldIsUp == isUp {
|
||||
return isUp, nil
|
||||
}
|
||||
|
||||
countUp := int(one.(*Node).CountUp)
|
||||
countDown := int(one.(*Node).CountDown)
|
||||
|
||||
op := NewNodeOperator()
|
||||
op.Id = nodeId
|
||||
|
||||
if isUp {
|
||||
countUp++
|
||||
countDown = 0
|
||||
|
||||
if countUp >= maxUp {
|
||||
changed = true
|
||||
op.IsUp = true
|
||||
}
|
||||
} else {
|
||||
countDown++
|
||||
countUp = 0
|
||||
|
||||
if countDown >= maxDown {
|
||||
changed = true
|
||||
op.IsUp = false
|
||||
}
|
||||
}
|
||||
|
||||
op.CountUp = countUp
|
||||
op.CountDown = countDown
|
||||
_, err = this.Save(op)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 生成唯一ID
|
||||
func (this *NodeDAO) genUniqueId() (string, error) {
|
||||
for {
|
||||
|
||||
Reference in New Issue
Block a user