Skip to content

Commit

Permalink
bugfix(cron):使用二进制指数退避算法优化删除文件时数据库链接失败直接panic的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
li1553770945 committed Mar 14, 2024
1 parent 3932158 commit dfc202e
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions biz/internal/service/global_service/cron.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/robfig/cron/v3"
"os"
"personal-page-be/biz/internal/constant"
"time"
)

func (s *GlobalService) DeleteFile() {
Expand All @@ -17,19 +18,30 @@ func (s *GlobalService) DeleteFile() {
if file.IsDir() {
continue
}
fileEntity, err := s.Repo.FindFileBySaveName(file.Name())
if err != nil {
panic(err)
}
if fileEntity.ID == 0 || fileEntity.Count == 0 {
filePath := fmt.Sprintf("./%s/%s", constant.FileBasePath, file.Name())
err = os.Remove(filePath)
maxRetries := 5 // 最大重试次数
waitInterval := 500 // 初始等待间隔(毫秒)

for i := 0; i < maxRetries; i++ {
fileEntity, err := s.Repo.FindFileBySaveName(file.Name())
if err != nil {
s.Logger.Error("删除文件失败" + err.Error())
} else {
s.Logger.Info(fmt.Sprintf("删除文件%s成功", file.Name()))
s.Logger.Error("查询要删除的文件失败:" + err.Error())
time.Sleep(time.Duration(waitInterval) * time.Millisecond)
waitInterval *= 2 // 指数增长等待间隔
continue
}
if fileEntity.ID == 0 || fileEntity.Count == 0 {
filePath := fmt.Sprintf("./%s/%s", constant.FileBasePath, file.Name())
err = os.Remove(filePath)
if err != nil {
s.Logger.Error("删除文件失败" + err.Error())
} else {
s.Logger.Info(fmt.Sprintf("删除文件%s成功", file.Name()))
}
}
break

}

}
}
func (s *GlobalService) StartCronDeleteFile() {
Expand Down

0 comments on commit dfc202e

Please sign in to comment.