From 340735f8336611a4c3d7cbeef5bfb23d6ce60e5e Mon Sep 17 00:00:00 2001 From: Ben Adrian Sarmiento Date: Thu, 20 Jun 2024 06:36:08 +0200 Subject: [PATCH] Simplify bin --- internal/torrent/bins.go | 75 ++++++------------------------------- internal/torrent/manager.go | 10 ++--- internal/torrent/refresh.go | 19 +++++++--- internal/torrent/repair.go | 16 ++++---- 4 files changed, 36 insertions(+), 84 deletions(-) diff --git a/internal/torrent/bins.go b/internal/torrent/bins.go index 7b309b6..7c64ceb 100644 --- a/internal/torrent/bins.go +++ b/internal/torrent/bins.go @@ -1,9 +1,7 @@ package torrent import ( - "fmt" "os" - "strings" mapset "github.com/deckarep/golang-set/v2" ) @@ -60,31 +58,14 @@ func (t *TorrentManager) persistBins() { } } -func (t *TorrentManager) setToBinOnceDone(torrentId string) { - t.repairLog.Debugf("id=%s set to delete once it completes", torrentId) +func (t *TorrentManager) willDeleteOnceDone(torrentId string) { + t.repairLog.Debugf("Torrent id=%s will be deleted once it is completed", torrentId) t.OnceDoneBin.Add(torrentId) t.persistBins() } -func (t *TorrentManager) setXToBinOnceYDone(deleteId, completeId string) { - t.repairLog.Debugf("id=%s set to delete once id=%s completes", deleteId, completeId) - t.OnceDoneBin.Add(fmt.Sprintf("%s-", completeId)) - t.OnceDoneBin.Add(fmt.Sprintf("%s-%s", completeId, deleteId)) - t.persistBins() -} - func (t *TorrentManager) cleanupBins(freshIDs mapset.Set[string]) { t.OnceDoneBin.Clone().Each(func(entry string) bool { - // check for: delete x once y is done cases - 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.ContainsOne(ids[0]) || (ids[1] != "" && !freshIDs.ContainsOne(ids[1])) { - t.OnceDoneBin.Remove(entry) - } - return false - } // check for: delete once done cases if !freshIDs.ContainsOne(entry) { t.OnceDoneBin.Remove(entry) @@ -94,51 +75,19 @@ func (t *TorrentManager) cleanupBins(freshIDs mapset.Set[string]) { t.persistBins() } -// 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" || status == "waiting_files_selection" { - return false - } - return t.binOnceDone(torrentId, true) -} - -// binOnceDone checks if the torrent is in the OnceDoneBin and deletes it if it is. +// deleteOnceDone 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(completedTorrentId string, errorCheck bool) bool { - if t.OnceDoneBin.ContainsOne(completedTorrentId) { - t.DeleteByID(completedTorrentId) - t.OnceDoneBin.Remove(completedTorrentId) - if errorCheck { - t.repairLog.Infof("Bin: deleting torrent id=%s early because it has encountered an error", completedTorrentId) - } else { - t.repairLog.Debugf("Bin: done deletion of torrent %s", completedTorrentId) - } - t.persistBins() - return true +func (t *TorrentManager) deleteOnceDone(completedTorrentId string, failed bool) { + if !t.OnceDoneBin.ContainsOne(completedTorrentId) { + return } - // special case: yyy-xxx means if yyy is done, delete xxx - specialCase := fmt.Sprintf("%s-", completedTorrentId) - if !t.OnceDoneBin.ContainsOne(specialCase) { - return false + t.DeleteByID(completedTorrentId) + t.OnceDoneBin.Remove(completedTorrentId) + if failed { + t.repairLog.Infof("Deleting torrent id=%s early because it has encountered an error", completedTorrentId) + } else { + t.repairLog.Debugf("Deleting unnecessary torrent id=%s", completedTorrentId) } - t.deleteInfoFile(completedTorrentId) - t.OnceDoneBin.Remove(specialCase) - t.OnceDoneBin.Clone().Each(func(entry string) bool { - if strings.Contains(entry, specialCase) { - if errorCheck { - t.DeleteByID(completedTorrentId) - t.OnceDoneBin.Remove(entry) - t.repairLog.Infof("Bin: deleting torrent id=%s early because it has encountered an error", completedTorrentId) - } else { - idToDelete := strings.Split(entry, "-")[1] - t.DeleteByID(idToDelete) - t.OnceDoneBin.Remove(entry) - t.repairLog.Debugf("Bin: %s completed, done deletion of torrent %s", completedTorrentId, idToDelete) - } - } - return false - }) t.persistBins() - return true } diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index c1da455..9ce1765 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -53,9 +53,8 @@ type TorrentManager struct { repairRunning bool repairRunningMu sync.Mutex - OnceDoneBin mapset.Set[string] - DeleteOnCompletionBin cmap.ConcurrentMap[string, string] - hasFFprobe bool + OnceDoneBin mapset.Set[string] + hasFFprobe bool } // NewTorrentManager creates a new torrent manager @@ -86,9 +85,8 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w latestState: &LibraryState{log: log}, - OnceDoneBin: mapset.NewSet[string](), - DeleteOnCompletionBin: cmap.New[string](), - hasFFprobe: hasFFprobe, + OnceDoneBin: mapset.NewSet[string](), + hasFFprobe: hasFFprobe, } t.initializeBins() diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index 2d14f1b..8f3abad 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -38,13 +38,21 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) { idx := i t.workerPool.Submit(func() { defer wg.Done() - if t.binOnceDoneErrorCheck(instances[idx].ID, instances[idx].Status) || - instances[idx].Progress != 100 { + status := instances[idx].Status + if status != "downloading" && status != "downloaded" && status != "uploading" && status != "queued" && status != "compressing" && status != "waiting_files_selection" { + t.deleteOnceDone(instances[idx].ID, true) + } + if instances[idx].Progress != 100 { mergeChan <- nil return } tInfo := t.getMoreInfo(instances[idx]) + if tInfo == nil { + // just in case! + mergeChan <- nil + return + } torrent := t.convertToTorrent(tInfo) accessKey := t.GetKey(torrent) freshAccessKeys.Add(accessKey) @@ -113,12 +121,11 @@ func (t *TorrentManager) refreshTorrents(initialRun bool) { 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 (both are false), then we delete the info file - if !t.binOnceDone(torrentID, false) && !freshIDs.ContainsOne(torrentID) { + if !freshIDs.ContainsOne(torrentID) { t.deleteInfoFile(torrentID) + return false } + t.deleteOnceDone(torrentID, false) return false }) diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 039428c..8de410d 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -217,7 +217,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) { t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent)) info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)}) if info != nil { - t.setToBinOnceDone(info.ID) + t.willDeleteOnceDone(info.ID) } } } @@ -226,7 +226,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) { t.repairLog.Debugf("Extracting %d video file(s) from rar'ed torrent %s", len(videoFiles), t.GetKey(torrent)) info, _ := t.redownloadTorrent(torrent, videoFiles) if info != nil { - t.setToBinOnceDone(info.ID) + t.willDeleteOnceDone(info.ID) } } return @@ -241,14 +241,12 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) { info, err := t.redownloadTorrent(torrent, []string{}) // reinsert the whole torrent, passing empty selection if info != nil && info.Progress == 100 { if !t.isStillBroken(info, brokenFiles) { - t.repairLog.Infof("Successfully repaired torrent %s by redownloading whole torrent", t.GetKey(torrent)) // delete the torrents it replaced oldDownloadedIDs.Each(func(torrentID string) bool { - if torrentID != info.ID { - t.setXToBinOnceYDone(torrentID, info.ID) - } + t.DeleteByID(torrentID) return false }) + t.repairLog.Infof("Successfully repaired torrent %s by redownloading whole torrent", t.GetKey(torrent)) return } @@ -308,7 +306,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) { // once done, we can delete the newly downloaded torrents because we only need the links for _, newId := range newlyDownloadedIds { - t.setToBinOnceDone(newId) + t.willDeleteOnceDone(newId) } } @@ -418,7 +416,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool { t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent)) info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)}) if info != nil { - t.setToBinOnceDone(info.ID) + t.willDeleteOnceDone(info.ID) } } }) @@ -426,7 +424,7 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool { t.repairLog.Debugf("Extracting %d video file(s) from rar'ed torrent %s", len(videoFiles), t.GetKey(torrent)) info, _ := t.redownloadTorrent(torrent, videoFiles) if info != nil { - t.setToBinOnceDone(info.ID) + t.willDeleteOnceDone(info.ID) } } } else {