Skip to content

Commit 15e499c

Browse files
authored
perf: Optimize cache cleanup logic (#11648)
1 parent 55ccb9f commit 15e499c

File tree

3 files changed

+64
-34
lines changed

3 files changed

+64
-34
lines changed

agent/app/dto/setting.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ type CleanTree struct {
5555
Size uint64 `json:"size"`
5656
IsCheck bool `json:"isCheck"`
5757
IsRecommend bool `json:"isRecommend"`
58-
IsDisabled bool `json:"isDisabled"`
5958
}
6059

6160
type Clean struct {

agent/app/service/device_clean.go

Lines changed: 63 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ func (u *DeviceService) Clean(req []dto.Clean) {
105105
case "tmp_backup":
106106
dropFileOrDir(path.Join(global.Dir.LocalBackupDir, "tmp"))
107107
case "unknown_backup":
108-
dropFileOrDir(item.Name)
108+
if strings.HasPrefix(item.Name, path.Join(global.Dir.LocalBackupDir, "log/website")) {
109+
dropFileOrDir(item.Name)
110+
} else {
111+
dropFile(item.Name)
112+
}
109113

110114
case "rollback":
111115
dropFileOrDir(path.Join(global.Dir.BaseDir, rollbackPath, "app"))
@@ -182,6 +186,7 @@ func (u *DeviceService) Clean(req []dto.Clean) {
182186
}
183187
}
184188

189+
_ = cleanEmptyDirs(global.Dir.LocalBackupDir)
185190
_ = settingRepo.Update("LastCleanTime", time.Now().Format(constant.DateTimeLayout))
186191
_ = settingRepo.Update("LastCleanSize", fmt.Sprintf("%v", size))
187192
_ = settingRepo.Update("LastCleanData", fmt.Sprintf("%v", len(req)))
@@ -317,6 +322,9 @@ func loadAgentPackage(fileOp fileUtils.FileOp) dto.CleanTree {
317322
itemSize, _ := file.Info()
318323
itemName := file.Name()
319324
isCurrentVersion := strings.HasPrefix(itemName, fmt.Sprintf("1panel-agent_%s_", global.CONF.Base.Version))
325+
if isCurrentVersion {
326+
continue
327+
}
320328
itemTree.Size += uint64(itemSize.Size())
321329
itemTree.Children = append(itemTree.Children, dto.CleanTree{
322330
ID: uuid.NewString(),
@@ -326,7 +334,6 @@ func loadAgentPackage(fileOp fileUtils.FileOp) dto.CleanTree {
326334
IsCheck: !isCurrentVersion,
327335
IsRecommend: true,
328336
Type: "agent",
329-
IsDisabled: isCurrentVersion,
330337
})
331338
}
332339
}
@@ -363,7 +370,7 @@ func loadBackupTree(fileOp fileUtils.FileOp) []dto.CleanTree {
363370
treeData = append(treeData, loadUnknownDbs(fileOp, recordMap))
364371
treeData = append(treeData, loadUnknownWebsites(fileOp, recordMap))
365372
treeData = append(treeData, loadUnknownSnapshot(fileOp))
366-
treeData = append(treeData, loadUnknownWebsiteLog(fileOp, recordMap))
373+
treeData = append(treeData, loadUnknownWebsiteLog(fileOp))
367374
return treeData
368375
}
369376

@@ -390,7 +397,6 @@ func loadUnknownApps(fileOp fileUtils.FileOp, recordMap map[string][]string) dto
390397
Type: "unknown_backup",
391398
}
392399
_ = loadFileOrDirWithExclude(fileOp, 0, backupPath, &treeData, excludePaths)
393-
loadBackupIsCheck(&treeData)
394400
return treeData
395401
}
396402
func loadUnknownDbs(fileOp fileUtils.FileOp, recordMap map[string][]string) dto.CleanTree {
@@ -418,15 +424,13 @@ func loadUnknownDbs(fileOp fileUtils.FileOp, recordMap map[string][]string) dto.
418424
Type: "unknown_backup",
419425
}
420426
_ = loadFileOrDirWithExclude(fileOp, 0, backupPath, &treeData, excludePaths)
421-
422-
loadBackupIsCheck(&treeData)
423427
return treeData
424428
}
425429
func loadUnknownWebsites(fileOp fileUtils.FileOp, recordMap map[string][]string) dto.CleanTree {
426430
websites, _ := websiteRepo.List()
427431
var excludePaths []string
428432
for _, website := range websites {
429-
itemName := fmt.Sprintf("website/%s", website.PrimaryDomain)
433+
itemName := fmt.Sprintf("website/%s", website.Alias)
430434
if val, ok := recordMap[itemName]; ok {
431435
for _, item := range val {
432436
excludePaths = append(excludePaths, path.Join(global.Dir.LocalBackupDir, itemName, item))
@@ -445,7 +449,6 @@ func loadUnknownWebsites(fileOp fileUtils.FileOp, recordMap map[string][]string)
445449
Type: "unknown_backup",
446450
}
447451
_ = loadFileOrDirWithExclude(fileOp, 0, backupPath, &treeData, excludePaths)
448-
loadBackupIsCheck(&treeData)
449452
return treeData
450453
}
451454
func loadUnknownSnapshot(fileOp fileUtils.FileOp) dto.CleanTree {
@@ -466,14 +469,16 @@ func loadUnknownSnapshot(fileOp fileUtils.FileOp) dto.CleanTree {
466469
entries, _ := os.ReadDir(backupPath)
467470
for _, entry := range entries {
468471
childPath := filepath.Join(backupPath, entry.Name())
472+
if isExactPathMatch(childPath, excludePaths) {
473+
continue
474+
}
469475
childNode := dto.CleanTree{
470476
ID: uuid.NewString(),
471477
Label: entry.Name(),
472478
IsCheck: false,
473479
IsRecommend: false,
474480
Name: childPath,
475481
Type: "unknown_backup",
476-
IsDisabled: isExactPathMatch(childPath, excludePaths),
477482
}
478483
if entry.IsDir() {
479484
itemSize, _ := fileOp.GetDirSize(childPath)
@@ -492,7 +497,7 @@ func loadUnknownSnapshot(fileOp fileUtils.FileOp) dto.CleanTree {
492497
return treeData
493498
}
494499

495-
func loadUnknownWebsiteLog(fileOp fileUtils.FileOp, recordMap map[string][]string) dto.CleanTree {
500+
func loadUnknownWebsiteLog(fileOp fileUtils.FileOp) dto.CleanTree {
496501
treeData := dto.CleanTree{
497502
ID: uuid.NewString(),
498503
Label: "unknown_website_log",
@@ -543,14 +548,16 @@ func loadFileOrDirWithExclude(fileOp fileUtils.FileOp, index uint, dir string, r
543548
}
544549
for _, entry := range entries {
545550
childPath := filepath.Join(dir, entry.Name())
551+
if isExactPathMatch(childPath, excludes) {
552+
continue
553+
}
546554
childNode := dto.CleanTree{
547555
ID: uuid.NewString(),
548556
Label: entry.Name(),
549557
IsCheck: false,
550558
IsRecommend: false,
551559
Name: childPath,
552560
Type: "unknown_backup",
553-
IsDisabled: !entry.IsDir() && isExactPathMatch(childPath, excludes),
554561
}
555562
if entry.IsDir() {
556563
if index < 4 {
@@ -578,26 +585,6 @@ func loadFileOrDirWithExclude(fileOp fileUtils.FileOp, index uint, dir string, r
578585
return nil
579586
}
580587

581-
func loadBackupIsCheck(treeData *dto.CleanTree) {
582-
for i := 0; i < len(treeData.Children); i++ {
583-
if len(treeData.Children[i].Children) == 0 {
584-
treeData.Children[i].IsCheck = true
585-
treeData.Children[i].IsRecommend = true
586-
continue
587-
}
588-
if treeData.Label != "unknown_database" && treeData.Label != "unknown_app" {
589-
continue
590-
}
591-
for j := 0; j < len(treeData.Children[i].Children); j++ {
592-
if len(treeData.Children[i].Children[j].Children) == 0 {
593-
treeData.Children[i].Children[j].IsCheck = true
594-
treeData.Children[i].Children[j].IsRecommend = true
595-
continue
596-
}
597-
}
598-
}
599-
}
600-
601588
func isExactPathMatch(path string, excludePaths []string) bool {
602589
cleanPath := filepath.Clean(path)
603590

@@ -909,6 +896,18 @@ func dropFileOrDir(itemPath string) {
909896
global.LOG.Errorf("drop file %s failed, err %v", itemPath, err)
910897
}
911898
}
899+
func dropFile(itemPath string) {
900+
info, err := os.Stat(itemPath)
901+
if err != nil {
902+
return
903+
}
904+
if info.IsDir() {
905+
return
906+
}
907+
if err := os.Remove(itemPath); err != nil {
908+
global.LOG.Errorf("drop file %s failed, err %v", itemPath, err)
909+
}
910+
}
912911

913912
func dropBuildCache() (int, int) {
914913
client, err := docker.NewDockerClient()
@@ -1107,3 +1106,36 @@ func scanFile(pathItem string, size *int64, count *int) {
11071106
}
11081107
}
11091108
}
1109+
1110+
func cleanEmptyDirs(root string) error {
1111+
dirsToCheck := make([]string, 0)
1112+
err := filepath.WalkDir(root, func(path string, d os.DirEntry, err error) error {
1113+
if err != nil {
1114+
return nil
1115+
}
1116+
if d.IsDir() {
1117+
dirsToCheck = append(dirsToCheck, path)
1118+
}
1119+
return nil
1120+
})
1121+
if err != nil {
1122+
return err
1123+
}
1124+
1125+
for i := len(dirsToCheck) - 1; i >= 0; i-- {
1126+
dir := dirsToCheck[i]
1127+
if dir == root {
1128+
continue
1129+
}
1130+
1131+
entries, err := os.ReadDir(dir)
1132+
if err != nil {
1133+
continue
1134+
}
1135+
1136+
if len(entries) == 0 {
1137+
_ = os.Remove(dir)
1138+
}
1139+
}
1140+
return nil
1141+
}

frontend/src/views/toolbox/clean/index.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,6 @@ const scanStatus = ref<string>('beforeScan');
271271
const defaultProps = {
272272
children: 'children',
273273
label: 'label',
274-
disabled: 'isDisabled',
275274
};
276275
const cleanData = reactive({
277276
systemClean: [],
@@ -403,7 +402,7 @@ const loadSubmitCheck = (data: any) => {
403402
return;
404403
}
405404
for (const item of data) {
406-
if (item.label === 'unknown_website_log' && item.isCheck && item.children) {
405+
if (item.type === 'unknown_backup' && item.isCheck && item.children) {
407406
loadSubmitCheck(item.children);
408407
continue;
409408
}

0 commit comments

Comments
 (0)