fix: ensure restart api return ok

This commit is contained in:
wwqgtxx 2023-03-14 22:38:42 +08:00
parent 09c53e7cb7
commit 68d7a6da7f

View File

@ -21,7 +21,7 @@ func restartRouter() http.Handler {
} }
func restart(w http.ResponseWriter, r *http.Request) { func restart(w http.ResponseWriter, r *http.Request) {
// modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L180 // modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L108
execPath, err := os.Executable() execPath, err := os.Executable()
if err != nil { if err != nil {
render.Status(r, http.StatusInternalServerError) render.Status(r, http.StatusInternalServerError)
@ -29,25 +29,34 @@ func restart(w http.ResponseWriter, r *http.Request) {
return return
} }
if runtime.GOOS == "windows" { render.JSON(w, r, render.M{"status": "ok"})
cmd := exec.Command(execPath, os.Args[1:]...) if f, ok := w.(http.Flusher); ok {
log.Infoln("restarting: %q %q", execPath, os.Args[1:]) f.Flush()
cmd.Stdin = os.Stdin }
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr // modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/home/controlupdate.go#L180
err = cmd.Start() // The background context is used because the underlying functions wrap it
if err != nil { // with timeout and shut down the server, which handles current request. It
log.Fatalln("restarting:: %s", err) // also should be done in a separate goroutine for the same reason.
go func() {
if runtime.GOOS == "windows" {
cmd := exec.Command(execPath, os.Args[1:]...)
log.Infoln("restarting: %q %q", execPath, os.Args[1:])
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
log.Fatalln("restarting:: %s", err)
}
os.Exit(0)
} }
os.Exit(0) log.Infoln("restarting: %q %q", execPath, os.Args[1:])
} err = syscall.Exec(execPath, os.Args, os.Environ())
if err != nil {
log.Infoln("restarting: %q %q", execPath, os.Args[1:]) log.Fatalln("restarting: %s", err)
err = syscall.Exec(execPath, os.Args, os.Environ()) }
if err != nil { }()
log.Fatalln("restarting: %s", err)
}
render.JSON(w, r, render.M{"status": "ok"})
} }