mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
fix(docker): nginx restart always output network error
This commit is contained in:
parent
070c0b4620
commit
45a68112b1
5 changed files with 113 additions and 94 deletions
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func Reload(c *gin.Context) {
|
||||
|
@ -23,9 +24,24 @@ func Test(c *gin.Context) {
|
|||
}
|
||||
|
||||
func Restart(c *gin.Context) {
|
||||
output := nginx.Restart()
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": output,
|
||||
"level": nginx.GetLogLevel(output),
|
||||
"message": "ok",
|
||||
})
|
||||
go nginx.Restart()
|
||||
}
|
||||
|
||||
func Status(c *gin.Context) {
|
||||
pidPath := nginx.GetPIDPath()
|
||||
lastOutput := nginx.GetLastOutput()
|
||||
|
||||
running := true
|
||||
if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
|
||||
running = false
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"running": running,
|
||||
"message": lastOutput,
|
||||
"level": nginx.GetLogLevel(lastOutput),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@ import (
|
|||
"github.com/0xJacky/Nginx-UI/internal/nginx"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func BuildNginxConfig(c *gin.Context) {
|
||||
|
@ -58,16 +57,3 @@ func FormatNginxConfig(c *gin.Context) {
|
|||
"content": content,
|
||||
})
|
||||
}
|
||||
|
||||
func Status(c *gin.Context) {
|
||||
pidPath := nginx.GetPIDPath()
|
||||
|
||||
running := true
|
||||
if fileInfo, err := os.Stat(pidPath); err != nil || fileInfo.Size() == 0 { // fileInfo.Size() == 0 no process id
|
||||
running = false
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"running": running,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -46,7 +46,7 @@ const ngx = {
|
|||
return http.post('/ngx/format_code', { content })
|
||||
},
|
||||
|
||||
status(): Promise<{ running: boolean }> {
|
||||
status(): Promise<{ running: boolean; message: string; level: number }> {
|
||||
return http.get('/nginx/status')
|
||||
},
|
||||
|
||||
|
|
|
@ -6,13 +6,14 @@ import { logLevel } from '@/views/config/constants'
|
|||
import { NginxStatus } from '@/constants'
|
||||
|
||||
const status = ref(0)
|
||||
function get_status() {
|
||||
ngx.status().then(r => {
|
||||
async function get_status() {
|
||||
const r = await ngx.status()
|
||||
if (r?.running === true)
|
||||
status.value = NginxStatus.Running
|
||||
else
|
||||
status.value = NginxStatus.Stopped
|
||||
})
|
||||
|
||||
return r
|
||||
}
|
||||
|
||||
function reload_nginx() {
|
||||
|
@ -29,9 +30,11 @@ function reload_nginx() {
|
|||
}).finally(() => get_status())
|
||||
}
|
||||
|
||||
function restart_nginx() {
|
||||
async function restart_nginx() {
|
||||
status.value = NginxStatus.Restarting
|
||||
ngx.restart().then(r => {
|
||||
await ngx.restart()
|
||||
|
||||
get_status().then(r => {
|
||||
if (r.level < logLevel.Warn)
|
||||
message.success($gettext('Nginx restarted successfully'))
|
||||
else if (r.level === logLevel.Warn)
|
||||
|
@ -40,7 +43,7 @@ function restart_nginx() {
|
|||
message.error(r.message)
|
||||
}).catch(e => {
|
||||
message.error(`${$gettext('Server error')} ${e?.message}`)
|
||||
}).finally(() => get_status())
|
||||
})
|
||||
}
|
||||
|
||||
const visible = ref(false)
|
||||
|
|
|
@ -4,27 +4,13 @@ import (
|
|||
"github.com/0xJacky/Nginx-UI/settings"
|
||||
"os/exec"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var mutex sync.Mutex
|
||||
|
||||
func execShell(cmd string) (out string) {
|
||||
bytes, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
|
||||
out = string(bytes)
|
||||
if err != nil {
|
||||
out += " " + err.Error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func execCommand(name string, cmd ...string) (out string) {
|
||||
bytes, err := exec.Command(name, cmd...).CombinedOutput()
|
||||
out = string(bytes)
|
||||
if err != nil {
|
||||
out += " " + err.Error()
|
||||
}
|
||||
return
|
||||
}
|
||||
var (
|
||||
mutex sync.Mutex
|
||||
lastOutput string
|
||||
)
|
||||
|
||||
func TestConf() (out string) {
|
||||
mutex.Lock()
|
||||
|
@ -53,11 +39,15 @@ func Reload() (out string) {
|
|||
return
|
||||
}
|
||||
|
||||
func Restart() (out string) {
|
||||
func Restart() {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
// fix(docker): nginx restart always output network error
|
||||
time.Sleep(500 * time.Millisecond)
|
||||
|
||||
if settings.NginxSettings.RestartCmd != "" {
|
||||
out = execShell(settings.NginxSettings.RestartCmd)
|
||||
lastOutput = execShell(settings.NginxSettings.RestartCmd)
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -65,15 +55,39 @@ func Restart() (out string) {
|
|||
pidPath := GetPIDPath()
|
||||
daemon := GetSbinPath()
|
||||
|
||||
out = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
|
||||
lastOutput = execCommand("start-stop-daemon", "--stop", "--quiet", "--oknodo", "--retry=TERM/30/KILL/5", "--pidfile", pidPath)
|
||||
|
||||
if daemon == "" {
|
||||
out += execCommand("nginx")
|
||||
lastOutput += execCommand("nginx")
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
out += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
|
||||
lastOutput += execCommand("start-stop-daemon", "--start", "--quiet", "--pidfile", pidPath, "--exec", daemon)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func GetLastOutput() string {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
return lastOutput
|
||||
}
|
||||
|
||||
func execShell(cmd string) (out string) {
|
||||
bytes, err := exec.Command("/bin/sh", "-c", cmd).CombinedOutput()
|
||||
out = string(bytes)
|
||||
if err != nil {
|
||||
out += " " + err.Error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func execCommand(name string, cmd ...string) (out string) {
|
||||
bytes, err := exec.Command(name, cmd...).CombinedOutput()
|
||||
out = string(bytes)
|
||||
if err != nil {
|
||||
out += " " + err.Error()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue