gf/net/ghttp/ghttp_server_admin_unix.go

66 lines
1.4 KiB
Go
Raw Normal View History

// 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,
// You can obtain one at https://github.com/gogf/gf.
2021-11-13 23:23:55 +08:00
//go:build !windows
// +build !windows
package ghttp
import (
"context"
2019-06-19 09:06:52 +08:00
"os"
"os/signal"
2019-06-19 09:06:52 +08:00
"syscall"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/internal/intlog"
)
2022-03-19 17:58:21 +08:00
// procSignalChan is the channel for listening to the signal.
var procSignalChan = make(chan os.Signal)
2022-03-19 17:58:21 +08:00
// handleProcessSignal handles all signals from system.
func handleProcessSignal() {
2021-09-27 21:27:24 +08:00
var (
ctx = context.TODO()
sig os.Signal
)
signal.Notify(
procSignalChan,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGKILL,
syscall.SIGTERM,
syscall.SIGABRT,
syscall.SIGUSR1,
syscall.SIGUSR2,
)
for {
sig = <-procSignalChan
2021-09-27 21:27:24 +08:00
intlog.Printf(ctx, `signal received: %s`, sig.String())
switch sig {
// Shutdown the servers.
case syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL, syscall.SIGABRT:
2021-09-27 21:27:24 +08:00
shutdownWebServers(ctx, sig.String())
return
// Shutdown the servers gracefully.
// Especially from K8S when running server in POD.
case syscall.SIGTERM:
2021-09-27 21:27:24 +08:00
shutdownWebServersGracefully(ctx, sig.String())
return
2020-05-01 03:31:04 +08:00
// Restart the servers.
case syscall.SIGUSR1:
2021-09-27 21:27:24 +08:00
if err := restartWebServers(ctx, sig.String()); err != nil {
intlog.Errorf(ctx, `%+v`, err)
}
return
default:
2019-06-19 09:06:52 +08:00
}
}
2019-06-19 09:06:52 +08:00
}