Simplify bin

This commit is contained in:
Ben Adrian Sarmiento
2024-06-20 06:36:08 +02:00
parent bc04737ca7
commit 340735f833
4 changed files with 36 additions and 84 deletions

View File

@@ -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
}