mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-10-18 11:26:10 +08:00
fix: fix issue with open container log failed (#9163)
This commit is contained in:
parent
754ac64bf6
commit
8b22acce27
2 changed files with 97 additions and 109 deletions
|
@ -884,14 +884,11 @@ func (u *ContainerService) StreamLogs(ctx *gin.Context, params dto.StreamLog) {
|
|||
errorChan := make(chan error, 1)
|
||||
doneChan := make(chan struct{})
|
||||
|
||||
// 监听客户端连接状态
|
||||
go func() {
|
||||
<-ctx.Request.Context().Done()
|
||||
close(doneChan) // 通知 collectLogs 停止
|
||||
close(doneChan)
|
||||
}()
|
||||
// 启动日志收集协程
|
||||
go collectLogs(doneChan, params, messageChan, errorChan)
|
||||
// 流式发送日志到客户端
|
||||
ctx.Stream(func(w io.Writer) bool {
|
||||
select {
|
||||
case msg, ok := <-messageChan:
|
||||
|
@ -912,12 +909,7 @@ func (u *ContainerService) StreamLogs(ctx *gin.Context, params dto.StreamLog) {
|
|||
})
|
||||
}
|
||||
|
||||
func collectLogs(
|
||||
done <-chan struct{},
|
||||
params dto.StreamLog,
|
||||
messageChan chan<- string,
|
||||
errorChan chan<- error,
|
||||
) {
|
||||
func collectLogs(done <-chan struct{}, params dto.StreamLog, messageChan chan<- string, errorChan chan<- error) {
|
||||
defer close(messageChan)
|
||||
defer close(errorChan)
|
||||
var cmdArgs []string
|
||||
|
@ -953,17 +945,14 @@ func collectLogs(
|
|||
return
|
||||
}
|
||||
|
||||
// 确保在函数退出时清理进程
|
||||
defer func() {
|
||||
if dockerCmd.Process != nil {
|
||||
_ = dockerCmd.Process.Kill()
|
||||
}
|
||||
}()
|
||||
|
||||
// 创建一个scanner来读取stdout
|
||||
scanner := bufio.NewScanner(stdout)
|
||||
|
||||
// 启动一个goroutine监听done信号
|
||||
processKilled := false
|
||||
go func() {
|
||||
<-done
|
||||
|
@ -973,12 +962,10 @@ func collectLogs(
|
|||
}
|
||||
}()
|
||||
|
||||
// 读取日志输出
|
||||
for scanner.Scan() {
|
||||
message := scanner.Text()
|
||||
select {
|
||||
case messageChan <- message:
|
||||
// 消息发送成功
|
||||
case <-done:
|
||||
return
|
||||
}
|
||||
|
@ -988,7 +975,6 @@ func collectLogs(
|
|||
errorChan <- fmt.Errorf("scanner error: %v", err)
|
||||
}
|
||||
|
||||
// 等待命令完成
|
||||
_ = dockerCmd.Wait()
|
||||
}
|
||||
|
||||
|
|
|
@ -153,7 +153,9 @@ const searchLogs = async () => {
|
|||
};
|
||||
eventSource.onerror = (event: MessageEvent) => {
|
||||
stopListening();
|
||||
if (event.data && event.data != '') {
|
||||
MsgError(event.data);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue