Commit 4138fe02 authored by astaxie's avatar astaxie

beego suppot graceful restart application

parent 245762f7
......@@ -19,9 +19,11 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"text/template"
"time"
"github.com/astaxie/beego/grace"
"github.com/astaxie/beego/toolbox"
"github.com/astaxie/beego/utils"
)
......@@ -458,8 +460,14 @@ func (admin *adminApp) Run() {
http.Handle(p, f)
}
BeeLogger.Info("Admin server Running on %s", addr)
err := http.ListenAndServe(addr, nil)
var err error
if Graceful {
err = grace.ListenAndServe(addr, nil)
} else {
err = http.ListenAndServe(addr, nil)
}
if err != nil {
BeeLogger.Critical("Admin ListenAndServe: ", err)
BeeLogger.Critical("Admin ListenAndServe: ", err, fmt.Sprint(os.Getpid()))
}
}
......@@ -22,6 +22,7 @@ import (
"os"
"time"
"github.com/astaxie/beego/grace"
"github.com/astaxie/beego/utils"
)
......@@ -76,57 +77,93 @@ func (app *App) Run() {
err = fcgi.Serve(l, app.Handlers)
}
} else {
app.Server.Addr = addr
app.Server.Handler = app.Handlers
app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
if EnableHttpTLS {
go func() {
time.Sleep(20 * time.Microsecond)
if HttpsPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
}
BeeLogger.Info("https server Running on %s", app.Server.Addr)
err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
if err != nil {
BeeLogger.Critical("ListenAndServeTLS: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}()
}
if EnableHttpListen {
go func() {
app.Server.Addr = addr
BeeLogger.Info("http server Running on %s", app.Server.Addr)
if ListenTCP4 && HttpAddr == "" {
ln, err := net.Listen("tcp4", app.Server.Addr)
if Graceful {
if EnableHttpTLS {
go func() {
time.Sleep(20 * time.Microsecond)
if HttpsPort != 0 {
addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
}
server := grace.NewServer(addr, app.Handlers)
server.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
server.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
err := server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
BeeLogger.Critical("ListenAndServeTLS: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
err = app.Server.Serve(ln)
}()
}
if EnableHttpListen {
go func() {
server := grace.NewServer(addr, app.Handlers)
server.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
server.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
if ListenTCP4 && HttpAddr == "" {
server.Network = "tcp4"
}
err := server.ListenAndServe()
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
BeeLogger.Critical("ListenAndServe: ", err, fmt.Sprint(os.Getpid()))
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
} else {
err := app.Server.ListenAndServe()
}()
}
} else {
app.Server.Addr = addr
app.Server.Handler = app.Handlers
app.Server.ReadTimeout = time.Duration(HttpServerTimeOut) * time.Second
app.Server.WriteTimeout = time.Duration(HttpServerTimeOut) * time.Second
if EnableHttpTLS {
go func() {
time.Sleep(20 * time.Microsecond)
if HttpsPort != 0 {
app.Server.Addr = fmt.Sprintf("%s:%d", HttpAddr, HttpsPort)
}
BeeLogger.Info("https server Running on %s", app.Server.Addr)
err := app.Server.ListenAndServeTLS(HttpCertFile, HttpKeyFile)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
BeeLogger.Critical("ListenAndServeTLS: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}
}()
}()
}
if EnableHttpListen {
go func() {
app.Server.Addr = addr
BeeLogger.Info("http server Running on %s", app.Server.Addr)
if ListenTCP4 && HttpAddr == "" {
ln, err := net.Listen("tcp4", app.Server.Addr)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
err = app.Server.Serve(ln)
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
return
}
} else {
err := app.Server.ListenAndServe()
if err != nil {
BeeLogger.Critical("ListenAndServe: ", err)
time.Sleep(100 * time.Microsecond)
endRunning <- true
}
}
}()
}
}
}
}
<-endRunning
}
......@@ -82,6 +82,7 @@ var (
EnableDocs bool // enable generate docs & server docs API Swagger
RouterCaseSensitive bool // router case sensitive default is true
AccessLogs bool // print access logs, default is false
Graceful bool // use graceful start the server
)
type beegoAppConfig struct {
......@@ -509,5 +510,8 @@ func ParseConfig() (err error) {
if casesensitive, err := AppConfig.Bool("RouterCaseSensitive"); err == nil {
RouterCaseSensitive = casesensitive
}
if graceful, err := AppConfig.Bool("Graceful"); err == nil {
Graceful = graceful
}
return nil
}
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment