From 04dee090a31fbd5b7a05134ea948149bc2ec1965 Mon Sep 17 00:00:00 2001 From: john Date: Sat, 25 Jul 2020 13:50:04 +0800 Subject: [PATCH] improve graceful reload feature for ghttp.Server --- internal/intlog/intlog.go | 1 + net/ghttp/ghttp_server.go | 3 +++ net/ghttp/ghttp_server_admin.go | 12 +++++++----- net/ghttp/ghttp_server_admin_process.go | 8 +++++++- net/ghttp/ghttp_server_config.go | 2 +- 5 files changed, 19 insertions(+), 7 deletions(-) diff --git a/internal/intlog/intlog.go b/internal/intlog/intlog.go index de62a0978..24d05d539 100644 --- a/internal/intlog/intlog.go +++ b/internal/intlog/intlog.go @@ -25,6 +25,7 @@ var ( ) func init() { + // Debugging configured. if !cmdenv.Get("GF_DEBUG").IsEmpty() { isGFDebug = true return diff --git a/net/ghttp/ghttp_server.go b/net/ghttp/ghttp_server.go index 3d7a6cdb1..c620b2ec8 100644 --- a/net/ghttp/ghttp_server.go +++ b/net/ghttp/ghttp_server.go @@ -210,7 +210,10 @@ func serverProcessInit() { // Process message handler. // It's enabled only graceful feature is enabled. if gracefulEnabled { + intlog.Printf("%d: graceful reload feature is enabled", gproc.Pid()) go handleProcessMessage() + } else { + intlog.Printf("%d: graceful reload feature is disabled", gproc.Pid()) } // It's an ugly calling for better initializing the main package path diff --git a/net/ghttp/ghttp_server_admin.go b/net/ghttp/ghttp_server_admin.go index 5f79692c2..474094a77 100644 --- a/net/ghttp/ghttp_server_admin.go +++ b/net/ghttp/ghttp_server_admin.go @@ -7,7 +7,7 @@ package ghttp import ( - "os" + "github.com/gogf/gf/os/gfile" "strings" "time" @@ -22,8 +22,9 @@ type utilAdmin struct{} // Index shows the administration page. func (p *utilAdmin) Index(r *Request) { data := map[string]interface{}{ - "pid": gproc.Pid(), - "uri": strings.TrimRight(r.URL.Path, "/"), + "pid": gproc.Pid(), + "path": gfile.SelfPath(), + "uri": strings.TrimRight(r.URL.Path, "/"), } buffer, _ := gview.ParseContent(` @@ -31,7 +32,8 @@ func (p *utilAdmin) Index(r *Request) { GoFrame Web Server Admin -

PID: {{.pid}}

+

Pid: {{.pid}}

+

File Path: {{.path}}

Restart

Shutdown

@@ -46,7 +48,7 @@ func (p *utilAdmin) Restart(r *Request) { // Custom start binary path when this process exits. path := r.GetQueryString("newExeFilePath") if path == "" { - path = os.Args[0] + path = gfile.SelfPath() } if len(path) > 0 { err = RestartAllServer(path) diff --git a/net/ghttp/ghttp_server_admin_process.go b/net/ghttp/ghttp_server_admin_process.go index f88212278..ad4032c0f 100644 --- a/net/ghttp/ghttp_server_admin_process.go +++ b/net/ghttp/ghttp_server_admin_process.go @@ -10,6 +10,7 @@ import ( "bytes" "errors" "fmt" + "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/text/gstr" "os" "runtime" @@ -49,6 +50,9 @@ var serverProcessStatus = gtype.NewInt() // RestartAllServer restarts all the servers of the process. // The optional parameter specifies the new binary file for creating process. func RestartAllServer(newExeFilePath ...string) error { + if !gracefulEnabled { + return errors.New("graceful reload feature is disabled") + } serverActionLocker.Lock() defer serverActionLocker.Unlock() if err := checkProcessStatus(); err != nil { @@ -147,7 +151,7 @@ func forkRestartProcess(newExeFilePath ...string) error { env = append(env, gADMIN_ACTION_RESTART_ENVKEY+"=1") p := gproc.NewProcess(path, os.Args, env) if _, err := p.Start(); err != nil { - glog.Errorf("%d: fork process failed, error:%s", gproc.Pid(), err.Error()) + glog.Errorf(`%d: fork process failed, error:%s, are you running using "go run"?`, gproc.Pid(), err.Error()) return err } return nil @@ -257,8 +261,10 @@ func handleProcessMessage() { for { if msg := gproc.Receive(gADMIN_GPROC_COMM_GROUP); msg != nil { if bytes.EqualFold(msg.Data, []byte("exit")) { + intlog.Printf("%d: process message: exit", gproc.Pid()) gracefulShutdownWebServers() allDoneChan <- struct{}{} + intlog.Printf("%d: process message: exit done", gproc.Pid()) return } } diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 4adb34210..b4983af9d 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -267,7 +267,7 @@ func Config() ServerConfig { ClientMaxBodySize: 8 * 1024 * 1024, // 8MB FormParsingMemory: 1024 * 1024, // 1MB Rewrites: make(map[string]string), - Graceful: true, + Graceful: false, } }