mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
improve graceful reload feature for ghttp.Server
This commit is contained in:
@ -25,6 +25,7 @@ var (
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Debugging configured.
|
||||
if !cmdenv.Get("GF_DEBUG").IsEmpty() {
|
||||
isGFDebug = true
|
||||
return
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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(`
|
||||
<html>
|
||||
@ -31,7 +32,8 @@ func (p *utilAdmin) Index(r *Request) {
|
||||
<title>GoFrame Web Server Admin</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>PID: {{.pid}}</p>
|
||||
<p>Pid: {{.pid}}</p>
|
||||
<p>File Path: {{.path}}</p>
|
||||
<p><a href="{{$.uri}}/restart">Restart</a></p>
|
||||
<p><a href="{{$.uri}}/shutdown">Shutdown</a></p>
|
||||
</body>
|
||||
@ -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)
|
||||
|
||||
@ -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 <newExeFilePath> 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
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user