Skip to content

Commit dbce178

Browse files
committed
Update.
1 parent 0c7e846 commit dbce178

File tree

4 files changed

+9
-25
lines changed

4 files changed

+9
-25
lines changed

service/rpc/server.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,6 @@ func (s *ServerHandler) ReportSystemState(c context.Context, r *pb.State) (*pb.R
144144
"last_state_json": singleton.ServerList[clientID].LastStateJSON,
145145
"last_online": singleton.ServerList[clientID].LastOnline,
146146
})
147-
148-
// 确认Host信息是否已保存,如果该服务器尚未保存Host信息,尝试保存当前内存中的信息
149-
if singleton.ServerList[clientID].Host != nil {
150-
var count int64
151-
singleton.DB.Raw("SELECT COUNT(*) FROM servers WHERE id = ? AND host_json IS NOT NULL", clientID).Scan(&count)
152-
153-
if count == 0 {
154-
hostJSON, hostErr := utils.Json.Marshal(singleton.ServerList[clientID].Host)
155-
if hostErr == nil && len(hostJSON) > 0 {
156-
singleton.DB.Exec("UPDATE servers SET host_json = ? WHERE id = ?", string(hostJSON), clientID)
157-
}
158-
}
159-
}
160147
} else {
161148
log.Printf("序列化服务器 %s 的最后状态失败: %v", singleton.ServerList[clientID].Name, err)
162149
}

service/singleton/api.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ func (s *ServerAPIService) GetStatusByIDList(idList []uint64) *ServerStatusRespo
119119
host := server.Host
120120
if host == nil || (host.MemTotal == 0 && len(host.CPU) == 0) {
121121
// 尝试从数据库重新加载Host信息
122-
var hostJSON []byte
123-
if err := DB.Raw("SELECT host_json FROM servers WHERE id = ?", server.ID).Scan(&hostJSON).Error; err == nil && len(hostJSON) > 0 {
122+
var hostJSONStr string
123+
if err := DB.Raw("SELECT host_json FROM servers WHERE id = ?", server.ID).Scan(&hostJSONStr).Error; err == nil && len(hostJSONStr) > 0 {
124124
tempHost := &model.Host{}
125-
if err := utils.Json.Unmarshal(hostJSON, tempHost); err == nil {
125+
if err := utils.Json.Unmarshal([]byte(hostJSONStr), tempHost); err == nil {
126126
tempHost.Initialize()
127127
host = tempHost
128128
server.Host = tempHost // 更新内存中的数据
@@ -193,10 +193,10 @@ func (s *ServerAPIService) GetAllStatus() *ServerStatusResponse {
193193
host := v.Host
194194
if host == nil || (host.MemTotal == 0 && len(host.CPU) == 0) {
195195
// 尝试从数据库重新加载Host信息
196-
var hostJSON []byte
197-
if err := DB.Raw("SELECT host_json FROM servers WHERE id = ?", v.ID).Scan(&hostJSON).Error; err == nil && len(hostJSON) > 0 {
196+
var hostJSONStr string
197+
if err := DB.Raw("SELECT host_json FROM servers WHERE id = ?", v.ID).Scan(&hostJSONStr).Error; err == nil && len(hostJSONStr) > 0 {
198198
tempHost := &model.Host{}
199-
if err := utils.Json.Unmarshal(hostJSON, tempHost); err == nil {
199+
if err := utils.Json.Unmarshal([]byte(hostJSONStr), tempHost); err == nil {
200200
tempHost.Initialize()
201201
host = tempHost
202202
v.Host = tempHost // 更新内存中的数据

service/singleton/server.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ func loadServers() {
4242
innerS.IsOnline = false // 初始状态为离线,等待agent报告
4343

4444
// 从数据库加载Host信息
45-
var hostJSON []byte
46-
if err := DB.Raw("SELECT host_json FROM servers WHERE id = ?", innerS.ID).Scan(&hostJSON).Error; err == nil && len(hostJSON) > 0 {
45+
var hostJSONStr string
46+
if err := DB.Raw("SELECT host_json FROM servers WHERE id = ?", innerS.ID).Scan(&hostJSONStr).Error; err == nil && len(hostJSONStr) > 0 {
4747
// 创建Host对象并解析数据
4848
host := &model.Host{}
49-
if err := utils.Json.Unmarshal(hostJSON, host); err != nil {
49+
if err := utils.Json.Unmarshal([]byte(hostJSONStr), host); err != nil {
5050
log.Printf("解析服务器 %s 的Host数据失败: %v", innerS.Name, err)
5151
// 创建空的Host对象作为后备
5252
host = &model.Host{}

service/singleton/singleton.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ func InitDBFromPath(path string) {
6363
if err != nil {
6464
panic(err)
6565
}
66-
if Conf.Debug {
67-
DB = DB.Debug()
68-
}
6966
err = DB.AutoMigrate(model.Server{}, model.User{},
7067
model.Notification{}, model.AlertRule{}, model.Monitor{},
7168
model.MonitorHistory{}, model.Cron{}, model.Transfer{},

0 commit comments

Comments
 (0)