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

This commit is contained in:
刘祥超
2024-04-13 17:14:58 +08:00
parent 17f0821945
commit b8b56db83c
2 changed files with 46 additions and 0 deletions

View File

@@ -35,11 +35,13 @@ import (
"net" "net"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"runtime" "runtime"
"sort" "sort"
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"syscall"
"time" "time"
// grpc decompression // grpc decompression
@@ -92,6 +94,9 @@ func (this *APINode) Start() {
return return
} }
// 监听信号
this.listenSignals()
// 启动IP库 // 启动IP库
this.setProgress("IP_LIBRARY", "开始初始化IP库") this.setProgress("IP_LIBRARY", "开始初始化IP库")
remotelogs.Println("API_NODE", "initializing ip library ...") 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 ( import (
"context" "context"
"encoding/json"
"errors" "errors"
teaconst "github.com/TeaOSLab/EdgeAPI/internal/const"
"github.com/TeaOSLab/EdgeAPI/internal/db/models" "github.com/TeaOSLab/EdgeAPI/internal/db/models"
"github.com/TeaOSLab/EdgeAPI/internal/events"
"github.com/TeaOSLab/EdgeAPI/internal/goman" "github.com/TeaOSLab/EdgeAPI/internal/goman"
"github.com/TeaOSLab/EdgeAPI/internal/remotelogs" "github.com/TeaOSLab/EdgeAPI/internal/remotelogs"
"github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils" "github.com/TeaOSLab/EdgeAPI/internal/utils/regexputils"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb" "github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs" "github.com/TeaOSLab/EdgeCommon/pkg/systemconfigs"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/dbs" "github.com/iwind/TeaGo/dbs"
"github.com/iwind/TeaGo/types" "github.com/iwind/TeaGo/types"
timeutil "github.com/iwind/TeaGo/utils/time" timeutil "github.com/iwind/TeaGo/utils/time"
"os"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -23,6 +28,29 @@ var serverBandwidthStatsMap = map[string]*pb.ServerBandwidthStat{} // server key
var serverBandwidthStatsLocker = &sync.Mutex{} var serverBandwidthStatsLocker = &sync.Mutex{}
func init() { 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 ticker = time.NewTicker(1 * time.Minute)
var useTx = true var useTx = true