mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-03 15:00:26 +08:00
执行edge-node cache.badge命令时打印进度
This commit is contained in:
@@ -475,6 +475,19 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
var progressSock = gosock.NewTmpSock(teaconst.CacheGarbageSockName)
|
||||
progressSock.OnCommand(func(cmd *gosock.Command) {
|
||||
var params = maps.NewMap(cmd.Params)
|
||||
if cmd.Code == "progress" {
|
||||
fmt.Printf("%.2f%% %d\n", params.GetFloat64("progress")*100, params.GetInt("count"))
|
||||
_ = cmd.ReplyOk()
|
||||
}
|
||||
})
|
||||
go func() {
|
||||
_ = progressSock.Listen()
|
||||
}()
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
var sock = gosock.NewTmpSock(teaconst.ProcessName)
|
||||
reply, err := sock.Send(&gosock.Command{
|
||||
Code: "cache.garbage",
|
||||
|
||||
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/iwind/TeaGo/types"
|
||||
stringutil "github.com/iwind/TeaGo/utils/string"
|
||||
timeutil "github.com/iwind/TeaGo/utils/time"
|
||||
"github.com/iwind/gosock/pkg/gosock"
|
||||
"github.com/shirou/gopsutil/v3/load"
|
||||
"math"
|
||||
"os"
|
||||
@@ -1562,6 +1563,15 @@ func (this *FileStorage) ScanGarbageCaches(fileCallback func(path string) error)
|
||||
allDirs = append(allDirs, subDir.Path)
|
||||
}
|
||||
|
||||
var countDirs = 0
|
||||
|
||||
// process progress
|
||||
var progressSock = gosock.NewTmpSock(teaconst.CacheGarbageSockName)
|
||||
_, sockErr := progressSock.SendTimeout(&gosock.Command{Code: "progress", Params: map[string]any{"progress": 0}}, 1*time.Second)
|
||||
var canReportProgress = sockErr == nil
|
||||
var lastProgress float64
|
||||
var countFound = 0
|
||||
|
||||
for _, subDir := range allDirs {
|
||||
var dir0 = subDir + "/p" + types.String(this.policy.Id)
|
||||
dir1Matches, err := filepath.Glob(dir0 + "/*")
|
||||
@@ -1585,6 +1595,20 @@ func (this *FileStorage) ScanGarbageCaches(fileCallback func(path string) error)
|
||||
continue
|
||||
}
|
||||
|
||||
countDirs++
|
||||
|
||||
// report progress
|
||||
if canReportProgress {
|
||||
var progress = float64(countDirs) / 65536
|
||||
if fmt.Sprintf("%.2f", lastProgress) != fmt.Sprintf("%.2f", progress) {
|
||||
lastProgress = progress
|
||||
_, _ = progressSock.SendTimeout(&gosock.Command{Code: "progress", Params: map[string]any{
|
||||
"progress": progress,
|
||||
"count": countFound,
|
||||
}}, 100*time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
fileMatches, err := filepath.Glob(dir2 + "/*.cache")
|
||||
if err != nil {
|
||||
// ignore error
|
||||
@@ -1617,6 +1641,7 @@ func (this *FileStorage) ScanGarbageCaches(fileCallback func(path string) error)
|
||||
}
|
||||
|
||||
if fileCallback != nil {
|
||||
countFound++
|
||||
err = fileCallback(file)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -1627,6 +1652,14 @@ func (this *FileStorage) ScanGarbageCaches(fileCallback func(path string) error)
|
||||
}
|
||||
}
|
||||
|
||||
// 100% progress
|
||||
if canReportProgress && lastProgress != 1 {
|
||||
_, _ = progressSock.SendTimeout(&gosock.Command{Code: "progress", Params: map[string]any{
|
||||
"progress": 1,
|
||||
"count": countFound,
|
||||
}}, 100*time.Millisecond)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -14,5 +14,6 @@ const (
|
||||
// SystemdServiceName systemd
|
||||
SystemdServiceName = "edge-node"
|
||||
|
||||
AccessLogSockName = "edge-node.accesslog.sock"
|
||||
AccessLogSockName = "edge-node.accesslog"
|
||||
CacheGarbageSockName = "edge-node.cache.garbage"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user