diff --git a/internal/config/config.go b/internal/config/config.go index 84bca76..13750a4 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -12,6 +12,13 @@ import ( "sync" ) +type RepairStrategy string + +const ( + RepairStrategyPerFile RepairStrategy = "per_file" + RepairStrategyPerTorrent RepairStrategy = "per_torrent" +) + var ( instance *Config once sync.Once @@ -60,13 +67,14 @@ type Arr struct { } type Repair struct { - Enabled bool `json:"enabled,omitempty"` - Interval string `json:"interval,omitempty"` - ZurgURL string `json:"zurg_url,omitempty"` - AutoProcess bool `json:"auto_process,omitempty"` - UseWebDav bool `json:"use_webdav,omitempty"` - Workers int `json:"workers,omitempty"` - ReInsert bool `json:"reinsert,omitempty"` + Enabled bool `json:"enabled,omitempty"` + Interval string `json:"interval,omitempty"` + ZurgURL string `json:"zurg_url,omitempty"` + AutoProcess bool `json:"auto_process,omitempty"` + UseWebDav bool `json:"use_webdav,omitempty"` + Workers int `json:"workers,omitempty"` + ReInsert bool `json:"reinsert,omitempty"` + Strategy RepairStrategy `json:"strategy,omitempty"` } type Auth struct { @@ -352,6 +360,11 @@ func (c *Config) setDefaults() { c.URLBase += "/" } + // Set repair defaults + if c.Repair.Strategy == "" { + c.Repair.Strategy = RepairStrategyPerTorrent + } + // Load the auth file c.Auth = c.GetAuth() } diff --git a/pkg/arr/arr.go b/pkg/arr/arr.go index 9b77cec..5934d5f 100644 --- a/pkg/arr/arr.go +++ b/pkg/arr/arr.go @@ -115,8 +115,10 @@ func (a *Arr) Validate() error { if err != nil { return err } - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("arr test failed: %s", resp.Status) + defer resp.Body.Close() + // If response is not 200 or 404(this is the case for Lidarr, etc), return an error + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotFound { + return fmt.Errorf("failed to validate arr %s: %s", a.Name, resp.Status) } return nil } diff --git a/pkg/debrid/store/repair.go b/pkg/debrid/store/repair.go index b807f27..dccf0d8 100644 --- a/pkg/debrid/store/repair.go +++ b/pkg/debrid/store/repair.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/sirrobot01/decypharr/internal/config" "github.com/sirrobot01/decypharr/internal/utils" "github.com/sirrobot01/decypharr/pkg/debrid/types" "sync" @@ -60,6 +61,7 @@ func (c *Cache) markAsSuccessfullyReinserted(torrentId string) { func (c *Cache) GetBrokenFiles(t *CachedTorrent, filenames []string) []string { files := make(map[string]types.File) + repairStrategy := config.Get().Repair.Strategy brokenFiles := make([]string, 0) if len(filenames) > 0 { for name, f := range t.Files { @@ -93,6 +95,10 @@ func (c *Cache) GetBrokenFiles(t *CachedTorrent, filenames []string) []string { ctx, cancel := context.WithCancel(context.Background()) defer cancel() + // Use a mutex to protect brokenFiles slice and torrent-wide failure flag + var mu sync.Mutex + torrentWideFailed := false + wg.Add(len(files)) for _, f := range files { @@ -106,14 +112,33 @@ func (c *Cache) GetBrokenFiles(t *CachedTorrent, filenames []string) []string { } if f.Link == "" { - cancel() + mu.Lock() + if repairStrategy == config.RepairStrategyPerTorrent { + torrentWideFailed = true + mu.Unlock() + cancel() // Signal all other goroutines to stop + return + } else { + // per_file strategy - only mark this file as broken + brokenFiles = append(brokenFiles, f.Name) + } + mu.Unlock() return } if err := c.client.CheckLink(f.Link); err != nil { if errors.Is(err, utils.HosterUnavailableError) { - cancel() // Signal all other goroutines to stop - return + mu.Lock() + if repairStrategy == config.RepairStrategyPerTorrent { + torrentWideFailed = true + mu.Unlock() + cancel() // Signal all other goroutines to stop + return + } else { + // per_file strategy - only mark this file as broken + brokenFiles = append(brokenFiles, f.Name) + } + mu.Unlock() } } }(f) @@ -121,12 +146,14 @@ func (c *Cache) GetBrokenFiles(t *CachedTorrent, filenames []string) []string { wg.Wait() - // If context was cancelled, mark all files as broken - if ctx.Err() != nil { + // Handle the result based on strategy + if repairStrategy == config.RepairStrategyPerTorrent && torrentWideFailed { + // Mark all files as broken for per_torrent strategy for _, f := range files { brokenFiles = append(brokenFiles, f.Name) } } + // For per_file strategy, brokenFiles already contains only the broken ones // Try to reinsert the torrent if it's broken if len(brokenFiles) > 0 && t.Torrent != nil { diff --git a/pkg/web/templates/config.html b/pkg/web/templates/config.html index eb1999e..45dad9e 100644 --- a/pkg/web/templates/config.html +++ b/pkg/web/templates/config.html @@ -337,6 +337,14 @@