Pārlūkot izejas kodu

fix: 删除过期文件 (#15)

Co-authored-by: jeessy2 <[email protected]>
jeessy2 3 gadi atpakaļ
vecāks
revīzija
910cfcd83c
3 mainītis faili ar 34 papildinājumiem un 62 dzēšanām
  1. 30 29
      client/delete_old_file.go
  2. 4 7
      util/docker_util.go
  3. 0 26
      util/file_util.go

+ 30 - 29
client/delete_old_file.go

@@ -12,43 +12,44 @@ import (
 
 // DeleteOldBackup for client
 func DeleteOldBackup() {
-	// sleep 30 minutes
-	time.Sleep(30 * time.Minute)
 	for {
+		delay := util.GetDelaySeconds(2)
+		log.Printf("删除过期的备份文件将在 %.1f 小时后运行\n", delay.Hours())
+		time.Sleep(delay)
+
 		conf, err := entity.GetConfigCache()
-		if err == nil {
-			for _, backupConf := range conf.BackupConfig {
-				// read from current path
-				backupFiles, err := ioutil.ReadDir(backupConf.GetProjectPath())
-				if err != nil {
-					log.Println("Read dir with error :", err)
-					continue
-				}
+		if err != nil {
+			return
+		}
 
-				// delete client files
-				ago := time.Now()
-				for _, conf := range conf.BackupConfig {
-					lastDay, _ := time.ParseDuration("-" + strconv.Itoa(conf.SaveDays*24) + "h")
-					ago = ago.Add(lastDay)
+		for _, backupConf := range conf.BackupConfig {
+			if !backupConf.NotEmptyProject() {
+				continue
+			}
+			// read from current path
+			backupFiles, err := ioutil.ReadDir(backupConf.GetProjectPath())
+			if err != nil {
+				log.Printf("读取项目 %s 目录失败! ERR: %s\n", backupConf.ProjectName, err)
+				continue
+			}
+
+			// delete client files
+			subDuration, _ := time.ParseDuration("-" + strconv.Itoa(backupConf.SaveDays*24) + "h")
+			before := time.Now().Add(subDuration)
 
-					// delete older file when file numbers gt MaxSaveDays
-					for _, backupFile := range backupFiles {
-						if backupFile.ModTime().Before(ago) {
-							filepath := backupConf.GetProjectPath() + "/" + backupFile.Name()
-							err := os.Remove(filepath)
-							if err != nil {
-								log.Printf("删除过期的文件 %s 失败", filepath)
-							} else {
-								log.Printf("删除过期的文件 %s 成功", filepath)
-							}
-						}
+			// delete older file when file numbers gt MaxSaveDays
+			for _, backupFile := range backupFiles {
+				if backupFile.ModTime().Before(before) {
+					filepath := backupConf.GetProjectPath() + string(os.PathSeparator) + backupFile.Name()
+					err := os.Remove(filepath)
+					if err != nil {
+						log.Printf("删除过期的文件 %s 失败", filepath)
+					} else {
+						log.Printf("删除过期的文件 %s 成功", filepath)
 					}
 				}
 			}
-
 		}
-		// sleep
-		util.SleepForFileDelete()
 	}
 
 }

+ 4 - 7
util/docker_util.go

@@ -2,14 +2,11 @@ package util
 
 import "os"
 
-// DockerEnvFile Docker容器中包含的文件
-const DockerEnvFile string = "/.dockerenv"
+// dockerEnvFile Docker容器中包含的文件
+const dockerEnvFile string = "/.dockerenv"
 
 // IsRunInDocker 是否在docker中运行
 func IsRunInDocker() bool {
-	_, err := os.Stat(DockerEnvFile)
-	if err != nil {
-		return false
-	}
-	return true
+	_, err := os.Stat(dockerEnvFile)
+	return err == nil
 }

+ 0 - 26
util/file_util.go

@@ -1,26 +0,0 @@
-package util
-
-import (
-	"log"
-	"os"
-	"time"
-)
-
-// PathExists Get path exist
-func PathExists(path string) bool {
-	_, err := os.Stat(path)
-	if err == nil {
-		return true
-	}
-	if os.IsNotExist(err) {
-		return false
-	}
-	return false
-}
-
-// SleepForFileDelete Sleep For File Delete
-func SleepForFileDelete() {
-	sleepHours := 24 - time.Now().Hour()
-	log.Printf("%d小时后再次运行:删除过期的备份文件", sleepHours)
-	time.Sleep(time.Hour * time.Duration(sleepHours))
-}