2022-04-12 13:16:33 +08:00
|
|
|
|
package singleton
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"sort"
|
|
|
|
|
"sync"
|
2022-04-14 16:41:34 +08:00
|
|
|
|
|
|
|
|
|
"github.com/naiba/nezha/model"
|
2022-04-12 13:16:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2024-10-20 23:23:04 +08:00
|
|
|
|
ServerList map[uint64]*model.Server // [ServerID] -> model.Server
|
|
|
|
|
ServerUUIDToID map[string]uint64 // [ServerUUID] -> ServerID
|
|
|
|
|
ServerLock sync.RWMutex
|
2022-04-12 13:16:33 +08:00
|
|
|
|
|
2022-09-30 22:40:56 +08:00
|
|
|
|
SortedServerList []*model.Server // 用于存储服务器列表的 slice,按照服务器 ID 排序
|
|
|
|
|
SortedServerListForGuest []*model.Server
|
|
|
|
|
SortedServerLock sync.RWMutex
|
2022-04-12 13:16:33 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func InitServer() {
|
|
|
|
|
ServerList = make(map[uint64]*model.Server)
|
2024-10-20 23:23:04 +08:00
|
|
|
|
ServerUUIDToID = make(map[string]uint64)
|
2022-04-12 13:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-07-14 19:41:50 +08:00
|
|
|
|
// loadServers 加载服务器列表并根据ID排序
|
|
|
|
|
func loadServers() {
|
2022-04-12 13:16:33 +08:00
|
|
|
|
InitServer()
|
|
|
|
|
var servers []model.Server
|
|
|
|
|
DB.Find(&servers)
|
|
|
|
|
for _, s := range servers {
|
|
|
|
|
innerS := s
|
|
|
|
|
innerS.Host = &model.Host{}
|
|
|
|
|
innerS.State = &model.HostState{}
|
2024-08-12 10:06:55 +08:00
|
|
|
|
innerS.TaskCloseLock = new(sync.Mutex)
|
2022-04-12 13:16:33 +08:00
|
|
|
|
ServerList[innerS.ID] = &innerS
|
2024-10-20 23:23:04 +08:00
|
|
|
|
ServerUUIDToID[innerS.UUID] = innerS.ID
|
2022-04-12 13:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
ReSortServer()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReSortServer 根据服务器ID 对服务器列表进行排序(ID越大越靠前)
|
|
|
|
|
func ReSortServer() {
|
|
|
|
|
ServerLock.RLock()
|
|
|
|
|
defer ServerLock.RUnlock()
|
|
|
|
|
SortedServerLock.Lock()
|
|
|
|
|
defer SortedServerLock.Unlock()
|
|
|
|
|
|
|
|
|
|
SortedServerList = []*model.Server{}
|
2022-09-30 22:40:56 +08:00
|
|
|
|
SortedServerListForGuest = []*model.Server{}
|
2022-04-12 13:16:33 +08:00
|
|
|
|
for _, s := range ServerList {
|
|
|
|
|
SortedServerList = append(SortedServerList, s)
|
2022-09-30 22:40:56 +08:00
|
|
|
|
if !s.HideForGuest {
|
2024-06-23 15:59:03 +08:00
|
|
|
|
SortedServerListForGuest = append(SortedServerListForGuest, s)
|
2022-09-30 22:40:56 +08:00
|
|
|
|
}
|
2022-04-12 13:16:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按照服务器 ID 排序的具体实现(ID越大越靠前)
|
|
|
|
|
sort.SliceStable(SortedServerList, func(i, j int) bool {
|
|
|
|
|
if SortedServerList[i].DisplayIndex == SortedServerList[j].DisplayIndex {
|
|
|
|
|
return SortedServerList[i].ID < SortedServerList[j].ID
|
|
|
|
|
}
|
|
|
|
|
return SortedServerList[i].DisplayIndex > SortedServerList[j].DisplayIndex
|
|
|
|
|
})
|
2022-09-30 22:40:56 +08:00
|
|
|
|
|
|
|
|
|
sort.SliceStable(SortedServerListForGuest, func(i, j int) bool {
|
|
|
|
|
if SortedServerListForGuest[i].DisplayIndex == SortedServerListForGuest[j].DisplayIndex {
|
|
|
|
|
return SortedServerListForGuest[i].ID < SortedServerListForGuest[j].ID
|
|
|
|
|
}
|
|
|
|
|
return SortedServerListForGuest[i].DisplayIndex > SortedServerListForGuest[j].DisplayIndex
|
|
|
|
|
})
|
2022-04-12 13:16:33 +08:00
|
|
|
|
}
|