Fix repairs for rared torrents

This commit is contained in:
Ben Adrian Sarmiento
2024-06-06 01:49:17 +02:00
parent cc19423420
commit 5a6b6b1546
5 changed files with 52 additions and 36 deletions

View File

@@ -13,7 +13,7 @@ const BINS_FILE = "data/bins.json"
// initializeBins reads from bins.json and assigns values to t.trashBin and t.repairBin
func (t *TorrentManager) initializeBins() {
if _, err := os.Stat(BINS_FILE); os.IsNotExist(err) {
t.repairLog.Warn("data/bins.json does not exist. Initializing empty bins.")
t.repairLog.Info("data/bins.json does not exist. Initializing empty bins.")
t.ImmediateBin = mapset.NewSet[string]()
t.OnceDoneBin = mapset.NewSet[string]()
return
@@ -120,6 +120,7 @@ func (t *TorrentManager) binImmediately(torrentId string) bool {
t.repairLog.Warnf("Failed to delete torrent %s: %v", torrentId, err)
}
t.ImmediateBin.Remove(torrentId)
t.repairLog.Debugf("Bin: immediate deletion of torrent %s", torrentId)
t.persistBins()
return true
}
@@ -128,21 +129,27 @@ func (t *TorrentManager) binImmediately(torrentId string) bool {
// 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" {
if status == "downloading" || status == "downloaded" || status == "uploading" || status == "queued" || status == "compressing" || status == "waiting_files_selection" {
return false
}
return t.binOnceDone(torrentId)
t.repairLog.Errorf("Bin: error status=%s, checking if %s should be deleted", status, torrentId)
return t.binOnceDone(torrentId, true)
}
// 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(completedTorrentId string) bool {
func (t *TorrentManager) binOnceDone(completedTorrentId string, errorCheck bool) bool {
if t.OnceDoneBin.Contains(completedTorrentId) {
if err := t.api.DeleteTorrent(completedTorrentId); err != nil {
t.repairLog.Warnf("Failed to delete torrent %s: %v", completedTorrentId, err)
}
t.deleteInfoFile(completedTorrentId)
t.OnceDoneBin.Remove(completedTorrentId)
if errorCheck {
t.repairLog.Errorf("Bin: error deletion of torrent %s", completedTorrentId)
} else {
t.repairLog.Debugf("Bin: done deletion of torrent %s", completedTorrentId)
}
t.persistBins()
return true
}
@@ -152,14 +159,22 @@ func (t *TorrentManager) binOnceDone(completedTorrentId string) bool {
if !t.OnceDoneBin.Contains(specialCase) {
return false
}
t.OnceDoneBin.Remove(specialCase)
t.OnceDoneBin.Clone().Each(func(entry string) bool {
if strings.Contains(entry, specialCase) {
idToDelete := strings.Split(entry, "-")[1]
if err := t.api.DeleteTorrent(idToDelete); err != nil {
t.repairLog.Warnf("Failed to delete torrent %s: %v", idToDelete, err)
if errorCheck {
if err := t.api.DeleteTorrent(completedTorrentId); err != nil {
t.repairLog.Warnf("Failed to delete torrent %s: %v", completedTorrentId, err)
}
t.OnceDoneBin.Remove(entry)
t.repairLog.Errorf("Bin: error deletion of torrent %s", completedTorrentId)
} else {
idToDelete := strings.Split(entry, "-")[1]
if err := t.api.DeleteTorrent(idToDelete); err != nil {
t.repairLog.Warnf("Failed to delete torrent %s: %v", idToDelete, err)
}
t.OnceDoneBin.Remove(entry)
t.repairLog.Debugf("Bin: %s completed, done deletion of torrent %s", completedTorrentId, idToDelete)
}
t.OnceDoneBin.Remove(entry)
}
return false
})