优雅退出

This commit is contained in:
gotoeasy 2022-06-28 11:28:53 +08:00
parent 829143b52f
commit fea93a4607
4 changed files with 58 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import (
"glc/ldb/conf"
"glc/ldb/sysmnt"
"glc/ldb/tokenizer"
"glc/onexit"
"log"
"sync"
"time"
@ -37,6 +38,7 @@ var mapLogDataStorage map[string](*LogDataStorage)
func init() {
mapLogDataStorage = make(map[string](*LogDataStorage))
onexit.RegisterExitHandle(onExit4LogDataStorage) // 优雅退出
}
func getCacheStore(cacheName string) *LogDataStorage {
@ -285,3 +287,10 @@ func (s *LogDataStorage) StoreName() string {
func (s *LogDataStorage) IsClose() bool {
return s.closing
}
func onExit4LogDataStorage() {
for k := range mapLogDataStorage {
mapLogDataStorage[k].Close()
}
log.Println("退出LogDataStorage")
}

View File

@ -8,6 +8,7 @@ package storage
import (
"glc/cmn"
"glc/ldb/conf"
"glc/onexit"
"log"
"math"
"sync"
@ -33,6 +34,7 @@ var mapWordIndexStorage map[string](*WordIndexStorage)
func init() {
mapWordIndexStorage = make(map[string](*WordIndexStorage))
onexit.RegisterExitHandle(onExit4WordIndexStorage) // 优雅退出
}
func getWidxStorage(cacheName string) *WordIndexStorage {
@ -178,3 +180,10 @@ func (s *WordIndexStorage) StoreName() string {
func (s *WordIndexStorage) IsClose() bool {
return s.closing
}
func onExit4WordIndexStorage() {
for k := range mapLogDataStorage {
mapLogDataStorage[k].Close()
}
log.Println("退出WordIndexStorage")
}

View File

@ -9,6 +9,7 @@ import (
"errors"
"glc/cmn"
"glc/ldb/conf"
"glc/onexit"
"log"
"sync"
"time"
@ -28,6 +29,10 @@ type SysmntStorage struct {
var sdbMu sync.Mutex // 锁
var sysmntStorage *SysmntStorage // 缓存用存储器
func init() {
onexit.RegisterExitHandle(onExit) // 优雅退出
}
// 获取存储对象,线程安全(带缓存无则创建有则直取)
func GetSysmntStorage(storeName string) *SysmntStorage { // 存储器,文档,自定义对象
@ -127,3 +132,10 @@ func (s *SysmntStorage) StoreName() string {
func (s *SysmntStorage) IsClose() bool {
return s.closing
}
func onExit() {
if sysmntStorage != nil {
sysmntStorage.Close()
}
log.Println("退出SysmntStorage")
}

28
glc/onexit/onexit.go Normal file
View File

@ -0,0 +1,28 @@
package onexit
import (
"log"
"os"
"os/signal"
"syscall"
)
var fnExits []func()
func init() {
go func() {
osc := make(chan os.Signal, 1)
signal.Notify(osc, syscall.SIGTERM, syscall.SIGINT)
<-osc
log.Println("收到退出信号,准备退出")
for _, fnExit := range fnExits {
fnExit()
}
log.Println("已完成全部退出处理")
}()
}
func RegisterExitHandle(fnExit func()) {
fnExits = append(fnExits, fnExit)
}