Skip to content

Commit

Permalink
Merge pull request TencentBlueKing#161 from tbs60/dev_tming
Browse files Browse the repository at this point in the history
Dev tming
  • Loading branch information
tming authored Dec 22, 2023
2 parents 1a9c835 + 119405d commit fc2b5fb
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/backend/booster/bk_dist/booster/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
},
}
)

Expand Down
6 changes: 6 additions & 0 deletions src/backend/booster/bk_dist/booster/command/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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{
Expand Down
16 changes: 16 additions & 0 deletions src/backend/booster/bk_dist/booster/pkg/booster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{}
Expand Down
23 changes: 23 additions & 0 deletions src/backend/booster/bk_dist/booster/pkg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/backend/booster/bk_dist/common/types/booster.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ type BoosterWorks struct {

IdleKeepSecs int

CleanTmpFilesDayAgo int

EnableLink bool
EnableLib bool
}
Expand Down

0 comments on commit fc2b5fb

Please sign in to comment.