improve graceful reload feature for ghttp.Server

This commit is contained in:
john
2020-07-25 13:50:04 +08:00
parent 245c6d24a1
commit 04dee090a3
5 changed files with 19 additions and 7 deletions

View File

@ -25,6 +25,7 @@ var (
)
func init() {
// Debugging configured.
if !cmdenv.Get("GF_DEBUG").IsEmpty() {
isGFDebug = true
return

View File

@ -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

View File

@ -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)

View File

@ -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
}
}

View File

@ -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,
}
}