diff --git a/src/backend/booster/bk_dist/booster/command/command.go b/src/backend/booster/bk_dist/booster/command/command.go index f4d99e2ec..177e2ade6 100644 --- a/src/backend/booster/bk_dist/booster/command/command.go +++ b/src/backend/booster/bk_dist/booster/command/command.go @@ -106,6 +106,7 @@ const ( FlagLongTCP = "long_tcp" FlagUseDefaultWorker = "use_default_worker" FlagDynamicPort = "dynamic_port" + FlagCleanTmpFilesDayAgo = "clean_tmp_files_day_ago" EnvBuildIDOld = "TURBO_PLAN_BUILD_ID" EnvBuildID = "TBS_BUILD_ID" @@ -447,6 +448,10 @@ var ( Name: "dynamic_port", Usage: "controller will listen dynamic port if true", }, + commandCli.IntFlag{ + Name: "clean_tmp_files_day_ago", + Usage: "clean tmp files which modify time before this days, default is 1", + }, } ) diff --git a/src/backend/booster/bk_dist/booster/command/process.go b/src/backend/booster/bk_dist/booster/command/process.go index a0f971fbf..ecc59cd8c 100644 --- a/src/backend/booster/bk_dist/booster/command/process.go +++ b/src/backend/booster/bk_dist/booster/command/process.go @@ -242,6 +242,11 @@ func newBooster(c *commandCli.Context) (*pkg.Booster, error) { withDynamicPort = false } + cleanTmpFilesDayAgo := 1 + if c.IsSet(FlagCleanTmpFilesDayAgo) && c.Int(FlagCleanTmpFilesDayAgo) >= 0 { + cleanTmpFilesDayAgo = c.Int(FlagCleanTmpFilesDayAgo) + } + // generate a new booster. cmdConfig := dcType.BoosterConfig{ Type: dcType.GetBoosterType(bt), @@ -308,6 +313,7 @@ func newBooster(c *commandCli.Context) (*pkg.Booster, error) { NoWork: c.Bool(FlagNoWork), WriteMemroy: c.Bool(FlagWriteMemroMemroy), IdleKeepSecs: c.Int(FlagIdleKeepSecs), + CleanTmpFilesDayAgo: cleanTmpFilesDayAgo, }, Transport: dcType.BoosterTransport{ diff --git a/src/backend/booster/bk_dist/booster/pkg/booster.go b/src/backend/booster/bk_dist/booster/pkg/booster.go index c1ce3d188..69871b14c 100644 --- a/src/backend/booster/bk_dist/booster/pkg/booster.go +++ b/src/backend/booster/bk_dist/booster/pkg/booster.go @@ -33,6 +33,7 @@ import ( dcUtil "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/common/util" v1 "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/controller/pkg/api/v1" "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/handler" + commonUtil "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/handler/common" "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/handler/handlermap" "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/worker/pkg/client" "github.com/TencentBlueKing/bk-turbo/src/backend/booster/common" @@ -515,6 +516,9 @@ func (b *Booster) run(pCtx context.Context) (int, error) { // support pump check b.checkPump() + // support tmp file clean + b.cleanTmpFiles() + // no work commands do not register if b.config.Works.NoWork { return b.runWorks(pCtx, nil, b.runNoWorkCommands) @@ -1349,6 +1353,18 @@ func (b *Booster) checkPumpCache(pumpdir string) { } } +func (b *Booster) cleanTmpFiles() { + env.SetEnv(env.BoosterType, b.config.Type.String()) + tmpdir := commonUtil.GetHandlerTmpDir(nil) + currentTime := time.Now() + + daysAgo := b.config.Works.CleanTmpFilesDayAgo + previousTime := currentTime.AddDate(0, 0, -daysAgo) + + blog.Infof("booster: ready clean tmp dir:%s before the time:%s", tmpdir, previousTime) + cleanDirOnlyByTime(tmpdir, previousTime) +} + // get default xcode link path func getXcodeIncludeLinkDir() ([]string, error) { sandbox := dcSyscall.Sandbox{} diff --git a/src/backend/booster/bk_dist/booster/pkg/util.go b/src/backend/booster/bk_dist/booster/pkg/util.go index 0dc7a8da2..e85446b9f 100644 --- a/src/backend/booster/bk_dist/booster/pkg/util.go +++ b/src/backend/booster/bk_dist/booster/pkg/util.go @@ -21,6 +21,7 @@ import ( "strconv" "strings" "syscall" + "time" "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/common/protocol" dcSDK "github.com/TencentBlueKing/bk-turbo/src/backend/booster/bk_dist/common/sdk" @@ -219,6 +220,28 @@ func cleanDirByTime(dir string, limitsize int64) { } } +func cleanDirOnlyByTime(dir string, t time.Time) { + f, err := os.Open(dir) + if err != nil { + blog.Warnf("booster: failed to open dir %s with error:%v", dir, err) + return + } + fis, _ := f.Readdir(-1) + f.Close() + sort.Sort(ByModTime(fis)) + + blog.Infof("booster: dir %s time:%s", dir, t) + fullpath := "" + for _, fi := range fis { + if fi.ModTime().After(t) { + break + } + fullpath = filepath.Join(dir, fi.Name()) + blog.Infof("booster: ready remove file:%s modify time:%s", fullpath, fi.ModTime()) + os.RemoveAll(fullpath) + } +} + func searchSymlink(dirPth string, files map[string]string) (err error) { if files == nil { files = make(map[string]string, 100) diff --git a/src/backend/booster/bk_dist/common/types/booster.go b/src/backend/booster/bk_dist/common/types/booster.go index 353127570..8dfdc0464 100644 --- a/src/backend/booster/bk_dist/common/types/booster.go +++ b/src/backend/booster/bk_dist/common/types/booster.go @@ -127,6 +127,8 @@ type BoosterWorks struct { IdleKeepSecs int + CleanTmpFilesDayAgo int + EnableLink bool EnableLib bool }