From 9e44721fa29be8ef36fc5fb4be401655e5a728fc Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Mon, 27 May 2024 07:01:59 +0200 Subject: [PATCH] Fix issue with bins --- internal/torrent/bins.go | 63 +++++++++++++++---------------------- internal/torrent/refresh.go | 2 -- internal/torrent/repair.go | 20 ++---------- 3 files changed, 29 insertions(+), 56 deletions(-) diff --git a/internal/torrent/bins.go b/internal/torrent/bins.go index 52e5c02..115dfc9 100644 --- a/internal/torrent/bins.go +++ b/internal/torrent/bins.go @@ -41,6 +41,32 @@ func (t *TorrentManager) initializeBins() { t.log.Debugf("Bin immediately: %v", t.ImmediateBin.ToSlice()) t.log.Debugf("Bin once done: %v", t.OnceDoneBin.ToSlice()) } + +func (t *TorrentManager) persistBins() { + data := map[string]interface{}{ + "trash_bin": t.ImmediateBin.ToSlice(), // Assuming trashBin is a mapset.Set[string] + "repair_bin": t.OnceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string] + } + + jsonData, err := json.Marshal(data) + if err != nil { + t.log.Errorf("Failed to marshal bin data: %v", err) + return + } + + file, err := os.Create(BINS_FILE) + if err != nil { + t.log.Errorf("Failed to create bins.json file: %v", err) + return + } + defer file.Close() + + _, err = file.Write(jsonData) + if err != nil { + t.log.Errorf("Failed to write to bins.json file: %v", err) + } +} + func (t *TorrentManager) setToBinImmediately(torrentId string) { t.log.Debugf("Set to delete immediately: %s", torrentId) t.ImmediateBin.Add(torrentId) @@ -99,40 +125,3 @@ func (t *TorrentManager) binOnceDone(torrentId string) bool { return found } - -func (t *TorrentManager) persistBins() { - data := map[string]interface{}{ - "trash_bin": t.ImmediateBin.ToSlice(), // Assuming trashBin is a mapset.Set[string] - "repair_bin": t.OnceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string] - } - - jsonData, err := json.Marshal(data) - if err != nil { - t.log.Errorf("Failed to marshal bin data: %v", err) - return - } - - file, err := os.Create(BINS_FILE) - if err != nil { - t.log.Errorf("Failed to create bins.json file: %v", err) - return - } - defer file.Close() - - _, err = file.Write(jsonData) - if err != nil { - t.log.Errorf("Failed to write to bins.json file: %v", err) - } -} - -func (t *TorrentManager) cleanupBins(freshIDs mapset.Set[string]) { - t.ImmediateBin.Difference(freshIDs).Each(func(id string) bool { - t.ImmediateBin.Remove(id) - return false - }) - t.OnceDoneBin.Difference(freshIDs).Each(func(id string) bool { - t.OnceDoneBin.Remove(id) - return false - }) - t.persistBins() -} diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index f025b63..f592939 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -145,8 +145,6 @@ func (t *TorrentManager) refreshTorrents() []string { return false }) - t.cleanupBins(freshIDs) - return updatedPaths.ToSlice() } diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 6438433..1202ddc 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -196,7 +196,7 @@ func (t *TorrentManager) repair(torrent *Torrent) { // delete the torrents it replaced torrent.DownloadedIDs.Each(func(torrentID string) bool { if torrentID != info.ID { - t.setToBinImmediately(torrentID) + t.setToBinOnceDone(fmt.Sprintf("%s-%s", info.ID, torrentID)) } return false }) @@ -240,9 +240,9 @@ func (t *TorrentManager) repair(torrent *Torrent) { batchNum := 1 brokenFileIDs := getFileIDs(brokenFiles) var group []string - for _, fileIDStr := range brokenFileIDs { + for i, fileIDStr := range brokenFileIDs { group = append(group, fileIDStr) - if len(group) >= 100 { + if len(group) >= 100 || i == len(brokenFileIDs)-1 { t.repairLog.Debugf("Downloading batch %d/%d of broken files of torrent %s", batchNum, totalBatches, t.GetKey(torrent)) batchNum++ redownloadedInfo, err := t.redownloadTorrent(torrent, group) @@ -259,20 +259,6 @@ func (t *TorrentManager) repair(torrent *Torrent) { } } - if len(group) > 0 { - t.repairLog.Debugf("Downloading batch %d/%d of broken files of torrent %s", batchNum, totalBatches, t.GetKey(torrent)) - redownloadedInfo, err := t.redownloadTorrent(torrent, group) - if err != nil { - t.repairLog.Warnf("Cannot repair torrent %s by downloading broken files (error=%v) giving up", t.GetKey(torrent), err) - // delete the newly downloaded torrents because the operation failed - for _, newId := range newlyDownloadedIds { - t.setToBinImmediately(newId) - } - return - } - newlyDownloadedIds = append(newlyDownloadedIds, redownloadedInfo.ID) - } - // once done, we can delete the newly downloaded torrents because we only need the links for _, newId := range newlyDownloadedIds { t.setToBinOnceDone(newId)