From 16e51d50b026ba4d766ea7b2eb0c70f8789f6c1b Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Tue, 28 May 2024 01:05:27 +0200 Subject: [PATCH] Cleanup bins --- internal/torrent/bins.go | 33 +++++++++++++++++++++++++++++++++ internal/torrent/refresh.go | 26 ++++++++++++++++---------- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/internal/torrent/bins.go b/internal/torrent/bins.go index 79dc534..4f4cab9 100644 --- a/internal/torrent/bins.go +++ b/internal/torrent/bins.go @@ -86,6 +86,34 @@ func (t *TorrentManager) setXToBinOnceYDone(deleteId, completeId string) { t.persistBins() } +func (t *TorrentManager) cleanupBins(freshIDs mapset.Set[string]) { + t.ImmediateBin.Clone().Each(func(entry string) bool { + if !freshIDs.Contains(entry) { + t.ImmediateBin.Remove(entry) + } + return false + }) + t.OnceDoneBin.Clone().Each(func(entry string) bool { + // check if the entry is a special case + if strings.Contains(entry, "-") { + // format is: id1-id2 or id1- + // if either id1 or id2 is not fresh, remove the entry + ids := strings.Split(entry, "-") + if !freshIDs.Contains(ids[0]) || (ids[1] != "" && !freshIDs.Contains(ids[1])) { + t.OnceDoneBin.Remove(entry) + } + return false + } + if !freshIDs.Contains(entry) { + t.OnceDoneBin.Remove(entry) + } + return false + }) + t.persistBins() +} + +// binImmediatelyErrorCheck checks if the torrent is in the ImmediateBin and deletes it if it is. +// returns true if the torrent was in the bin and was deleted, false otherwise func (t *TorrentManager) binImmediately(torrentId string) bool { if t.ImmediateBin.Contains(torrentId) { if err := t.api.DeleteTorrent(torrentId); err != nil { @@ -98,6 +126,7 @@ func (t *TorrentManager) binImmediately(torrentId string) bool { return false } +// binOnceDoneErrorCheck checks if the torrent is in error states and then checks if it should be deleted func (t *TorrentManager) binOnceDoneErrorCheck(torrentId, status string) bool { if status == "downloading" || status == "downloaded" || status == "uploading" || status == "queued" || status == "compressing" { return false @@ -105,11 +134,14 @@ func (t *TorrentManager) binOnceDoneErrorCheck(torrentId, status string) bool { return t.binOnceDone(torrentId) } +// binOnceDone checks if the torrent is in the OnceDoneBin and deletes it if it is. +// returns true if the torrent was in the bin and was deleted, false otherwise func (t *TorrentManager) binOnceDone(torrentId string) bool { if t.OnceDoneBin.Contains(torrentId) { if err := t.api.DeleteTorrent(torrentId); err != nil { t.repairLog.Warnf("Failed to delete torrent %s: %v", torrentId, err) } + t.deleteInfoFile(torrentId) t.OnceDoneBin.Remove(torrentId) t.persistBins() return true @@ -130,6 +162,7 @@ func (t *TorrentManager) binOnceDone(torrentId string) bool { } return false }) + t.deleteInfoFile(torrentId) t.OnceDoneBin.Remove(specialCase) t.persistBins() return true diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index ec8ea27..f07ee63 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -38,6 +38,7 @@ func (t *TorrentManager) refreshTorrents() []string { freshAccessKeys := mapset.NewSet[string]() for i := range instances { + freshIDs.Add(instances[i].ID) wg.Add(1) idx := i _ = t.workerPool.Submit(func() { @@ -52,8 +53,6 @@ func (t *TorrentManager) refreshTorrents() []string { return } - freshIDs.Add(instances[idx].ID) - tInfo := t.getMoreInfo(instances[idx]) torrent := t.convertToTorrent(tInfo) accessKey := t.GetKey(torrent) @@ -124,14 +123,21 @@ func (t *TorrentManager) refreshTorrents() []string { t.log.Infof("Compiled into %d unique torrents", allTorrents.Count()) - // delete info files that are no longer present - t.getInfoFiles().Each(func(path string) bool { - path = filepath.Base(path) - torrentID := strings.TrimSuffix(path, ".zurginfo") - if !t.binOnceDone(torrentID) && !freshIDs.Contains(torrentID) { - t.deleteInfoFile(torrentID) - } - return false + t.workerPool.Submit(func() { + // delete info files that are no longer present + t.getInfoFiles().Each(func(path string) bool { + path = filepath.Base(path) + torrentID := strings.TrimSuffix(path, ".zurginfo") + // if binOnceDone returns true, it means the info file is deleted + // if false, then we check if it's one of the torrents we just fetched + // if not, then we delete the info file + if !t.binOnceDone(torrentID) && !freshIDs.Contains(torrentID) { + t.deleteInfoFile(torrentID) + } + return false + }) + + t.cleanupBins(freshIDs) }) return updatedPaths.ToSlice()