Skip to content

Commit

Permalink
fix: taking 100% CPU if the log file is not a regular file
Browse files Browse the repository at this point in the history
  • Loading branch information
0xJacky committed Jan 26, 2024
1 parent f20d97a commit d70e37c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
23 changes: 18 additions & 5 deletions api/nginx/nginx_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package nginx
import (
"encoding/json"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/helper"
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/0xJacky/Nginx-UI/internal/nginx"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -50,15 +49,21 @@ func GetNginxLogPage(c *gin.Context) {
return
}

f, err := os.Open(logPath)
logFileStat, err := os.Stat(logPath)

if err != nil {
c.JSON(http.StatusOK, nginxLogPageResp{})
logger.Error(err)
return
}

logFileStat, err := os.Stat(logPath)
if !logFileStat.Mode().IsRegular() {
c.JSON(http.StatusOK, nginxLogPageResp{})
logger.Error("log file is not regular file:", logPath)
return
}

f, err := os.Open(logPath)

if err != nil {
c.JSON(http.StatusOK, nginxLogPageResp{})
Expand Down Expand Up @@ -188,8 +193,16 @@ func tailNginxLog(ws *websocket.Conn, controlChan chan controlStruct, errChan ch
Whence: io.SeekEnd,
}

if !helper.FileExists(logPath) {
errChan <- errors.New("error log path not exists " + logPath)
stat, err := os.Stat(logPath)
if os.IsNotExist(err) {
errChan <- errors.New("[error] log path not exists " + logPath)
return
}

if !stat.Mode().IsRegular() {
errChan <- errors.New("[error] " + logPath + " is not a regular file. " +
"If you are using nginx-ui in docker container, please refer to " +
"https://nginxui.com/zh_CN/guide/config-nginx-log.html for more information.")
return
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"2.0.0-beta.11","build_id":109,"total_build":313}
{"version":"2.0.0-beta.11","build_id":110,"total_build":314}
2 changes: 1 addition & 1 deletion app/version.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":"2.0.0-beta.11","build_id":109,"total_build":313}
{"version":"2.0.0-beta.11","build_id":110,"total_build":314}
2 changes: 1 addition & 1 deletion resources/demo/app.ini
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ StartCmd = bash
NodeSecret = fdc7764f-92d2-454c-9640-6a09be121139
Demo = true

[nginx_log]
[nginx]
AccessLogPath = /var/log/nginx/access.local.log
ErrorLogPath = /var/log/nginx/error.local.log

Expand Down

0 comments on commit d70e37c

Please # to comment.