mirror of
				https://github.com/TeaOSLab/EdgeAdmin.git
				synced 2025-11-04 13:10:26 +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"
 | 
			
		||||
 
 | 
			
		||||
@@ -27,7 +27,6 @@
 | 
			
		||||
		</a>
 | 
			
		||||
 | 
			
		||||
        <div class="right menu">
 | 
			
		||||
			<a href="" class="item" v-if="globalChangedClusters.length > 0" @click.prevent="syncClustersConfigs()"><span class="blink"><i class="icon refresh"></i></span>{{globalChangedClusters.length}}个集群服务变更</a>
 | 
			
		||||
			<a href="/messages" class="item" :class="{active:teaMenu == 'message'}"><span :class="{'blink':globalMessageBadge > 0}"><i class="icon bell"></i>消息({{globalMessageBadge}}) </span></a>
 | 
			
		||||
			<a href="/settings/profile" class="item"  :class="{active: teaMenu == 'settings'}">
 | 
			
		||||
				<i class="icon user" v-if="teaUserAvatar.length == 0"></i>
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,5 @@
 | 
			
		||||
Tea.context(function () {
 | 
			
		||||
	this.moreOptionsVisible = false
 | 
			
		||||
	this.globalChangedClusters = []
 | 
			
		||||
	this.globalMessageBadge = 0
 | 
			
		||||
 | 
			
		||||
	if (typeof this.leftMenuItemIsDisabled == "undefined") {
 | 
			
		||||
@@ -12,9 +11,6 @@ Tea.context(function () {
 | 
			
		||||
			this.$refs.focus.focus()
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// 检查变更
 | 
			
		||||
		this.checkClusterChanges()
 | 
			
		||||
 | 
			
		||||
		// 检查消息
 | 
			
		||||
		this.checkMessages()
 | 
			
		||||
	})
 | 
			
		||||
@@ -37,32 +33,6 @@ Tea.context(function () {
 | 
			
		||||
		menu.isActive = !menu.isActive
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 检查集群变更
 | 
			
		||||
	 */
 | 
			
		||||
	this.checkClusterChanges = function () {
 | 
			
		||||
		this.$post("/clusters/checkChange")
 | 
			
		||||
			.params({
 | 
			
		||||
				isNotifying: (this.globalChangedClusters.length > 0) ? 1 : 0
 | 
			
		||||
			})
 | 
			
		||||
			.timeout(60)
 | 
			
		||||
			.success(function (resp) {
 | 
			
		||||
				this.globalChangedClusters = resp.data.clusters;
 | 
			
		||||
			})
 | 
			
		||||
			.fail(function () {
 | 
			
		||||
				this.globalChangedClusters = [];
 | 
			
		||||
			})
 | 
			
		||||
			.done(function () {
 | 
			
		||||
				let delay = 3000
 | 
			
		||||
				if (this.globalChangedClusters.length > 0) {
 | 
			
		||||
					delay = 30000
 | 
			
		||||
				}
 | 
			
		||||
				this.$delay(function () {
 | 
			
		||||
					this.checkClusterChanges()
 | 
			
		||||
				}, delay)
 | 
			
		||||
			})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 检查消息
 | 
			
		||||
	 */
 | 
			
		||||
@@ -83,18 +53,6 @@ Tea.context(function () {
 | 
			
		||||
			})
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 同步集群配置
 | 
			
		||||
	 */
 | 
			
		||||
	this.syncClustersConfigs = function () {
 | 
			
		||||
		teaweb.confirm("html:有若干个集群配置已变更!<br/>确定要同步配置到边缘节点吗?", function () {
 | 
			
		||||
			this.$post("/clusters/sync")
 | 
			
		||||
				.success(function () {
 | 
			
		||||
					this.globalChangedClusters = [];
 | 
			
		||||
				})
 | 
			
		||||
		})
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * 底部伸展框
 | 
			
		||||
	 */
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user