mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-12-16 07:46:35 +08:00
优化边缘节点在线状态管理
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
)
|
||||
|
||||
type APINodeService struct {
|
||||
@@ -231,6 +232,47 @@ func (this *APINodeService) FindCurrentAPINodeVersion(ctx context.Context, req *
|
||||
return &pb.FindCurrentAPINodeVersionResponse{Version: teaconst.Version}, nil
|
||||
}
|
||||
|
||||
// FindCurrentAPINode 获取当前API节点的信息
|
||||
func (this *APINodeService) FindCurrentAPINode(ctx context.Context, req *pb.FindCurrentAPINodeRequest) (*pb.FindCurrentAPINodeResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var nodeId = teaconst.NodeId
|
||||
var tx *dbs.Tx
|
||||
node, err := models.SharedAPINodeDAO.FindEnabledAPINode(tx, nodeId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if node == nil {
|
||||
return &pb.FindCurrentAPINodeResponse{ApiNode: nil}, nil
|
||||
}
|
||||
|
||||
accessAddrs, err := node.DecodeAccessAddrStrings()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &pb.FindCurrentAPINodeResponse{ApiNode: &pb.APINode{
|
||||
Id: int64(node.Id),
|
||||
IsOn: node.IsOn == 1,
|
||||
NodeClusterId: 0,
|
||||
UniqueId: "",
|
||||
Secret: "",
|
||||
Name: "",
|
||||
Description: "",
|
||||
HttpJSON: nil,
|
||||
HttpsJSON: nil,
|
||||
RestIsOn: false,
|
||||
RestHTTPJSON: nil,
|
||||
RestHTTPSJSON: nil,
|
||||
AccessAddrsJSON: []byte(node.AccessAddrs),
|
||||
AccessAddrs: accessAddrs,
|
||||
StatusJSON: nil,
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// CountAllEnabledAPINodesWithSSLCertId 计算使用某个SSL证书的API节点数量
|
||||
func (this *APINodeService) CountAllEnabledAPINodesWithSSLCertId(ctx context.Context, req *pb.CountAllEnabledAPINodesWithSSLCertIdRequest) (*pb.RPCCountResponse, error) {
|
||||
_, err := this.ValidateAdmin(ctx, 0)
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/configs"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/errors"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
|
||||
rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
@@ -82,6 +83,7 @@ func (this *NodeService) NodeStream(server pb.NodeService_NodeStreamServer) erro
|
||||
}
|
||||
|
||||
defer func() {
|
||||
// 修改当前API节点的主边缘节点
|
||||
if primaryNodeId == nodeId {
|
||||
primaryNodeId = 0
|
||||
|
||||
@@ -94,6 +96,12 @@ func (this *NodeService) NodeStream(server pb.NodeService_NodeStreamServer) erro
|
||||
}
|
||||
nodeLocker.Unlock()
|
||||
}
|
||||
|
||||
// 修改在线状态
|
||||
err = models.SharedNodeDAO.UpdateNodeActive(nil, nodeId, false)
|
||||
if err != nil {
|
||||
remotelogs.Error("NODE_SERVICE", "change node active failed: "+err.Error())
|
||||
}
|
||||
}()
|
||||
|
||||
// 返回连接成功
|
||||
@@ -125,6 +133,7 @@ func (this *NodeService) NodeStream(server pb.NodeService_NodeStreamServer) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !oldIsActive {
|
||||
err = models.SharedNodeDAO.UpdateNodeActive(tx, nodeId, true)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user