2021-01-22 00:36:28 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2018-05-13 00:17:12 +08:00
|
|
|
//
|
|
|
|
// 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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2018-05-13 00:17:12 +08:00
|
|
|
|
|
|
|
// +build !windows
|
|
|
|
|
|
|
|
package ghttp
|
|
|
|
|
|
|
|
import (
|
2021-06-26 16:23:54 +08:00
|
|
|
"context"
|
2020-04-13 23:44:43 +08:00
|
|
|
"github.com/gogf/gf/internal/intlog"
|
2019-06-19 09:06:52 +08:00
|
|
|
"os"
|
2021-01-22 00:36:28 +08:00
|
|
|
"os/signal"
|
2019-06-19 09:06:52 +08:00
|
|
|
"syscall"
|
2018-05-13 00:17:12 +08:00
|
|
|
)
|
|
|
|
|
2021-01-22 00:36:28 +08:00
|
|
|
// procSignalChan is the channel for listening the signal.
|
|
|
|
var procSignalChan = make(chan os.Signal)
|
|
|
|
|
|
|
|
// handleProcessSignal handles all signal from system.
|
|
|
|
func handleProcessSignal() {
|
|
|
|
var sig os.Signal
|
|
|
|
signal.Notify(
|
|
|
|
procSignalChan,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
syscall.SIGKILL,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGABRT,
|
|
|
|
syscall.SIGUSR1,
|
|
|
|
syscall.SIGUSR2,
|
|
|
|
)
|
|
|
|
for {
|
|
|
|
sig = <-procSignalChan
|
2021-06-26 16:23:54 +08:00
|
|
|
intlog.Printf(context.TODO(), `signal received: %s`, sig.String())
|
2021-01-22 00:36:28 +08:00
|
|
|
switch sig {
|
|
|
|
// Shutdown the servers.
|
|
|
|
case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGABRT:
|
|
|
|
shutdownWebServers(sig.String())
|
|
|
|
return
|
2018-06-01 00:11:45 +08:00
|
|
|
|
2021-01-08 00:58:58 +08:00
|
|
|
// Shutdown the servers gracefully.
|
|
|
|
// Especially from K8S when running server in POD.
|
2021-01-22 00:36:28 +08:00
|
|
|
case syscall.SIGTERM:
|
|
|
|
shutdownWebServersGracefully(sig.String())
|
|
|
|
return
|
2021-01-08 00:58:58 +08:00
|
|
|
|
2020-05-01 03:31:04 +08:00
|
|
|
// Restart the servers.
|
2021-01-22 00:36:28 +08:00
|
|
|
case syscall.SIGUSR1:
|
|
|
|
if err := restartWebServers(sig.String()); err != nil {
|
2021-06-26 16:23:54 +08:00
|
|
|
intlog.Error(context.TODO(), err)
|
2021-01-22 00:36:28 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
default:
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
2021-01-22 00:36:28 +08:00
|
|
|
}
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|