mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 05:00:25 +08:00 
			
		
		
		
	配置变更的时候自动同步,不再需要手工点击同步
This commit is contained in:
		
							
								
								
									
										8
									
								
								internal/events/events.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								internal/events/events.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
package events
 | 
			
		||||
 | 
			
		||||
type Event = string
 | 
			
		||||
 | 
			
		||||
const (
 | 
			
		||||
	EventStart Event = "start" // start loading
 | 
			
		||||
	EventQuit  Event = "quit"  // quit node gracefully
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										27
									
								
								internal/events/utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								internal/events/utils.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
package events
 | 
			
		||||
 | 
			
		||||
import "sync"
 | 
			
		||||
 | 
			
		||||
var eventsMap = map[string][]func(){} // event => []callbacks
 | 
			
		||||
var locker = sync.Mutex{}
 | 
			
		||||
 | 
			
		||||
// 增加事件回调
 | 
			
		||||
func On(event string, callback func()) {
 | 
			
		||||
	locker.Lock()
 | 
			
		||||
	defer locker.Unlock()
 | 
			
		||||
 | 
			
		||||
	callbacks, _ := eventsMap[event]
 | 
			
		||||
	callbacks = append(callbacks, callback)
 | 
			
		||||
	eventsMap[event] = callbacks
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 通知事件
 | 
			
		||||
func Notify(event string) {
 | 
			
		||||
	locker.Lock()
 | 
			
		||||
	callbacks, _ := eventsMap[event]
 | 
			
		||||
	locker.Unlock()
 | 
			
		||||
	
 | 
			
		||||
	for _, callback := range callbacks {
 | 
			
		||||
		callback()
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										16
									
								
								internal/events/utils_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								internal/events/utils_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,16 @@
 | 
			
		||||
package events
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
func TestOn(t *testing.T) {
 | 
			
		||||
	On("hello", func() {
 | 
			
		||||
		t.Log("world")
 | 
			
		||||
	})
 | 
			
		||||
	On("hello", func() {
 | 
			
		||||
		t.Log("world2")
 | 
			
		||||
	})
 | 
			
		||||
	On("hello2", func() {
 | 
			
		||||
		t.Log("world2")
 | 
			
		||||
	})
 | 
			
		||||
	Notify("hello")
 | 
			
		||||
}
 | 
			
		||||
@@ -3,6 +3,7 @@ package nodes
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/configs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/errors"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/events"
 | 
			
		||||
	"github.com/iwind/TeaGo"
 | 
			
		||||
	"github.com/iwind/TeaGo/Tea"
 | 
			
		||||
	"github.com/iwind/TeaGo/logs"
 | 
			
		||||
@@ -50,6 +51,9 @@ func (this *AdminNode) Run() {
 | 
			
		||||
		}
 | 
			
		||||
	}()
 | 
			
		||||
 | 
			
		||||
	// 触发事件
 | 
			
		||||
	events.Notify(events.EventStart)
 | 
			
		||||
 | 
			
		||||
	// 启动API节点
 | 
			
		||||
	this.startAPINode()
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										65
									
								
								internal/tasks/task_sync_cluster.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								internal/tasks/task_sync_cluster.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,65 @@
 | 
			
		||||
package tasks
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/events"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/rpc"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/nodes/nodeutils"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/messageconfigs"
 | 
			
		||||
	"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
 | 
			
		||||
	_ "github.com/iwind/TeaGo/bootstrap"
 | 
			
		||||
	"github.com/iwind/TeaGo/logs"
 | 
			
		||||
	"time"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	events.On(events.EventStart, func() {
 | 
			
		||||
		task := NewSyncClusterTask()
 | 
			
		||||
		go task.Start()
 | 
			
		||||
	})
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 自动同步集群任务
 | 
			
		||||
type SyncClusterTask struct {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func NewSyncClusterTask() *SyncClusterTask {
 | 
			
		||||
	return &SyncClusterTask{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *SyncClusterTask) Start() {
 | 
			
		||||
	ticker := time.NewTicker(3 * time.Second)
 | 
			
		||||
	for range ticker.C {
 | 
			
		||||
		err := this.loop()
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			logs.Println("[TASK][SYNC_CLUSTER]" + err.Error())
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (this *SyncClusterTask) loop() error {
 | 
			
		||||
	rpcClient, err := rpc.SharedRPC()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
	ctx := rpcClient.Context(1)
 | 
			
		||||
	resp, err := rpcClient.NodeClusterRPC().FindAllChangedNodeClusters(ctx, &pb.FindAllChangedNodeClustersRequest{})
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return err
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	for _, cluster := range resp.Clusters {
 | 
			
		||||
		_, err := rpcClient.NodeRPC().SyncNodesVersionWithCluster(ctx, &pb.SyncNodesVersionWithClusterRequest{
 | 
			
		||||
			ClusterId: cluster.Id,
 | 
			
		||||
		})
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 发送通知
 | 
			
		||||
		_, err = nodeutils.SendMessageToCluster(ctx, cluster.Id, messageconfigs.MessageCodeConfigChanged, &messageconfigs.ConfigChangedMessage{}, 10)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			return err
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								internal/tasks/task_sync_cluster_test.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								internal/tasks/task_sync_cluster_test.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,12 @@
 | 
			
		||||
package tasks
 | 
			
		||||
 | 
			
		||||
import "testing"
 | 
			
		||||
 | 
			
		||||
func TestSyncClusterTask_loop(t *testing.T) {
 | 
			
		||||
	task := NewSyncClusterTask()
 | 
			
		||||
	err := task.loop()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		t.Fatal(err)
 | 
			
		||||
	}
 | 
			
		||||
	t.Log("ok")
 | 
			
		||||
}
 | 
			
		||||
@@ -86,7 +86,7 @@ func (this *UserMustAuth) BeforeAction(actionPtr actions.ActionWrapper, paramNam
 | 
			
		||||
			"icon": "clone outsize",
 | 
			
		||||
			"subItems": []maps.Map{
 | 
			
		||||
				{
 | 
			
		||||
					"name": "通用组件",
 | 
			
		||||
					"name": "通用设置",
 | 
			
		||||
					"url":  "/servers/components",
 | 
			
		||||
					"code": "components",
 | 
			
		||||
				},
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
package web
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	_ "github.com/TeaOSLab/EdgeAdmin/internal/tasks"
 | 
			
		||||
	_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/about"
 | 
			
		||||
	_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/api"
 | 
			
		||||
	_ "github.com/TeaOSLab/EdgeAdmin/internal/web/actions/default/api/node"
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user