自动在空闲时间执行定时任务

This commit is contained in:
刘祥超
2024-04-17 13:10:55 +08:00
parent 234887cc1d
commit 7220c53ced
13 changed files with 218 additions and 49 deletions

View File

@@ -5,6 +5,7 @@ package fsutils
import (
"encoding/json"
teaconst "github.com/TeaOSLab/EdgeNode/internal/const"
"github.com/TeaOSLab/EdgeNode/internal/goman"
"github.com/iwind/TeaGo/Tea"
"github.com/shirou/gopsutil/v3/load"
"os"
@@ -55,7 +56,7 @@ func init() {
}
// test disk
go func() {
goman.New(func() {
// load last result from local disk
cacheData, cacheErr := os.ReadFile(Tea.Root + "/data/" + diskSpeedDataFile)
if cacheErr == nil {
@@ -83,17 +84,17 @@ func init() {
}
}
}
}()
})
// check high load
go func() {
goman.New(func() {
var ticker = time.NewTicker(5 * time.Second)
for range ticker.C {
stat, _ := load.Avg()
IsInExtremelyHighLoad = stat != nil && stat.Load1 > extremelyHighLoad1Threshold
IsInHighLoad = stat != nil && stat.Load1 > highLoad1Threshold && !DiskIsFast()
}
}()
})
}
func DiskIsFast() bool {
@@ -145,8 +146,12 @@ func calculateDiskMaxWrites() {
func WaitLoad(maxLoad float64, maxLoops int, delay time.Duration) {
for i := 0; i < maxLoops; i++ {
stat, err := load.Avg()
if err == nil && stat.Load1 > maxLoad {
time.Sleep(delay)
if err == nil {
if stat.Load1 > maxLoad {
time.Sleep(delay)
} else {
return
}
}
}
}