feat: 计划任务增加缓存清理 (#2592)

Refs #2564
This commit is contained in:
ssongliu 2023-10-18 17:08:24 +08:00 committed by GitHub
parent 743e7d0b59
commit 8bb07cf4f5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 183 additions and 63 deletions

View file

@ -64,6 +64,11 @@ func (u *CronjobService) HandleJob(cronjob *model.Cronjob) {
if err != nil { if err != nil {
global.LOG.Errorf("cut website log file failed, err: %v", err) global.LOG.Errorf("cut website log file failed, err: %v", err)
} }
case "clean":
messageItem := ""
messageItem, err = u.handleSystemClean()
message = []byte(messageItem)
u.HandleRmExpired("LOCAL", "", "", cronjob, nil)
} }
if err != nil { if err != nil {
@ -584,3 +589,7 @@ func (u *CronjobService) handleSnapshot(cronjob *model.Cronjob, startTime time.T
u.HandleRmExpired(backup.Type, backup.BackupPath, "", cronjob, client) u.HandleRmExpired(backup.Type, backup.BackupPath, "", cronjob, client)
return message, path, nil return message, path, nil
} }
func (u *CronjobService) handleSystemClean() (string, error) {
return NewISettingService().SystemCleanForCronjob()
}

View file

@ -41,6 +41,7 @@ type ISettingService interface {
SystemScan() dto.CleanData SystemScan() dto.CleanData
SystemClean(req []dto.Clean) SystemClean(req []dto.Clean)
SystemCleanForCronjob() (string, error)
} }
func NewISettingService() ISettingService { func NewISettingService() ISettingService {

View file

@ -11,10 +11,28 @@ import (
"github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto"
"github.com/1Panel-dev/1Panel/backend/global" "github.com/1Panel-dev/1Panel/backend/global"
"github.com/1Panel-dev/1Panel/backend/utils/cmd" "github.com/1Panel-dev/1Panel/backend/utils/cmd"
"github.com/1Panel-dev/1Panel/backend/utils/common"
fileUtils "github.com/1Panel-dev/1Panel/backend/utils/files" fileUtils "github.com/1Panel-dev/1Panel/backend/utils/files"
"github.com/google/uuid" "github.com/google/uuid"
) )
const (
upgradePath = "1panel/tmp/upgrade"
snapshotTmpPath = "1panel/tmp/system"
rollbackPath = "1panel/tmp"
cachePath = "1panel/cache"
oldOriginalPath = "original"
oldAppBackupPath = "1panel/resource/apps_bak"
oldDownloadPath = "1panel/tmp/download"
oldUpgradePath = "1panel/tmp"
tmpUploadPath = "1panel/tmp/upload"
uploadPath = "1panel/uploads"
downloadPath = "1panel/download"
logPath = "1panel/log"
dockerLogPath = "1panel/tmp/docker_logs"
taskPath = "1panel/task"
)
func (u *SettingService) SystemScan() dto.CleanData { func (u *SettingService) SystemScan() dto.CleanData {
var ( var (
SystemClean dto.CleanData SystemClean dto.CleanData
@ -34,7 +52,7 @@ func (u *SettingService) SystemScan() dto.CleanData {
Children: loadTreeWithDir(true, "1panel_original", originalPath, fileOp), Children: loadTreeWithDir(true, "1panel_original", originalPath, fileOp),
}) })
upgradePath := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade") upgradePath := path.Join(global.CONF.System.BaseDir, upgradePath)
upgradeSize, _ := fileOp.GetDirSize(upgradePath) upgradeSize, _ := fileOp.GetDirSize(upgradePath)
treeData = append(treeData, dto.CleanTree{ treeData = append(treeData, dto.CleanTree{
ID: uuid.NewString(), ID: uuid.NewString(),
@ -76,7 +94,7 @@ func (u *SettingService) SystemScan() dto.CleanData {
Children: rollBackTree, Children: rollBackTree,
}) })
cachePath := path.Join(global.CONF.System.BaseDir, "1panel/cache") cachePath := path.Join(global.CONF.System.BaseDir, cachePath)
cacheSize, _ := fileOp.GetDirSize(cachePath) cacheSize, _ := fileOp.GetDirSize(cachePath)
treeData = append(treeData, dto.CleanTree{ treeData = append(treeData, dto.CleanTree{
ID: uuid.NewString(), ID: uuid.NewString(),
@ -125,94 +143,94 @@ func (u *SettingService) SystemClean(req []dto.Clean) {
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel_original", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel_original", item.Name))
case "upgrade": case "upgrade":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/upgrade", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, upgradePath, item.Name))
case "snapshot": case "snapshot":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/system", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, snapshotTmpPath, item.Name))
dropFileOrDir(path.Join(global.CONF.System.Backup, "system", item.Name)) dropFileOrDir(path.Join(global.CONF.System.Backup, "system", item.Name))
case "snapshot_tmp": case "snapshot_tmp":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/system", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, snapshotTmpPath, item.Name))
case "snapshot_local": case "snapshot_local":
dropFileOrDir(path.Join(global.CONF.System.Backup, "system", item.Name)) dropFileOrDir(path.Join(global.CONF.System.Backup, "system", item.Name))
case "rollback": case "rollback":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/app")) dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "app"))
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/database")) dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "database"))
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/website")) dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "website"))
case "rollback_app": case "rollback_app":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/app", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "app", item.Name))
case "rollback_database": case "rollback_database":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/database", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "database", item.Name))
case "rollback_website": case "rollback_website":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/website", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, rollbackPath, "website", item.Name))
case "cache": case "cache":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/cache", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, cachePath, item.Name))
restart = true restart = true
case "unused": case "unused":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "original")) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldOriginalPath))
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/resource/apps_bak")) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldAppBackupPath))
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/download")) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldDownloadPath))
files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp")) files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath))
if len(files) == 0 { if len(files) == 0 {
continue continue
} }
for _, file := range files { for _, file := range files {
if strings.HasPrefix(file.Name(), "upgrade_") { if strings.HasPrefix(file.Name(), "upgrade_") {
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp", file.Name())) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath, file.Name()))
} }
} }
case "old_original": case "old_original":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "original", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldOriginalPath, item.Name))
case "old_apps_bak": case "old_apps_bak":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/resource/apps_bak", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldAppBackupPath, item.Name))
case "old_download": case "old_download":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/download", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldDownloadPath, item.Name))
case "old_upgrade": case "old_upgrade":
if len(item.Name) == 0 { if len(item.Name) == 0 {
files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp")) files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath))
if len(files) == 0 { if len(files) == 0 {
continue continue
} }
for _, file := range files { for _, file := range files {
if strings.HasPrefix(file.Name(), "upgrade_") { if strings.HasPrefix(file.Name(), "upgrade_") {
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp", file.Name())) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath, file.Name()))
} }
} }
} else { } else {
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, oldUpgradePath, item.Name))
} }
case "upload": case "upload":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, item.Name))
if len(item.Name) == 0 { if len(item.Name) == 0 {
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/upload")) dropFileOrDir(path.Join(global.CONF.System.BaseDir, tmpUploadPath))
} }
case "upload_tmp": case "upload_tmp":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/upload", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, tmpUploadPath, item.Name))
case "upload_app": case "upload_app":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/app", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "app", item.Name))
case "upload_database": case "upload_database":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/database", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "database", item.Name))
case "upload_website": case "upload_website":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/website", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "website", item.Name))
case "upload_directory": case "upload_directory":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/uploads/directory", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, uploadPath, "directory", item.Name))
case "download": case "download":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, item.Name))
case "download_app": case "download_app":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/app", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "app", item.Name))
case "download_database": case "download_database":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/database", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "database", item.Name))
case "download_website": case "download_website":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/website", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "website", item.Name))
case "download_directory": case "download_directory":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/download/directory", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, downloadPath, "directory", item.Name))
case "system_log": case "system_log":
if len(item.Name) == 0 { if len(item.Name) == 0 {
files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, "1panel/log")) files, _ := os.ReadDir(path.Join(global.CONF.System.BaseDir, logPath))
if len(files) == 0 { if len(files) == 0 {
continue continue
} }
@ -220,16 +238,16 @@ func (u *SettingService) SystemClean(req []dto.Clean) {
if file.Name() == "1Panel.log" { if file.Name() == "1Panel.log" {
continue continue
} }
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/log", file.Name())) dropFileOrDir(path.Join(global.CONF.System.BaseDir, logPath, file.Name()))
} }
} else { } else {
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/log", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, logPath, item.Name))
} }
case "docker_log": case "docker_log":
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/tmp/docker_logs", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, dockerLogPath, item.Name))
case "task_log": case "task_log":
pathItem := path.Join(global.CONF.System.BaseDir, "1panel/task", item.Name) pathItem := path.Join(global.CONF.System.BaseDir, taskPath, item.Name)
dropFileOrDir(path.Join(global.CONF.System.BaseDir, "1panel/task", item.Name)) dropFileOrDir(path.Join(global.CONF.System.BaseDir, taskPath, item.Name))
if len(item.Name) == 0 { if len(item.Name) == 0 {
files, _ := os.ReadDir(pathItem) files, _ := os.ReadDir(pathItem)
if len(files) == 0 { if len(files) == 0 {
@ -258,9 +276,70 @@ func (u *SettingService) SystemClean(req []dto.Clean) {
} }
} }
func (u *SettingService) SystemCleanForCronjob() (string, error) {
logs := ""
size := int64(0)
fileCount := 0
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, "1panel_original"), &logs, &size, &fileCount)
upgradePath := path.Join(global.CONF.System.BaseDir, upgradePath)
upgradeFiles, _ := os.ReadDir(upgradePath)
if len(upgradeFiles) != 0 {
sort.Slice(upgradeFiles, func(i, j int) bool {
return upgradeFiles[i].Name() > upgradeFiles[j].Name()
})
for i := 0; i < len(upgradeFiles); i++ {
if i != 0 {
dropFileOrDirWithLog(path.Join(upgradePath, upgradeFiles[i].Name()), &logs, &size, &fileCount)
}
}
}
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, snapshotTmpPath), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.Backup, "system"), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, rollbackPath, "app"), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, rollbackPath, "website"), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, rollbackPath, "database"), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, oldOriginalPath), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, oldAppBackupPath), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, oldDownloadPath), &logs, &size, &fileCount)
oldUpgradePath := path.Join(global.CONF.System.BaseDir, oldUpgradePath)
oldUpgradeFiles, _ := os.ReadDir(oldUpgradePath)
if len(oldUpgradeFiles) != 0 {
for i := 0; i < len(oldUpgradeFiles); i++ {
dropFileOrDirWithLog(path.Join(oldUpgradePath, oldUpgradeFiles[i].Name()), &logs, &size, &fileCount)
}
}
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, tmpUploadPath), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, uploadPath), &logs, &size, &fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, downloadPath), &logs, &size, &fileCount)
logPath := path.Join(global.CONF.System.BaseDir, logPath)
logFiles, _ := os.ReadDir(logPath)
if len(logFiles) != 0 {
for i := 0; i < len(logFiles); i++ {
if logFiles[i].Name() != "1Panel.log" {
dropFileOrDirWithLog(path.Join(logPath, logFiles[i].Name()), &logs, &size, &fileCount)
}
}
}
timeNow := time.Now().Format("2006-01-02 15:04:05")
logs += fmt.Sprintf("%s: total clean: %s, total count: %d", timeNow, common.LoadSizeUnit2F(float64(size)), fileCount)
dropFileOrDirWithLog(path.Join(global.CONF.System.BaseDir, dockerLogPath), &logs, &size, &fileCount)
_ = settingRepo.Update("LastCleanTime", timeNow)
_ = settingRepo.Update("LastCleanSize", fmt.Sprintf("%v", size))
_ = settingRepo.Update("LastCleanData", fmt.Sprintf("%v", fileCount))
return logs, nil
}
func loadSnapshotTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadSnapshotTree(fileOp fileUtils.FileOp) []dto.CleanTree {
var treeData []dto.CleanTree var treeData []dto.CleanTree
path1 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/system") path1 := path.Join(global.CONF.System.BaseDir, snapshotTmpPath)
list1 := loadTreeWithAllFile(true, path1, "snapshot_tmp", path1, fileOp) list1 := loadTreeWithAllFile(true, path1, "snapshot_tmp", path1, fileOp)
if len(list1) != 0 { if len(list1) != 0 {
size, _ := fileOp.GetDirSize(path1) size, _ := fileOp.GetDirSize(path1)
@ -278,17 +357,17 @@ func loadSnapshotTree(fileOp fileUtils.FileOp) []dto.CleanTree {
func loadRollBackTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadRollBackTree(fileOp fileUtils.FileOp) []dto.CleanTree {
var treeData []dto.CleanTree var treeData []dto.CleanTree
path1 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/app") path1 := path.Join(global.CONF.System.BaseDir, rollbackPath, "app")
list1 := loadTreeWithAllFile(true, path1, "rollback_app", path1, fileOp) list1 := loadTreeWithAllFile(true, path1, "rollback_app", path1, fileOp)
size1, _ := fileOp.GetDirSize(path1) size1, _ := fileOp.GetDirSize(path1)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_app", Size: uint64(size1), Children: list1, Type: "rollback_app", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_app", Size: uint64(size1), Children: list1, Type: "rollback_app", IsRecommend: true})
path2 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/website") path2 := path.Join(global.CONF.System.BaseDir, rollbackPath, "website")
list2 := loadTreeWithAllFile(true, path2, "rollback_website", path2, fileOp) list2 := loadTreeWithAllFile(true, path2, "rollback_website", path2, fileOp)
size2, _ := fileOp.GetDirSize(path2) size2, _ := fileOp.GetDirSize(path2)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_website", Size: uint64(size2), Children: list2, Type: "rollback_website", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_website", Size: uint64(size2), Children: list2, Type: "rollback_website", IsRecommend: true})
path3 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/database") path3 := path.Join(global.CONF.System.BaseDir, rollbackPath, "database")
list3 := loadTreeWithAllFile(true, path3, "rollback_database", path3, fileOp) list3 := loadTreeWithAllFile(true, path3, "rollback_database", path3, fileOp)
size3, _ := fileOp.GetDirSize(path3) size3, _ := fileOp.GetDirSize(path3)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_database", Size: uint64(size3), Children: list3, Type: "rollback_database", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "rollback_database", Size: uint64(size3), Children: list3, Type: "rollback_database", IsRecommend: true})
@ -298,28 +377,28 @@ func loadRollBackTree(fileOp fileUtils.FileOp) []dto.CleanTree {
func loadUnusedFile(fileOp fileUtils.FileOp) []dto.CleanTree { func loadUnusedFile(fileOp fileUtils.FileOp) []dto.CleanTree {
var treeData []dto.CleanTree var treeData []dto.CleanTree
path1 := path.Join(global.CONF.System.BaseDir, "original") path1 := path.Join(global.CONF.System.BaseDir, oldOriginalPath)
list1 := loadTreeWithAllFile(true, path1, "old_original", path1, fileOp) list1 := loadTreeWithAllFile(true, path1, "old_original", path1, fileOp)
if len(list1) != 0 { if len(list1) != 0 {
size, _ := fileOp.GetDirSize(path1) size, _ := fileOp.GetDirSize(path1)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_original", Size: uint64(size), Children: list1, Type: "old_original"}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_original", Size: uint64(size), Children: list1, Type: "old_original"})
} }
path2 := path.Join(global.CONF.System.BaseDir, "1panel/resource/apps_bak") path2 := path.Join(global.CONF.System.BaseDir, oldAppBackupPath)
list2 := loadTreeWithAllFile(true, path2, "old_apps_bak", path2, fileOp) list2 := loadTreeWithAllFile(true, path2, "old_apps_bak", path2, fileOp)
if len(list2) != 0 { if len(list2) != 0 {
size, _ := fileOp.GetDirSize(path2) size, _ := fileOp.GetDirSize(path2)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_apps_bak", Size: uint64(size), Children: list2, Type: "old_apps_bak"}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_apps_bak", Size: uint64(size), Children: list2, Type: "old_apps_bak"})
} }
path3 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/download") path3 := path.Join(global.CONF.System.BaseDir, oldDownloadPath)
list3 := loadTreeWithAllFile(true, path3, "old_download", path3, fileOp) list3 := loadTreeWithAllFile(true, path3, "old_download", path3, fileOp)
if len(list3) != 0 { if len(list3) != 0 {
size, _ := fileOp.GetDirSize(path3) size, _ := fileOp.GetDirSize(path3)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_download", Size: uint64(size), Children: list3, Type: "old_download"}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "old_download", Size: uint64(size), Children: list3, Type: "old_download"})
} }
path4 := path.Join(global.CONF.System.BaseDir, "1panel/tmp") path4 := path.Join(global.CONF.System.BaseDir, oldUpgradePath)
list4 := loadTreeWithDir(true, "old_upgrade", path4, fileOp) list4 := loadTreeWithDir(true, "old_upgrade", path4, fileOp)
itemSize := uint64(0) itemSize := uint64(0)
for _, item := range list4 { for _, item := range list4 {
@ -334,32 +413,32 @@ func loadUnusedFile(fileOp fileUtils.FileOp) []dto.CleanTree {
func loadUploadTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadUploadTree(fileOp fileUtils.FileOp) []dto.CleanTree {
var treeData []dto.CleanTree var treeData []dto.CleanTree
path0 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/upload") path0 := path.Join(global.CONF.System.BaseDir, tmpUploadPath)
list0 := loadTreeWithAllFile(true, path0, "upload_tmp", path0, fileOp) list0 := loadTreeWithAllFile(true, path0, "upload_tmp", path0, fileOp)
size0, _ := fileOp.GetDirSize(path0) size0, _ := fileOp.GetDirSize(path0)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_tmp", Size: uint64(size0), Children: list0, Type: "upload_tmp", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_tmp", Size: uint64(size0), Children: list0, Type: "upload_tmp", IsRecommend: true})
path1 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/app") path1 := path.Join(global.CONF.System.BaseDir, uploadPath, "app")
list1 := loadTreeWithAllFile(true, path1, "upload_app", path1, fileOp) list1 := loadTreeWithAllFile(true, path1, "upload_app", path1, fileOp)
size1, _ := fileOp.GetDirSize(path1) size1, _ := fileOp.GetDirSize(path1)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_app", Size: uint64(size1), Children: list1, Type: "upload_app", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_app", Size: uint64(size1), Children: list1, Type: "upload_app", IsRecommend: true})
path2 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/website") path2 := path.Join(global.CONF.System.BaseDir, uploadPath, "website")
list2 := loadTreeWithAllFile(true, path2, "upload_website", path2, fileOp) list2 := loadTreeWithAllFile(true, path2, "upload_website", path2, fileOp)
size2, _ := fileOp.GetDirSize(path2) size2, _ := fileOp.GetDirSize(path2)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_website", Size: uint64(size2), Children: list2, Type: "upload_website", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_website", Size: uint64(size2), Children: list2, Type: "upload_website", IsRecommend: true})
path3 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/database") path3 := path.Join(global.CONF.System.BaseDir, uploadPath, "database")
list3 := loadTreeWithAllFile(true, path3, "upload_database", path3, fileOp) list3 := loadTreeWithAllFile(true, path3, "upload_database", path3, fileOp)
size3, _ := fileOp.GetDirSize(path3) size3, _ := fileOp.GetDirSize(path3)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_database", Size: uint64(size3), Children: list3, Type: "upload_database", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_database", Size: uint64(size3), Children: list3, Type: "upload_database", IsRecommend: true})
path4 := path.Join(global.CONF.System.BaseDir, "1panel/uploads/directory") path4 := path.Join(global.CONF.System.BaseDir, uploadPath, "directory")
list4 := loadTreeWithAllFile(true, path4, "upload_directory", path4, fileOp) list4 := loadTreeWithAllFile(true, path4, "upload_directory", path4, fileOp)
size4, _ := fileOp.GetDirSize(path4) size4, _ := fileOp.GetDirSize(path4)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_directory", Size: uint64(size4), Children: list4, Type: "upload_directory", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "upload_directory", Size: uint64(size4), Children: list4, Type: "upload_directory", IsRecommend: true})
path5 := path.Join(global.CONF.System.BaseDir, "1panel/uploads") path5 := path.Join(global.CONF.System.BaseDir, uploadPath)
uploadTreeData := loadTreeWithAllFile(true, path5, "upload", path5, fileOp) uploadTreeData := loadTreeWithAllFile(true, path5, "upload", path5, fileOp)
treeData = append(treeData, uploadTreeData...) treeData = append(treeData, uploadTreeData...)
@ -368,27 +447,27 @@ func loadUploadTree(fileOp fileUtils.FileOp) []dto.CleanTree {
func loadDownloadTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadDownloadTree(fileOp fileUtils.FileOp) []dto.CleanTree {
var treeData []dto.CleanTree var treeData []dto.CleanTree
path1 := path.Join(global.CONF.System.BaseDir, "1panel/download/app") path1 := path.Join(global.CONF.System.BaseDir, downloadPath, "app")
list1 := loadTreeWithAllFile(true, path1, "download_app", path1, fileOp) list1 := loadTreeWithAllFile(true, path1, "download_app", path1, fileOp)
size1, _ := fileOp.GetDirSize(path1) size1, _ := fileOp.GetDirSize(path1)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_app", Size: uint64(size1), Children: list1, Type: "download_app", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_app", Size: uint64(size1), Children: list1, Type: "download_app", IsRecommend: true})
path2 := path.Join(global.CONF.System.BaseDir, "1panel/download/website") path2 := path.Join(global.CONF.System.BaseDir, downloadPath, "website")
list2 := loadTreeWithAllFile(true, path2, "download_website", path2, fileOp) list2 := loadTreeWithAllFile(true, path2, "download_website", path2, fileOp)
size2, _ := fileOp.GetDirSize(path2) size2, _ := fileOp.GetDirSize(path2)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_website", Size: uint64(size2), Children: list2, Type: "download_website", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_website", Size: uint64(size2), Children: list2, Type: "download_website", IsRecommend: true})
path3 := path.Join(global.CONF.System.BaseDir, "1panel/download/database") path3 := path.Join(global.CONF.System.BaseDir, downloadPath, "database")
list3 := loadTreeWithAllFile(true, path3, "download_database", path3, fileOp) list3 := loadTreeWithAllFile(true, path3, "download_database", path3, fileOp)
size3, _ := fileOp.GetDirSize(path3) size3, _ := fileOp.GetDirSize(path3)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_database", Size: uint64(size3), Children: list3, Type: "download_database", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_database", Size: uint64(size3), Children: list3, Type: "download_database", IsRecommend: true})
path4 := path.Join(global.CONF.System.BaseDir, "1panel/download/directory") path4 := path.Join(global.CONF.System.BaseDir, downloadPath, "directory")
list4 := loadTreeWithAllFile(true, path4, "download_directory", path4, fileOp) list4 := loadTreeWithAllFile(true, path4, "download_directory", path4, fileOp)
size4, _ := fileOp.GetDirSize(path4) size4, _ := fileOp.GetDirSize(path4)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_directory", Size: uint64(size4), Children: list4, Type: "download_directory", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "download_directory", Size: uint64(size4), Children: list4, Type: "download_directory", IsRecommend: true})
path5 := path.Join(global.CONF.System.BaseDir, "1panel/download") path5 := path.Join(global.CONF.System.BaseDir, downloadPath)
uploadTreeData := loadTreeWithAllFile(true, path5, "download", path5, fileOp) uploadTreeData := loadTreeWithAllFile(true, path5, "download", path5, fileOp)
treeData = append(treeData, uploadTreeData...) treeData = append(treeData, uploadTreeData...)
@ -397,7 +476,7 @@ func loadDownloadTree(fileOp fileUtils.FileOp) []dto.CleanTree {
func loadLogTree(fileOp fileUtils.FileOp) []dto.CleanTree { func loadLogTree(fileOp fileUtils.FileOp) []dto.CleanTree {
var treeData []dto.CleanTree var treeData []dto.CleanTree
path1 := path.Join(global.CONF.System.BaseDir, "1panel/log") path1 := path.Join(global.CONF.System.BaseDir, logPath)
list1 := loadTreeWithAllFile(true, path1, "system_log", path1, fileOp) list1 := loadTreeWithAllFile(true, path1, "system_log", path1, fileOp)
size := uint64(0) size := uint64(0)
for _, file := range list1 { for _, file := range list1 {
@ -405,12 +484,12 @@ func loadLogTree(fileOp fileUtils.FileOp) []dto.CleanTree {
} }
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "system_log", Size: uint64(size), Children: list1, Type: "system_log", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "system_log", Size: uint64(size), Children: list1, Type: "system_log", IsRecommend: true})
path2 := path.Join(global.CONF.System.BaseDir, "1panel/tmp/docker_logs") path2 := path.Join(global.CONF.System.BaseDir, dockerLogPath)
list2 := loadTreeWithAllFile(true, path2, "docker_log", path2, fileOp) list2 := loadTreeWithAllFile(true, path2, "docker_log", path2, fileOp)
size2, _ := fileOp.GetDirSize(path2) size2, _ := fileOp.GetDirSize(path2)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "docker_log", Size: uint64(size2), Children: list2, Type: "docker_log", IsRecommend: true}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "docker_log", Size: uint64(size2), Children: list2, Type: "docker_log", IsRecommend: true})
path3 := path.Join(global.CONF.System.BaseDir, "1panel/task") path3 := path.Join(global.CONF.System.BaseDir, taskPath)
list3 := loadTreeWithAllFile(false, path3, "task_log", path3, fileOp) list3 := loadTreeWithAllFile(false, path3, "task_log", path3, fileOp)
size3, _ := fileOp.GetDirSize(path3) size3, _ := fileOp.GetDirSize(path3)
treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "task_log", Size: uint64(size3), Children: list3, Type: "task_log"}) treeData = append(treeData, dto.CleanTree{ID: uuid.NewString(), Label: "task_log", Size: uint64(size3), Children: list3, Type: "task_log"})
@ -506,3 +585,33 @@ func dropFileOrDir(itemPath string) {
global.LOG.Errorf("drop file %s failed, err %v", itemPath, err) global.LOG.Errorf("drop file %s failed, err %v", itemPath, err)
} }
} }
func dropFileOrDirWithLog(itemPath string, log *string, size *int64, count *int) {
itemSize := int64(0)
itemCount := 0
scanFile(itemPath, &itemSize, &itemCount)
*size += itemSize
*count += itemCount
if err := os.RemoveAll(itemPath); err != nil {
global.LOG.Errorf("drop file %s failed, err %v", itemPath, err)
*log += fmt.Sprintf("- drop file %s failed, err: %v \n\n", itemPath, err)
return
}
*log += fmt.Sprintf("+ drop file %s successful!, size: %s, count: %d \n\n", itemPath, common.LoadSizeUnit2F(float64(itemSize)), itemCount)
}
func scanFile(pathItem string, size *int64, count *int) {
files, _ := os.ReadDir(pathItem)
for _, f := range files {
if f.IsDir() {
scanFile(path.Join(pathItem, f.Name()), size, count)
} else {
fileInfo, err := f.Info()
if err != nil {
continue
}
*count++
*size += fileInfo.Size()
}
}
}

View file

@ -27,6 +27,7 @@
<el-option value="curl" :label="$t('cronjob.curl')" /> <el-option value="curl" :label="$t('cronjob.curl')" />
<el-option value="ntp" :label="$t('cronjob.ntp')" /> <el-option value="ntp" :label="$t('cronjob.ntp')" />
<el-option value="cutWebsiteLog" :label="$t('cronjob.cutWebsiteLog')" /> <el-option value="cutWebsiteLog" :label="$t('cronjob.cutWebsiteLog')" />
<el-option value="clean" :label="$t('setting.diskClean')" />
</el-select> </el-select>
<el-tag v-else>{{ $t('cronjob.' + dialogData.rowData!.type) }}</el-tag> <el-tag v-else>{{ $t('cronjob.' + dialogData.rowData!.type) }}</el-tag>
</el-form-item> </el-form-item>