Skip to content

Commit d25c3c2

Browse files
authored
fix: add deleted flag to Instance struct and update logging behavior to respect deletion status
1 parent 642d982 commit d25c3c2

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

internal/master.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ type Instance struct {
111111
UDPTXBase uint64 `json:"-" gob:"-"` // UDP发送字节数基线(不序列化)
112112
cmd *exec.Cmd `json:"-" gob:"-"` // 命令对象(不序列化)
113113
stopped chan struct{} `json:"-" gob:"-"` // 停止信号通道(不序列化)
114+
deleted bool `json:"-" gob:"-"` // 删除标志(不序列化)
114115
cancelFunc context.CancelFunc `json:"-" gob:"-"` // 取消函数(不序列化)
115116
lastCheckPoint time.Time `json:"-" gob:"-"` // 上次检查点时间(不序列化)
116117
}
@@ -285,18 +286,22 @@ func (w *InstanceLogWriter) Write(p []byte) (n int, err error) {
285286
}
286287

287288
w.instance.lastCheckPoint = time.Now()
288-
w.master.instances.Store(w.instanceID, w.instance)
289-
// 发送检查点更新事件
290-
w.master.sendSSEEvent("update", w.instance)
289+
// 仅当实例未被删除时才存储和发送更新事件
290+
if !w.instance.deleted {
291+
w.master.instances.Store(w.instanceID, w.instance)
292+
w.master.sendSSEEvent("update", w.instance)
293+
}
291294
// 过滤检查点日志
292295
continue
293296
}
294297

295298
// 输出日志加实例ID
296299
fmt.Fprintf(w.target, "%s [%s]\n", line, w.instanceID)
297300

298-
// 发送日志事件
299-
w.master.sendSSEEvent("log", w.instance, line)
301+
// 仅当实例未被删除时才发送日志事件
302+
if !w.instance.deleted {
303+
w.master.sendSSEEvent("log", w.instance, line)
304+
}
300305
}
301306

302307
if err := scanner.Err(); err != nil {
@@ -1201,6 +1206,10 @@ func (m *Master) handleDeleteInstance(w http.ResponseWriter, id string, instance
12011206
return
12021207
}
12031208

1209+
// 标记实例为已删除
1210+
instance.deleted = true
1211+
m.instances.Store(id, instance)
1212+
12041213
if instance.Status == "running" {
12051214
m.stopInstance(instance)
12061215
}

0 commit comments

Comments
 (0)