mirror of
https://github.com/gotoeasy/glogcenter.git
synced 2025-09-15 12:58:34 +08:00
优雅退出
This commit is contained in:
parent
f17a0ad45f
commit
51baefdc84
@ -1,18 +1,22 @@
|
||||
package gweb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"glc/ldb/conf"
|
||||
"glc/onexit"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func Run() {
|
||||
server := gin.Default()
|
||||
ginEngine := gin.Default()
|
||||
|
||||
server.NoRoute(func(c *gin.Context) {
|
||||
ginEngine.NoRoute(func(c *gin.Context) {
|
||||
req := NewHttpRequest(c)
|
||||
|
||||
// filter
|
||||
@ -40,6 +44,25 @@ func Run() {
|
||||
}
|
||||
})
|
||||
|
||||
server.Run(fmt.Sprintf(":%d", conf.GetServerPort()))
|
||||
httpServer := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", conf.GetServerPort()), // :8080
|
||||
Handler: ginEngine,
|
||||
}
|
||||
|
||||
// 优雅退出
|
||||
onexit.RegisterExitHandle(func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
if err := httpServer.Shutdown(ctx); err != nil {
|
||||
log.Println("退出Web服务:", err)
|
||||
} else {
|
||||
log.Println("退出Web服务")
|
||||
}
|
||||
})
|
||||
|
||||
// 启动Web服务
|
||||
err := httpServer.ListenAndServe()
|
||||
if err != nil && err != http.ErrServerClosed {
|
||||
log.Fatalf("%s", err) // 启动失败的话打印错误信息后退出
|
||||
}
|
||||
}
|
||||
|
||||
@ -245,7 +245,7 @@ func (s *LogDataStorage) Add(model *LogDataModel) error {
|
||||
|
||||
// 关闭Storage
|
||||
func (s *LogDataStorage) Close() {
|
||||
if s.closing {
|
||||
if s == nil || s.closing { // 优雅退出时可能会正好nil,判断一下优雅点
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -158,7 +158,7 @@ func (s *WordIndexStorage) Get(id uint64) uint64 {
|
||||
|
||||
// 关闭Storage
|
||||
func (s *WordIndexStorage) Close() {
|
||||
if s.closing {
|
||||
if s == nil || s.closing { // 优雅退出时可能会正好nil,判断一下优雅点
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -88,7 +88,7 @@ func autoCloseSysmntStorageWhenMaxIdle(store *SysmntStorage) {
|
||||
|
||||
// 关闭Storage
|
||||
func (s *SysmntStorage) Close() {
|
||||
if s.closing {
|
||||
if s == nil || s.closing { // 优雅退出时可能会正好nil,判断一下优雅点
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@ -14,12 +14,10 @@ func init() {
|
||||
osc := make(chan os.Signal, 1)
|
||||
signal.Notify(osc, syscall.SIGTERM, syscall.SIGINT)
|
||||
<-osc
|
||||
log.Println("收到退出信号,准备退出")
|
||||
|
||||
log.Println("收到退出信号准备退出")
|
||||
for _, fnExit := range fnExits {
|
||||
fnExit()
|
||||
}
|
||||
log.Println("已完成全部退出处理")
|
||||
}()
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user