Remove immediate bin

This commit is contained in:
Ben Adrian Sarmiento
2024-06-18 23:43:31 +02:00
parent 34a7d6a432
commit acc9b69b5a
8 changed files with 58 additions and 87 deletions

View File

@@ -31,17 +31,14 @@ func (t *TorrentManager) initializeBins() {
return
}
t.ImmediateBin = mapset.NewSet[string](data["trash_bin"]...)
t.OnceDoneBin = mapset.NewSet[string](data["repair_bin"]...)
t.repairLog.Debugf("Bin immediately: %v", t.ImmediateBin.ToSlice())
t.repairLog.Debugf("Bin once done: %v", t.OnceDoneBin.ToSlice())
t.repairLog.Debugf("These IDs will be deleted after completion: %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]
"repair_bin": t.OnceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string]
}
jsonData, err := json.Marshal(data)
@@ -63,12 +60,6 @@ func (t *TorrentManager) persistBins() {
}
}
func (t *TorrentManager) setToBinImmediately(torrentId string) {
t.repairLog.Debugf("id=%s set to delete immediately", torrentId)
t.ImmediateBin.Add(torrentId)
t.persistBins()
}
func (t *TorrentManager) setToBinOnceDone(torrentId string) {
t.repairLog.Debugf("id=%s set to delete once it completes", torrentId)
t.OnceDoneBin.Add(torrentId)
@@ -83,24 +74,19 @@ func (t *TorrentManager) setXToBinOnceYDone(deleteId, completeId string) {
}
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
// 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.Contains(ids[0]) || (ids[1] != "" && !freshIDs.Contains(ids[1])) {
if !freshIDs.ContainsOne(ids[0]) || (ids[1] != "" && !freshIDs.ContainsOne(ids[1])) {
t.OnceDoneBin.Remove(entry)
}
return false
}
if !freshIDs.Contains(entry) {
// check for: delete once done cases
if !freshIDs.ContainsOne(entry) {
t.OnceDoneBin.Remove(entry)
}
return false
@@ -108,41 +94,22 @@ func (t *TorrentManager) cleanupBins(freshIDs mapset.Set[string]) {
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 {
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
}
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" || status == "waiting_files_selection" {
return false
}
t.repairLog.Infof("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, 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)
if t.OnceDoneBin.ContainsOne(completedTorrentId) {
t.DeleteByID(completedTorrentId)
t.OnceDoneBin.Remove(completedTorrentId)
if errorCheck {
t.repairLog.Errorf("Bin: error deletion of torrent %s", completedTorrentId)
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)
}
@@ -152,7 +119,7 @@ func (t *TorrentManager) binOnceDone(completedTorrentId string, errorCheck bool)
// special case: yyy-xxx means if yyy is done, delete xxx
specialCase := fmt.Sprintf("%s-", completedTorrentId)
if !t.OnceDoneBin.Contains(specialCase) {
if !t.OnceDoneBin.ContainsOne(specialCase) {
return false
}
t.deleteInfoFile(completedTorrentId)
@@ -160,16 +127,12 @@ func (t *TorrentManager) binOnceDone(completedTorrentId string, errorCheck bool)
t.OnceDoneBin.Clone().Each(func(entry string) bool {
if strings.Contains(entry, specialCase) {
if errorCheck {
if err := t.api.DeleteTorrent(completedTorrentId); err != nil {
t.repairLog.Warnf("Failed to delete torrent %s: %v", completedTorrentId, err)
}
t.DeleteByID(completedTorrentId)
t.OnceDoneBin.Remove(entry)
t.repairLog.Errorf("Bin: error deletion of torrent %s", completedTorrentId)
t.repairLog.Infof("Bin: deleting torrent id=%s early because it has encountered an error", 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.DeleteByID(idToDelete)
t.OnceDoneBin.Remove(entry)
t.repairLog.Debugf("Bin: %s completed, done deletion of torrent %s", completedTorrentId, idToDelete)
}