进程重启时,自动保存未保存的带宽统计数据到本地文件,以便于在重启后恢复

This commit is contained in:
GoEdgeLab
2024-04-13 17:14:58 +08:00
parent 06ddd03dd8
commit 2f67f2fa6f
2 changed files with 46 additions and 0 deletions

View File

@@ -35,11 +35,13 @@ import (
"net"
"os"
"os/exec"
"os/signal"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"syscall"
"time"
// grpc decompression
@@ -92,6 +94,9 @@ func (this *APINode) Start() {
return
}
// 监听信号
this.listenSignals()
// 启动IP库
this.setProgress("IP_LIBRARY", "开始初始化IP库")
remotelogs.Println("API_NODE", "initializing ip library ...")
@@ -921,3 +926,16 @@ func (this *APINode) setupTimeZone() {
}
}
}
// 监听一些信号
func (this *APINode) listenSignals() {
var queue = make(chan os.Signal, 8)
signal.Notify(queue, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL, syscall.SIGQUIT)
goman.New(func() {
for range queue {
events.Notify(events.EventQuit)
os.Exit(0)
return
}
})
}

View File

@@ -4,16 +4,21 @@ package services
import (
"context"
"encoding/json"
"errors"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/events"
"github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time"
"os"
"strings"
"sync"
"time"
@@ -23,6 +28,29 @@ var serverBandwidthStatsMap = map[string]*pb.ServerBandwidthStat{} // server key
var serverBandwidthStatsLocker = &sync.Mutex{}
func init() {
// 数据缓存
if teaconst.IsMain {
var cacheFile = Tea.Root + "/data/server_bandwidth_stats.cache"
{
data, err := os.ReadFile(cacheFile)
if err == nil {
_ = os.Remove(cacheFile)
serverBandwidthStatsLocker.Lock()
_ = json.Unmarshal(data, &serverBandwidthStatsMap)
serverBandwidthStatsLocker.Unlock()
}
}
events.On(events.EventQuit, func() {
serverBandwidthStatsMapJSON, err := json.Marshal(serverBandwidthStatsMap)
if err == nil {
_ = os.WriteFile(cacheFile, serverBandwidthStatsMapJSON, 0666)
}
})
}
// 定时处理数据
var ticker = time.NewTicker(1 * time.Minute)
var useTx = true