From d0b1afe4251afda44d7c57a94f2a2a9a2b6e30b8 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Fri, 1 Oct 2021 11:13:36 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8A=82=E7=82=B9=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E5=A6=82=E6=9E=9C=E5=8A=A0=E8=BD=BD=E7=9A=84=E6=98=AF=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E9=85=8D=E7=BD=AE=E5=88=99=E5=9C=A8=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E6=81=A2=E5=A4=8D=E5=90=8E=E9=87=8D=E6=96=B0=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/nodes/api_stream.go | 10 ++++++++++ internal/nodes/node.go | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/internal/nodes/api_stream.go b/internal/nodes/api_stream.go index cc0b08f..024a0ca 100644 --- a/internal/nodes/api_stream.go +++ b/internal/nodes/api_stream.go @@ -145,6 +145,16 @@ func (this *APIStream) handleConnectedAPINode(message *pb.NodeStreamMessage) err return errors.Wrap(err) } remotelogs.Println("API_STREAM", "connected to api node '"+strconv.FormatInt(msg.APINodeId, 10)+"'") + + // 重新读取配置 + if nodeConfigUpdatedAt == 0 { + select { + case nodeConfigChangedNotify <- true: + default: + + } + } + return nil } diff --git a/internal/nodes/node.go b/internal/nodes/node.go index ba84065..42103b9 100644 --- a/internal/nodes/node.go +++ b/internal/nodes/node.go @@ -27,11 +27,14 @@ import ( "os" "os/exec" "runtime" + "sync" "time" ) var sharedNodeConfig *nodeconfigs.NodeConfig var nodeTaskNotify = make(chan bool, 8) +var nodeConfigChangedNotify = make(chan bool, 8) +var nodeConfigUpdatedAt int64 var DaemonIsOn = false var DaemonPid = 0 @@ -39,6 +42,7 @@ var DaemonPid = 0 type Node struct { isLoaded bool sock *gosock.Sock + locker sync.Mutex } func NewNode() *Node { @@ -280,6 +284,9 @@ func (this *Node) loop() error { // 读取API配置 func (this *Node) syncConfig() error { + this.locker.Lock() + defer this.locker.Unlock() + // 检查api.yaml是否存在 apiConfigFile := Tea.ConfigFile("api.yaml") _, err := os.Stat(apiConfigFile) @@ -315,6 +322,7 @@ func (this *Node) syncConfig() error { if !configResp.IsChanged { return nil } + nodeConfigUpdatedAt = time.Now().Unix() configJSON := configResp.NodeJSON nodeConfig := &nodeconfigs.NodeConfig{} @@ -398,6 +406,12 @@ func (this *Node) startSyncTimer() { remotelogs.Error("NODE", "sync config error: "+err.Error()) continue } + case <-nodeConfigChangedNotify: + err := this.syncConfig() + if err != nil { + remotelogs.Error("NODE", "sync config error: "+err.Error()) + continue + } } } }()