From 3c4b7ca57b902a7d60e892738c99d54dbe7d8545 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=A5=A5=E8=B6=85?= Date: Mon, 6 Jun 2022 19:39:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8A=82=E7=82=B9=E7=8A=B6=E6=80=81=E4=B8=AD?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=B6=E9=97=B4=E6=88=B3=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../db/models/authority/authority_node_dao.go | 15 +++++++++---- internal/db/models/node_dao.go | 15 ++++++++++--- internal/db/models/ns_node_dao.go | 14 +++++++++---- internal/db/models/report_node_dao.go | 14 +++++++++---- internal/db/models/user_node_dao.go | 14 +++++++++---- .../services/nameservers/service_ns_node.go | 14 +++++++++++-- .../rpc/services/service_authority_node.go | 16 ++++++++++++-- internal/rpc/services/service_node.go | 21 +++++++++++++------ internal/rpc/services/service_user_node.go | 16 ++++++++++++-- 9 files changed, 108 insertions(+), 31 deletions(-) diff --git a/internal/db/models/authority/authority_node_dao.go b/internal/db/models/authority/authority_node_dao.go index 7280d62f..0ccbd34a 100644 --- a/internal/db/models/authority/authority_node_dao.go +++ b/internal/db/models/authority/authority_node_dao.go @@ -1,6 +1,7 @@ package authority import ( + "encoding/json" "github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/utils" @@ -188,13 +189,19 @@ func (this *AuthorityNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) { } // UpdateNodeStatus 更改节点状态 -func (this *AuthorityNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error { - if statusJSON == nil { +func (this *AuthorityNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error { + if nodeStatus == nil { return nil } - _, err := this.Query(tx). + + nodeStatusJSON, err := json.Marshal(nodeStatus) + if err != nil { + return err + } + + _, err = this.Query(tx). Pk(nodeId). - Set("status", string(statusJSON)). + Set("status", nodeStatusJSON). Update() return err } diff --git a/internal/db/models/node_dao.go b/internal/db/models/node_dao.go index 174ffb82..f50c22d1 100644 --- a/internal/db/models/node_dao.go +++ b/internal/db/models/node_dao.go @@ -813,11 +813,20 @@ func (this *NodeDAO) CountAllEnabledNodesMatch(tx *dbs.Tx, } // UpdateNodeStatus 更改节点状态 -func (this *NodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error { - _, err := this.Query(tx). +func (this *NodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error { + if nodeStatus == nil { + return nil + } + + nodeStatusJSON, err := json.Marshal(nodeStatus) + if err != nil { + return err + } + + _, err = this.Query(tx). Pk(nodeId). Set("isActive", true). - Set("status", string(statusJSON)). + Set("status", nodeStatusJSON). Update() return err } diff --git a/internal/db/models/ns_node_dao.go b/internal/db/models/ns_node_dao.go index 25565dd3..56bee800 100644 --- a/internal/db/models/ns_node_dao.go +++ b/internal/db/models/ns_node_dao.go @@ -339,13 +339,19 @@ func (this *NSNodeDAO) UpdateNodeIsInstalled(tx *dbs.Tx, nodeId int64, isInstall } // UpdateNodeStatus 更改节点状态 -func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error { - if statusJSON == nil { +func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error { + if nodeStatus == nil { return nil } - _, err := this.Query(tx). + + nodeStatusJSON, err := json.Marshal(nodeStatus) + if err != nil { + return err + } + + _, err = this.Query(tx). Pk(nodeId). - Set("status", string(statusJSON)). + Set("status", nodeStatusJSON). Update() return err } diff --git a/internal/db/models/report_node_dao.go b/internal/db/models/report_node_dao.go index dcacb49a..189011f8 100644 --- a/internal/db/models/report_node_dao.go +++ b/internal/db/models/report_node_dao.go @@ -264,13 +264,19 @@ func (this *ReportNodeDAO) FindEnabledNodeIdWithUniqueId(tx *dbs.Tx, uniqueId st } // UpdateNodeStatus 更改节点状态 -func (this ReportNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error { - if statusJSON == nil { +func (this ReportNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *reporterconfigs.Status) error { + if nodeStatus == nil { return nil } - _, err := this.Query(tx). + + nodeStatusJSON, err := json.Marshal(nodeStatus) + if err != nil { + return err + } + + _, err = this.Query(tx). Pk(nodeId). - Set("status", string(statusJSON)). + Set("status", nodeStatusJSON). Update() return err } diff --git a/internal/db/models/user_node_dao.go b/internal/db/models/user_node_dao.go index e97886da..b6ab9868 100644 --- a/internal/db/models/user_node_dao.go +++ b/internal/db/models/user_node_dao.go @@ -254,13 +254,19 @@ func (this *UserNodeDAO) GenUniqueId(tx *dbs.Tx) (string, error) { } // UpdateNodeStatus 更改节点状态 -func (this *UserNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, statusJSON []byte) error { - if len(statusJSON) == 0 { +func (this *UserNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error { + if nodeStatus == nil { return nil } - _, err := this.Query(tx). + + nodeStatusJSON, err := json.Marshal(nodeStatus) + if err != nil { + return err + } + + _, err = this.Query(tx). Pk(nodeId). - Set("status", string(statusJSON)). + Set("status", nodeStatusJSON). Update() return err } diff --git a/internal/rpc/services/nameservers/service_ns_node.go b/internal/rpc/services/nameservers/service_ns_node.go index 16533bb9..24dfd75a 100644 --- a/internal/rpc/services/nameservers/service_ns_node.go +++ b/internal/rpc/services/nameservers/service_ns_node.go @@ -18,6 +18,7 @@ import ( stringutil "github.com/iwind/TeaGo/utils/string" "io" "path/filepath" + "time" ) // NSNodeService 域名服务器节点服务 @@ -398,9 +399,18 @@ func (this *NSNodeService) UpdateNSNodeStatus(ctx context.Context, req *pb.Updat return nil, errors.New("'nodeId' should be greater than 0") } - tx := this.NullTx() + var tx = this.NullTx() - err = models.SharedNSNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON) + // 修改时间戳 + var nodeStatus = &nodeconfigs.NodeStatus{} + err = json.Unmarshal(req.StatusJSON, nodeStatus) + if err != nil { + return nil, errors.New("decode node status json failed: " + err.Error()) + } + nodeStatus.UpdatedAt = time.Now().Unix() + + // 保存 + err = models.SharedNSNodeDAO.UpdateNodeStatus(tx, nodeId, nodeStatus) if err != nil { return nil, err } diff --git a/internal/rpc/services/service_authority_node.go b/internal/rpc/services/service_authority_node.go index b03f02bd..86848db6 100644 --- a/internal/rpc/services/service_authority_node.go +++ b/internal/rpc/services/service_authority_node.go @@ -2,11 +2,14 @@ package services import ( "context" + "encoding/json" "github.com/TeaOSLab/EdgeAPI/internal/db/models/authority" "github.com/TeaOSLab/EdgeAPI/internal/errors" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" + "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "google.golang.org/grpc/metadata" + "time" ) type AuthorityNodeService struct { @@ -223,9 +226,18 @@ func (this *AuthorityNodeService) UpdateAuthorityNodeStatus(ctx context.Context, return nil, errors.New("'nodeId' should be greater than 0") } - tx := this.NullTx() + var tx = this.NullTx() - err = authority.SharedAuthorityNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON) + // 修改时间戳 + var nodeStatus = &nodeconfigs.NodeStatus{} + err = json.Unmarshal(req.StatusJSON, nodeStatus) + if err != nil { + return nil, errors.New("decode node status json failed: " + err.Error()) + } + nodeStatus.UpdatedAt = time.Now().Unix() + + // 保存 + err = authority.SharedAuthorityNodeDAO.UpdateNodeStatus(tx, nodeId, nodeStatus) if err != nil { return nil, err } diff --git a/internal/rpc/services/service_node.go b/internal/rpc/services/service_node.go index b1abe969..2a0ee728 100644 --- a/internal/rpc/services/service_node.go +++ b/internal/rpc/services/service_node.go @@ -10,6 +10,7 @@ import ( "github.com/TeaOSLab/EdgeAPI/internal/errors" "github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/installers" + "github.com/TeaOSLab/EdgeAPI/internal/remotelogs" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" "github.com/TeaOSLab/EdgeAPI/internal/utils" "github.com/TeaOSLab/EdgeAPI/internal/utils/numberutils" @@ -21,7 +22,6 @@ import ( "github.com/andybalholm/brotli" "github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/lists" - "github.com/iwind/TeaGo/logs" "github.com/iwind/TeaGo/types" stringutil "github.com/iwind/TeaGo/utils/string" "io" @@ -775,7 +775,7 @@ func (this *NodeService) FindCurrentNodeConfig(ctx context.Context, req *pb.Find // UpdateNodeStatus 更新节点状态 func (this *NodeService) UpdateNodeStatus(ctx context.Context, req *pb.UpdateNodeStatusRequest) (*pb.RPCSuccess, error) { // 校验节点 - _, _, nodeId, err := rpcutils.ValidateRequest(ctx, rpcutils.UserTypeNode) + nodeId, err := this.ValidateNode(ctx) if err != nil { return nil, err } @@ -788,9 +788,18 @@ func (this *NodeService) UpdateNodeStatus(ctx context.Context, req *pb.UpdateNod return nil, errors.New("'nodeId' should be greater than 0") } - tx := this.NullTx() + var tx = this.NullTx() - err = models.SharedNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON) + // 修改时间戳 + var nodeStatus = &nodeconfigs.NodeStatus{} + err = json.Unmarshal(req.StatusJSON, nodeStatus) + if err != nil { + return nil, errors.New("decode node status json failed: " + err.Error()) + } + nodeStatus.UpdatedAt = time.Now().Unix() + + // 保存 + err = models.SharedNodeDAO.UpdateNodeStatus(tx, nodeId, nodeStatus) if err != nil { return nil, err } @@ -825,7 +834,7 @@ func (this *NodeService) InstallNode(ctx context.Context, req *pb.InstallNodeReq goman.New(func() { err = installers.SharedNodeQueue().InstallNodeProcess(req.NodeId, false) if err != nil { - logs.Println("[RPC]install node:" + err.Error()) + remotelogs.Error("NODE_SERVICE", "install node failed:"+err.Error()) } }) @@ -865,7 +874,7 @@ func (this *NodeService) UpgradeNode(ctx context.Context, req *pb.UpgradeNodeReq goman.New(func() { err = installers.SharedNodeQueue().InstallNodeProcess(req.NodeId, true) if err != nil { - logs.Println("[RPC]install node:" + err.Error()) + remotelogs.Error("NODE_SERVICE", "install node:"+err.Error()) } }) diff --git a/internal/rpc/services/service_user_node.go b/internal/rpc/services/service_user_node.go index b976cfc0..a10a8542 100644 --- a/internal/rpc/services/service_user_node.go +++ b/internal/rpc/services/service_user_node.go @@ -2,11 +2,14 @@ package services import ( "context" + "encoding/json" "github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/errors" rpcutils "github.com/TeaOSLab/EdgeAPI/internal/rpc/utils" + "github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "google.golang.org/grpc/metadata" + "time" ) type UserNodeService struct { @@ -259,9 +262,18 @@ func (this *UserNodeService) UpdateUserNodeStatus(ctx context.Context, req *pb.U return nil, errors.New("'nodeId' should be greater than 0") } - tx := this.NullTx() + var tx = this.NullTx() - err = models.SharedUserNodeDAO.UpdateNodeStatus(tx, nodeId, req.StatusJSON) + // 修改时间戳 + var nodeStatus = &nodeconfigs.NodeStatus{} + err = json.Unmarshal(req.StatusJSON, nodeStatus) + if err != nil { + return nil, errors.New("decode node status json failed: " + err.Error()) + } + nodeStatus.UpdatedAt = time.Now().Unix() + + // 保存 + err = models.SharedUserNodeDAO.UpdateNodeStatus(tx, nodeId, nodeStatus) if err != nil { return nil, err }