1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 03:01:28 +00:00

fix #1176 grace support windows

This commit is contained in:
astaxie 2015-05-27 23:22:05 +08:00
parent 40974365e6
commit ae37689314

View File

@ -133,19 +133,13 @@ func NewServer(addr string, handler http.Handler) (srv *graceServer) {
SignalHooks: map[int]map[os.Signal][]func(){ SignalHooks: map[int]map[os.Signal][]func(){
PRE_SIGNAL: map[os.Signal][]func(){ PRE_SIGNAL: map[os.Signal][]func(){
syscall.SIGHUP: []func(){}, syscall.SIGHUP: []func(){},
syscall.SIGUSR1: []func(){},
syscall.SIGUSR2: []func(){},
syscall.SIGINT: []func(){}, syscall.SIGINT: []func(){},
syscall.SIGTERM: []func(){}, syscall.SIGTERM: []func(){},
syscall.SIGTSTP: []func(){},
}, },
POST_SIGNAL: map[os.Signal][]func(){ POST_SIGNAL: map[os.Signal][]func(){
syscall.SIGHUP: []func(){}, syscall.SIGHUP: []func(){},
syscall.SIGUSR1: []func(){},
syscall.SIGUSR2: []func(){},
syscall.SIGINT: []func(){}, syscall.SIGINT: []func(){},
syscall.SIGTERM: []func(){}, syscall.SIGTERM: []func(){},
syscall.SIGTSTP: []func(){},
}, },
}, },
state: STATE_INIT, state: STATE_INIT,
@ -208,16 +202,24 @@ func (srv *graceServer) ListenAndServe() (err error) {
l, err := srv.getListener(addr) l, err := srv.getListener(addr)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return err
} }
srv.GraceListener = newGraceListener(l, srv) srv.GraceListener = newGraceListener(l, srv)
if srv.isChild { if srv.isChild {
syscall.Kill(syscall.Getppid(), syscall.SIGTERM) process, err := os.FindProcess(os.Getppid())
if err != nil {
log.Println(err)
return err
}
err = process.Kill()
if err != nil {
return err
}
} }
log.Println(syscall.Getpid(), srv.Addr) log.Println(os.Getpid(), srv.Addr)
return srv.Serve() return srv.Serve()
} }
@ -255,17 +257,24 @@ func (srv *graceServer) ListenAndServeTLS(certFile, keyFile string) (err error)
l, err := srv.getListener(addr) l, err := srv.getListener(addr)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
return return err
} }
srv.tlsInnerListener = newGraceListener(l, srv) srv.tlsInnerListener = newGraceListener(l, srv)
srv.GraceListener = tls.NewListener(srv.tlsInnerListener, config) srv.GraceListener = tls.NewListener(srv.tlsInnerListener, config)
if srv.isChild { if srv.isChild {
syscall.Kill(syscall.Getppid(), syscall.SIGTERM) process, err := os.FindProcess(os.Getppid())
if err != nil {
log.Println(err)
return err
}
err = process.Kill()
if err != nil {
return err
}
} }
log.Println(os.Getpid(), srv.Addr)
log.Println(syscall.Getpid(), srv.Addr)
return srv.Serve() return srv.Serve()
} }
@ -303,11 +312,8 @@ func (srv *graceServer) handleSignals() {
signal.Notify( signal.Notify(
srv.sigChan, srv.sigChan,
syscall.SIGHUP, syscall.SIGHUP,
syscall.SIGUSR1,
syscall.SIGUSR2,
syscall.SIGINT, syscall.SIGINT,
syscall.SIGTERM, syscall.SIGTERM,
syscall.SIGTSTP,
) )
pid := syscall.Getpid() pid := syscall.Getpid()
@ -321,19 +327,12 @@ func (srv *graceServer) handleSignals() {
if err != nil { if err != nil {
log.Println("Fork err:", err) log.Println("Fork err:", err)
} }
case syscall.SIGUSR1:
log.Println(pid, "Received SIGUSR1.")
case syscall.SIGUSR2:
log.Println(pid, "Received SIGUSR2.")
srv.serverTimeout(0 * time.Second)
case syscall.SIGINT: case syscall.SIGINT:
log.Println(pid, "Received SIGINT.") log.Println(pid, "Received SIGINT.")
srv.shutdown() srv.shutdown()
case syscall.SIGTERM: case syscall.SIGTERM:
log.Println(pid, "Received SIGTERM.") log.Println(pid, "Received SIGTERM.")
srv.shutdown() srv.shutdown()
case syscall.SIGTSTP:
log.Println(pid, "Received SIGTSTP.")
default: default:
log.Printf("Received %v: nothing i care about...\n", sig) log.Printf("Received %v: nothing i care about...\n", sig)
} }