mirror of
https://gitee.com/johng/gf
synced 2026-07-07 06:15:15 +08:00
shutdown server gracefully when reveiving TERM signal
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
// Copyright GoFrame Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -233,8 +233,13 @@ func shutdownWebServers(signal ...string) {
|
||||
}
|
||||
}
|
||||
|
||||
// gracefulShutdownWebServers gracefully shuts down all servers.
|
||||
func gracefulShutdownWebServers() {
|
||||
// shutdownWebServersGracefully gracefully shuts down all servers.
|
||||
func shutdownWebServersGracefully(signal ...string) {
|
||||
if len(signal) > 0 {
|
||||
glog.Printf("%d: server gracefully shutting down by signal: %s", gproc.Pid(), signal[0])
|
||||
} else {
|
||||
glog.Printf("%d: server gracefully shutting down by api", gproc.Pid())
|
||||
}
|
||||
serverMapping.RLockFunc(func(m map[string]interface{}) {
|
||||
for _, v := range m {
|
||||
for _, s := range v.(*Server).servers {
|
||||
@ -262,7 +267,7 @@ func handleProcessMessage() {
|
||||
if msg := gproc.Receive(adminGProcCommGroup); msg != nil {
|
||||
if bytes.EqualFold(msg.Data, []byte("exit")) {
|
||||
intlog.Printf("%d: process message: exit", gproc.Pid())
|
||||
gracefulShutdownWebServers()
|
||||
shutdownWebServersGracefully()
|
||||
allDoneChan <- struct{}{}
|
||||
intlog.Printf("%d: process message: exit done", gproc.Pid())
|
||||
return
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
||||
// Copyright 2017 gf Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
@ -35,14 +35,22 @@ func handleProcessSignal() {
|
||||
sig = <-procSignalChan
|
||||
intlog.Printf(`signal received: %s`, sig.String())
|
||||
switch sig {
|
||||
// Stop the servers.
|
||||
case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGABRT:
|
||||
// Shutdown the servers.
|
||||
case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGABRT:
|
||||
shutdownWebServers(sig.String())
|
||||
return
|
||||
|
||||
// Shutdown the servers gracefully.
|
||||
// Especially from K8S when running server in POD.
|
||||
case syscall.SIGTERM:
|
||||
shutdownWebServersGracefully(sig.String())
|
||||
return
|
||||
|
||||
// Restart the servers.
|
||||
case syscall.SIGUSR1:
|
||||
restartWebServers(sig.String())
|
||||
if err := restartWebServers(sig.String()); err != nil {
|
||||
intlog.Error(err)
|
||||
}
|
||||
return
|
||||
|
||||
default:
|
||||
|
||||
Reference in New Issue
Block a user