mirror of
https://github.com/TeaOSLab/EdgeAPI.git
synced 2025-11-08 03:00:26 +08:00
优化代码
This commit is contained in:
@@ -337,7 +337,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
var countries = []*iplibrary.Country{}
|
||||
for _, country := range dbCountries {
|
||||
countries = append(countries, &iplibrary.Country{
|
||||
Id: int64(country.Id),
|
||||
Id: country.Id,
|
||||
Name: country.DisplayName(),
|
||||
Codes: country.AllCodes(),
|
||||
})
|
||||
@@ -352,7 +352,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
var provinces = []*iplibrary.Province{}
|
||||
for _, province := range dbProvinces {
|
||||
provinces = append(provinces, &iplibrary.Province{
|
||||
Id: int64(province.Id),
|
||||
Id: province.Id,
|
||||
Name: province.DisplayName(),
|
||||
Codes: province.AllCodes(),
|
||||
})
|
||||
@@ -367,7 +367,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
var cities = []*iplibrary.City{}
|
||||
for _, city := range dbCities {
|
||||
cities = append(cities, &iplibrary.City{
|
||||
Id: int64(city.Id),
|
||||
Id: city.Id,
|
||||
Name: city.DisplayName(),
|
||||
Codes: city.AllCodes(),
|
||||
})
|
||||
@@ -382,7 +382,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
var towns = []*iplibrary.Town{}
|
||||
for _, town := range dbTowns {
|
||||
towns = append(towns, &iplibrary.Town{
|
||||
Id: int64(town.Id),
|
||||
Id: town.Id,
|
||||
Name: town.DisplayName(),
|
||||
Codes: town.AllCodes(),
|
||||
})
|
||||
@@ -397,7 +397,7 @@ func (this *IPLibraryFileDAO) GenerateIPLibrary(tx *dbs.Tx, libraryFileId int64)
|
||||
var providers = []*iplibrary.Provider{}
|
||||
for _, provider := range dbProviders {
|
||||
providers = append(providers, &iplibrary.Provider{
|
||||
Id: int64(provider.Id),
|
||||
Id: provider.Id,
|
||||
Name: provider.DisplayName(),
|
||||
Codes: provider.AllCodes(),
|
||||
})
|
||||
|
||||
@@ -12,7 +12,7 @@ func TestIPLibraryFileDAO_GenerateIPLibrary(t *testing.T) {
|
||||
dbs.NotifyReady()
|
||||
|
||||
var tx *dbs.Tx
|
||||
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 3)
|
||||
err := models.SharedIPLibraryFileDAO.GenerateIPLibrary(tx, 4)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"github.com/iwind/TeaGo/rands"
|
||||
"github.com/iwind/TeaGo/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -347,7 +348,7 @@ func (this *NSNodeDAO) UpdateNodeIsInstalled(tx *dbs.Tx, nodeId int64, isInstall
|
||||
}
|
||||
|
||||
// UpdateNodeStatus 更改节点状态
|
||||
func (this NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error {
|
||||
func (this *NSNodeDAO) UpdateNodeStatus(tx *dbs.Tx, nodeId int64, nodeStatus *nodeconfigs.NodeStatus) error {
|
||||
if nodeStatus == nil {
|
||||
return nil
|
||||
}
|
||||
@@ -516,6 +517,7 @@ func (this *NSNodeDAO) UpdateNodeActive(tx *dbs.Tx, nodeId int64, isActive bool)
|
||||
Pk(nodeId).
|
||||
Set("isActive", isActive).
|
||||
Set("statusIsNotified", false).
|
||||
Set("inactiveNotifiedAt", 0).
|
||||
Update()
|
||||
return err
|
||||
}
|
||||
@@ -562,9 +564,18 @@ func (this *NSNodeDAO) UpdateNodeStatusIsNotified(tx *dbs.Tx, nodeId int64) erro
|
||||
return this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Set("statusIsNotified", true).
|
||||
Set("inactiveNotifiedAt", time.Now().Unix()).
|
||||
UpdateQuickly()
|
||||
}
|
||||
|
||||
// FindNodeInactiveNotifiedAt 读取上次的节点离线通知时间
|
||||
func (this *NSNodeDAO) FindNodeInactiveNotifiedAt(tx *dbs.Tx, nodeId int64) (int64, error) {
|
||||
return this.Query(tx).
|
||||
Pk(nodeId).
|
||||
Result("inactiveNotifiedAt").
|
||||
FindInt64Col(0)
|
||||
}
|
||||
|
||||
// FindAllNodeIdsMatch 匹配节点并返回节点ID
|
||||
func (this *NSNodeDAO) FindAllNodeIdsMatch(tx *dbs.Tx, clusterId int64, includeSecondaryNodes bool, isOn configutils.BoolState) (result []int64, err error) {
|
||||
query := this.Query(tx)
|
||||
|
||||
@@ -19,26 +19,28 @@ type NSNode struct {
|
||||
State uint8 `field:"state"` // 状态
|
||||
IsActive bool `field:"isActive"` // 是否活跃
|
||||
StatusIsNotified uint8 `field:"statusIsNotified"` // 活跃状态已经通知
|
||||
InactiveNotifiedAt uint64 `field:"inactiveNotifiedAt"` // 离线通知时间
|
||||
ConnectedAPINodes dbs.JSON `field:"connectedAPINodes"` // 当前连接的API节点
|
||||
}
|
||||
|
||||
type NSNodeOperator struct {
|
||||
Id interface{} // ID
|
||||
AdminId interface{} // 管理员ID
|
||||
ClusterId interface{} // 集群ID
|
||||
Name interface{} // 节点名称
|
||||
IsOn interface{} // 是否启用
|
||||
Status interface{} // 运行状态
|
||||
UniqueId interface{} // 节点ID
|
||||
Secret interface{} // 密钥
|
||||
IsUp interface{} // 是否运行
|
||||
IsInstalled interface{} // 是否已安装
|
||||
InstallStatus interface{} // 安装状态
|
||||
InstallDir interface{} // 安装目录
|
||||
State interface{} // 状态
|
||||
IsActive interface{} // 是否活跃
|
||||
StatusIsNotified interface{} // 活跃状态已经通知
|
||||
ConnectedAPINodes interface{} // 当前连接的API节点
|
||||
Id any // ID
|
||||
AdminId any // 管理员ID
|
||||
ClusterId any // 集群ID
|
||||
Name any // 节点名称
|
||||
IsOn any // 是否启用
|
||||
Status any // 运行状态
|
||||
UniqueId any // 节点ID
|
||||
Secret any // 密钥
|
||||
IsUp any // 是否运行
|
||||
IsInstalled any // 是否已安装
|
||||
InstallStatus any // 安装状态
|
||||
InstallDir any // 安装目录
|
||||
State any // 状态
|
||||
IsActive any // 是否活跃
|
||||
StatusIsNotified any // 活跃状态已经通知
|
||||
InactiveNotifiedAt any // 离线通知时间
|
||||
ConnectedAPINodes any // 当前连接的API节点
|
||||
}
|
||||
|
||||
func NewNSNodeOperator() *NSNodeOperator {
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -1,92 +0,0 @@
|
||||
package tasks
|
||||
|
||||
import (
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
|
||||
"github.com/TeaOSLab/EdgeAPI/internal/goman"
|
||||
"github.com/TeaOSLab/EdgeCommon/pkg/nodeconfigs"
|
||||
"github.com/iwind/TeaGo/dbs"
|
||||
"time"
|
||||
)
|
||||
|
||||
func init() {
|
||||
dbs.OnReadyDone(func() {
|
||||
goman.New(func() {
|
||||
NewNSNodeMonitorTask(1 * time.Minute).Start()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// NSNodeMonitorTask 边缘节点监控任务
|
||||
type NSNodeMonitorTask struct {
|
||||
BaseTask
|
||||
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
func NewNSNodeMonitorTask(duration time.Duration) *NSNodeMonitorTask {
|
||||
return &NSNodeMonitorTask{
|
||||
ticker: time.NewTicker(duration),
|
||||
}
|
||||
}
|
||||
|
||||
func (this *NSNodeMonitorTask) Start() {
|
||||
for range this.ticker.C {
|
||||
err := this.Loop()
|
||||
if err != nil {
|
||||
this.logErr("NS_NODE_MONITOR", err.Error())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (this *NSNodeMonitorTask) Loop() error {
|
||||
// 检查是否为主节点
|
||||
if !models.SharedAPINodeDAO.CheckAPINodeIsPrimaryWithoutErr() {
|
||||
return nil
|
||||
}
|
||||
|
||||
clusters, err := models.SharedNSClusterDAO.FindAllEnabledClusters(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, cluster := range clusters {
|
||||
err := this.monitorCluster(cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *NSNodeMonitorTask) monitorCluster(cluster *models.NSCluster) error {
|
||||
clusterId := int64(cluster.Id)
|
||||
|
||||
// 检查离线节点
|
||||
inactiveNodes, err := models.SharedNSNodeDAO.FindAllNotifyingInactiveNodesWithClusterId(nil, clusterId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, node := range inactiveNodes {
|
||||
subject := "DNS节点\"" + node.Name + "\"已处于离线状态"
|
||||
msg := "DNS节点\"" + node.Name + "\"已处于离线状态"
|
||||
err = models.SharedMessageDAO.CreateNodeMessage(nil, nodeconfigs.NodeRoleDNS, clusterId, int64(node.Id), models.MessageTypeNSNodeInactive, models.LevelError, subject, msg, nil, false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 修改在线状态
|
||||
err = models.SharedNSNodeDAO.UpdateNodeStatusIsNotified(nil, int64(node.Id))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// TODO 检查恢复连接
|
||||
|
||||
// 检查CPU、内存、磁盘不足节点,而且离线的节点不再重复提示
|
||||
// TODO 需要实现
|
||||
|
||||
// TODO 检查53/tcp、53/udp是否能够访问
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user