From 52a31abe35b03e2e50d649569e8b47da0fadfffd Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Wed, 24 Jan 2024 12:23:50 +0100 Subject: [PATCH] Fix duplication --- internal/torrent/manager.go | 2 ++ internal/torrent/refresh.go | 13 ++++++++++++- internal/torrent/repair.go | 16 +++++++++------- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index d1276ee..9ca1121 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -29,6 +29,7 @@ type TorrentManager struct { DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download] fixers cmap.ConcurrentMap[string, *Torrent] repairs mapset.Set[string] + ensureDelete mapset.Set[string] allAccessKeys mapset.Set[string] latestState *LibraryState requiredVersion string @@ -49,6 +50,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w DownloadMap: cmap.New[*realdebrid.Download](), fixers: cmap.New[*Torrent](), repairs: mapset.NewSet[string](), + ensureDelete: mapset.NewSet[string](), allAccessKeys: mapset.NewSet[string](), latestState: &LibraryState{}, requiredVersion: "24.01.2024", diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index b2a406d..841c8af 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -23,7 +23,7 @@ func (t *TorrentManager) RefreshTorrents() []string { var wg sync.WaitGroup allTorrents, _ := t.DirectoryMap.Get(INT_ALL) - expiredFixers := mapset.NewSet[string](t.fixers.Keys()...) + expiredFixers := t.ensureDelete.Clone() for i := range instances { idx := i expiredFixers.Remove(instances[idx].ID) @@ -71,6 +71,17 @@ func (t *TorrentManager) RefreshTorrents() []string { expiredFixers.Each(func(fixerID string) bool { t.log.Debugf("Deleting expired fixer %s", fixerID) t.fixers.Remove(fixerID) + t.ensureDelete.Remove(fixerID) + return false + }) + // ensure delete + infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) + t.ensureDelete.Each(func(fixerID string) bool { + if torrent, exists := infoCache.Get(fixerID); exists && torrent.AnyInProgress() { + return false + } + t.log.Debugf("Ensuring that torrent id=%s is deleted", fixerID) + t.Api.DeleteTorrent(fixerID) return false }) diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 5669895..f008e2d 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -210,7 +210,7 @@ func (t *TorrentManager) repair(torrent *Torrent) { } else if info != nil && info.ID != "" { t.log.Warnf("Torrent %s is still broken after repair_method#1, cleaning up (deleting ID=%s)", t.GetKey(torrent), info.ID) t.Api.DeleteTorrent(info.ID) - t.fixers.Set(info.ID, torrent) + t.ensureDelete.Add(info.ID) } if torrent.UnrepairableReason != "" { @@ -264,7 +264,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) err = t.Api.SelectTorrentFiles(newTorrentID, brokenFiles) if err != nil { t.Api.DeleteTorrent(newTorrentID) - t.fixers.Set(newTorrentID, torrent) + t.ensureDelete.Add(newTorrentID) return nil, fmt.Errorf("cannot start redownloading: %v", err) } @@ -272,7 +272,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) info, err := t.Api.GetTorrentInfo(newTorrentID) if err != nil { t.Api.DeleteTorrent(newTorrentID) - t.fixers.Set(newTorrentID, torrent) + t.ensureDelete.Add(newTorrentID) return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err) } @@ -288,7 +288,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) } if !isOkStatus { t.Api.DeleteTorrent(newTorrentID) - t.fixers.Set(newTorrentID, torrent) + t.ensureDelete.Add(newTorrentID) return nil, fmt.Errorf("the redownloaded torrent %s (id=%s) is in error state: %s", t.GetKey(torrent), newTorrentID, info.Status) } @@ -297,10 +297,11 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) if len(oldTorrentIDs) > 0 { for _, id := range oldTorrentIDs { t.Api.DeleteTorrent(id) - t.fixers.Set(id, torrent) + t.ensureDelete.Add(id) } } else { t.fixers.Set(newTorrentID, torrent) + t.ensureDelete.Add(newTorrentID) } return info, nil } @@ -308,7 +309,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) brokenCount := len(strings.Split(brokenFiles, ",")) if len(info.Links) != brokenCount { t.Api.DeleteTorrent(newTorrentID) - t.fixers.Set(newTorrentID, torrent) + t.ensureDelete.Add(newTorrentID) return nil, fmt.Errorf("it did not fix the issue for %s (id=%s), only got %d files but we need %d, undoing", t.GetKey(torrent), info.ID, len(info.Links), brokenCount) } @@ -316,10 +317,11 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) // only triggered when brokenFiles == "" for _, id := range oldTorrentIDs { t.Api.DeleteTorrent(id) - t.fixers.Set(id, torrent) + t.ensureDelete.Add(id) } } else { t.fixers.Set(newTorrentID, torrent) + t.ensureDelete.Add(newTorrentID) } return info, nil }