1
0
mirror of https://github.com/1Panel-dev/1Panel.git synced 2025-01-31 14:08:06 +08:00

feat(log): Handle Errors in Event Stream (#7708)

This commit is contained in:
zhengkunwang 2025-01-13 23:08:05 +08:00 committed by GitHub
parent e2354b8528
commit c4f9d29bcb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 27 additions and 12 deletions

View File

@ -811,10 +811,8 @@ func (u *ContainerService) StreamLogs(ctx *gin.Context, params dto.StreamLog) {
}
return true
case err := <-errorChan:
_, err = fmt.Fprintf(w, "data: {\"event\": \"error\", \"data\": \"%s\"}\n\n", err.Error())
if err != nil {
return false
}
errorMsg := fmt.Sprintf("event: error\ndata: %v\n\n", err.Error())
_, err = fmt.Fprintf(w, errorMsg)
return false
case <-ctx.Request.Context().Done():
return false
@ -843,14 +841,19 @@ func collectLogs(params dto.StreamLog, messageChan chan<- string, errorChan chan
if params.Container != "" {
cmdArgs = append(cmdArgs, params.Container)
}
cmd := exec.Command("docker", cmdArgs...)
dockerCmd := exec.Command("docker", cmdArgs...)
stdout, err := cmd.StdoutPipe()
stdout, err := dockerCmd.StdoutPipe()
if err != nil {
errorChan <- fmt.Errorf("failed to get stdout pipe: %v", err)
return
}
if err := cmd.Start(); err != nil {
stderr, err := dockerCmd.StderrPipe()
if err != nil {
errorChan <- fmt.Errorf("failed to get stderr pipe: %v", err)
return
}
if err = dockerCmd.Start(); err != nil {
errorChan <- fmt.Errorf("failed to start command: %v", err)
return
}
@ -869,11 +872,20 @@ func collectLogs(params dto.StreamLog, messageChan chan<- string, errorChan chan
}
}
if err := scanner.Err(); err != nil {
if err = scanner.Err(); err != nil {
errorChan <- fmt.Errorf("scanner error: %v", err)
return
}
cmd.Wait()
errScanner := bufio.NewScanner(stderr)
for errScanner.Scan() {
line := errScanner.Text()
errorChan <- fmt.Errorf("%v", line)
}
if err = dockerCmd.Wait(); err != nil {
errorChan <- fmt.Errorf("%v", err)
return
}
}
func (u *ContainerService) DownloadContainerLogs(containerType, container, since, tail string, c *gin.Context) error {

View File

@ -139,6 +139,10 @@ const searchLogs = async () => {
}
});
};
eventSource.onerror = (event: MessageEvent) => {
stopListening();
MsgError(event.data);
};
};
const onDownload = async () => {

View File

@ -30,13 +30,12 @@ const nginxRules: TokenRule[] = [
},
{
type: 'path',
pattern:
/(?:(?<=GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)\s+|(?<=open\(\s*")|(?<="\s*))(\/[^"\s]+(?:\.\w+)?(?:\?\w+=\w+)?)/g,
pattern: /(?<=[\s"])\/[^"\s]+(?:\.\w+)?(?:\?\w+=\w+)?/g,
color: '#B87A2B',
},
{
type: 'http-method',
pattern: /(?<=")(?:GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)(?=\s)/g,
pattern: /(?<=)(?:GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS)(?=\s)/g,
color: '#27AE60',
},
{