hotgo/server/internal/cmd/handler_shutdown.go

45 lines
1.1 KiB
Go
Raw Normal View History

2022-11-24 23:37:34 +08:00
// Package cmd
// @Link https://github.com/bufanyun/hotgo
// @Copyright Copyright (c) 2023 HotGo CLI
2022-11-24 23:37:34 +08:00
// @Author Ms <133814250@qq.com>
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
package cmd
import (
"context"
"github.com/gogf/gf/v2/os/gctx"
2022-11-24 23:37:34 +08:00
"github.com/gogf/gf/v2/os/gproc"
"hotgo/internal/consts"
2022-11-25 23:22:44 +08:00
"hotgo/utility/simple"
2022-11-24 23:37:34 +08:00
"os"
"sync"
)
var (
serverCloseSignal = make(chan struct{}, 1)
serverWg = sync.WaitGroup{}
once sync.Once
2022-11-24 23:37:34 +08:00
)
// signalHandlerForOverall 关闭信号处理
2022-11-24 23:37:34 +08:00
func signalHandlerForOverall(sig os.Signal) {
serverCloseEvent(gctx.GetInitCtx())
serverCloseSignal <- struct{}{}
2022-11-24 23:37:34 +08:00
}
// signalListen 信号监听
2022-11-24 23:37:34 +08:00
func signalListen(ctx context.Context, handler ...gproc.SigHandler) {
2022-11-25 23:22:44 +08:00
simple.SafeGo(ctx, func(ctx context.Context) {
2022-11-24 23:37:34 +08:00
gproc.AddSigHandlerShutdown(handler...)
gproc.Listen()
})
}
// serverCloseEvent 关闭事件
// 区别于服务收到退出信号后的处理,只会执行一次
func serverCloseEvent(ctx context.Context) {
once.Do(func() {
simple.Event().Call(consts.EventServerClose, ctx)
})
}