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