From 984977c889da4abf1adb9d9ebbbfe0b015f35d24 Mon Sep 17 00:00:00 2001 From: GoEdgeLab Date: Thu, 28 Sep 2023 15:02:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A7=E8=A1=8Cedge-node=20cache.badge?= =?UTF-8?q?=E5=91=BD=E4=BB=A4=E6=97=B6=E6=89=93=E5=8D=B0=E8=BF=9B=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/edge-node/main.go | 13 +++++++++++++ internal/caches/storage_file.go | 33 +++++++++++++++++++++++++++++++++ internal/const/const.go | 3 ++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/cmd/edge-node/main.go b/cmd/edge-node/main.go index 0bc22ab..a2e525b 100644 --- a/cmd/edge-node/main.go +++ b/cmd/edge-node/main.go @@ -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", diff --git a/internal/caches/storage_file.go b/internal/caches/storage_file.go index 6a41f3d..df26750 100644 --- a/internal/caches/storage_file.go +++ b/internal/caches/storage_file.go @@ -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 } diff --git a/internal/const/const.go b/internal/const/const.go index 27d4e7d..23bb46f 100644 --- a/internal/const/const.go +++ b/internal/const/const.go @@ -14,5 +14,6 @@ const ( // SystemdServiceName systemd SystemdServiceName = "edge-node" - AccessLogSockName = "edge-node.accesslog.sock" + AccessLogSockName = "edge-node.accesslog" + CacheGarbageSockName = "edge-node.cache.garbage" )