Commit 4138fe02 authored by astaxie's avatar astaxie

beego suppot graceful restart application

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