Skip to content

Commit

Permalink
feat: add retry configuration for failed save task
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Jan 7, 2025
1 parent 4b5cabc commit 8d0851f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.example.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
workers = 4 # 同时下载文件数
retry = 3 # 下载失败重试次数

[telegram]
token = "" # Bot Token
Expand Down
1 change: 1 addition & 0 deletions config/viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Config struct {
Workers int `toml:"workers" mapstructure:"workers"`
Retry int `toml:"retry" mapstructure:"retry"` // Retry times for failed tasks

Temp tempConfig `toml:"temp" mapstructure:"temp"`
Log logConfig `toml:"log" mapstructure:"log"`
Expand Down
18 changes: 15 additions & 3 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,21 @@ func processPendingTask(task *types.Task) error {
Message: "下载完成, 正在转存文件...",
ID: task.ReplyMessageID,
})

if err := storage.Save(task.Storage, task.Ctx, dest.Name(), task.StoragePath); err != nil {
return fmt.Errorf("Failed to save file: %w", err)
if config.Cfg.Retry <= 0 {
if err := storage.Save(task.Storage, task.Ctx, dest.Name(), task.StoragePath); err != nil {
return fmt.Errorf("Failed to save file: %w", err)
}
} else {
for i := 0; i < config.Cfg.Retry; i++ {
if err := storage.Save(task.Storage, task.Ctx, dest.Name(), task.StoragePath); err != nil {
logger.L.Errorf("Failed to save file: %s, retrying...", err)
if i == config.Cfg.Retry-1 {
return fmt.Errorf("Failed to save file: %w", err)
}
} else {
break
}
}
}
return nil
}
Expand Down

0 comments on commit 8d0851f

Please sign in to comment.