From 6cd9db58d25a22ccb29641dd013ecce72b05e861 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 22 Jan 2024 22:36:29 +0100 Subject: [PATCH] Surefire way of no dupes --- internal/torrent/refresh.go | 48 +++++++++++++++++++++++-------------- internal/torrent/repair.go | 4 ++++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 9e6a835..76305b8 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -23,37 +23,42 @@ func (t *TorrentManager) RefreshTorrents() []string { var wg sync.WaitGroup allTorrents, _ := t.DirectoryMap.Get(INT_ALL) + expiredFixers := mapset.NewSet[string](t.fixers.Keys()...) for i := range instances { idx := i + expiredFixers.Remove(instances[idx].ID) wg.Add(1) _ = t.workerPool.Submit(func() { defer wg.Done() - if instances[idx].IsDone() && t.fixers.Has(instances[idx].ID) { + if !t.fixers.Has(instances[idx].ID) { + infoChan <- t.getMoreInfo(instances[idx]) + return + } else if instances[idx].IsDone() { fixer := instances[idx] torrent, _ := t.fixers.Pop(fixer.ID) t.log.Debugf("Fixer %s is done, let's check if it fixed torrent %s by redownloading", instances[idx].ID, t.GetKey(torrent)) brokenFiles := getBrokenFiles(torrent) info, err := t.redownloadTorrent(torrent, "") - if err == nil { - if info.IsDone() { - if t.isStillBroken(info, brokenFiles) { - t.log.Warnf("Fixer is done but torrent %s is still broken; let's keep the fixer", t.GetKey(torrent)) - infoChan <- t.getMoreInfo(fixer) - return - } else { - t.log.Infof("Fixer resolved issues for torrent %s, broken files are repaired", t.GetKey(torrent)) - } - } else { - t.log.Warnf("Torrent %s is still not done after redownload; likely the fixer did its job", t.GetKey(torrent)) - } - t.Api.DeleteTorrent(fixer.ID) // delete the fixer + if err != nil { + t.log.Warnf("Cannot redownload torrent %s after fixer is done: %v", t.GetKey(torrent), err) infoChan <- nil return - } else { - t.log.Warnf("Cannot redownload torrent %s after fixer is done: %v", t.GetKey(torrent), err) } - } else if !t.fixers.Has(instances[idx].ID) { - infoChan <- t.getMoreInfo(instances[idx]) + if info.IsDone() { + if t.isStillBroken(info, brokenFiles) { + t.log.Warnf("Fixer is done but torrent %s is still broken; let's keep the fixer", t.GetKey(torrent)) + infoChan <- t.getMoreInfo(fixer) + return + } else { + t.log.Infof("Fixer resolved issues for torrent %s, broken files are repaired", t.GetKey(torrent)) + } + } else { + t.log.Warnf("Torrent %s is still not done after redownload; likely the fixer did its job", t.GetKey(torrent)) + } + t.Api.DeleteTorrent(fixer.ID) // delete the fixer + infoChan <- nil + return + } }) } @@ -62,6 +67,13 @@ func (t *TorrentManager) RefreshTorrents() []string { close(infoChan) t.log.Debugf("Fetched info for %d torrents", len(instances)) + // delete expired fixers + expiredFixers.Each(func(fixerID string) bool { + t.log.Debugf("Deleting expired fixer %s", fixerID) + t.fixers.Remove(fixerID) + return false + }) + freshKeys := mapset.NewSet[string]() noInfoCount := 0 for info := range infoChan { diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 9f593b6..ebb7121 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -297,6 +297,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) return nil, fmt.Errorf("cannot start redownloading: %v", err) } @@ -304,6 +305,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) return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err) } @@ -319,6 +321,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) } if !isOkStatus { t.Api.DeleteTorrent(newTorrentID) + t.fixers.Set(newTorrentID, torrent) return nil, fmt.Errorf("the redownloaded torrent %s (id=%s) is in error state: %s", t.GetKey(torrent), newTorrentID, info.Status) } @@ -337,6 +340,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) 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) }