Optimizations

This commit is contained in:
Ben Sarmiento
2024-05-27 10:18:51 +02:00
parent 5c5a73e6e4
commit 4ec1c4496d
6 changed files with 76 additions and 75 deletions

View File

@@ -68,17 +68,24 @@ func (t *TorrentManager) persistBins() {
}
func (t *TorrentManager) setToBinImmediately(torrentId string) {
t.log.Debugf("Set to delete immediately: %s", torrentId)
t.log.Debugf("id=%s set to delete immediately", torrentId)
t.ImmediateBin.Add(torrentId)
t.persistBins()
}
func (t *TorrentManager) setToBinOnceDone(torrentId string) {
t.log.Debugf("Set to delete once completed: %s", torrentId)
t.log.Debugf("id=%s set to delete once it completes", torrentId)
t.OnceDoneBin.Add(torrentId)
t.persistBins()
}
func (t *TorrentManager) setXToBinOnceYDone(deleteId, completeId string) {
t.log.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) binImmediately(torrentId string) bool {
if t.ImmediateBin.Contains(torrentId) {
if err := t.api.DeleteTorrent(torrentId); err != nil {
@@ -86,42 +93,52 @@ func (t *TorrentManager) binImmediately(torrentId string) bool {
return false
}
t.ImmediateBin.Remove(torrentId)
t.persistBins()
return true
}
return false
}
func (t *TorrentManager) binOnceDoneErrorCheck(torrentId, status string) bool {
okStatuses := mapset.NewSet("downloading", "downloaded", "uploading", "queued", "compressing")
if !okStatuses.Contains(status) {
return t.binOnceDone(torrentId)
if status == "downloading" || status == "downloaded" || status == "uploading" || status == "queued" || status == "compressing" {
return false
}
return false
return t.binOnceDone(torrentId)
}
func (t *TorrentManager) binOnceDone(torrentId string) bool {
found := false
binnedIDs := t.OnceDoneBin.ToSlice()
// special case: xxx-yyy means if xxx is done, delete yyy
if t.OnceDoneBin.Contains(torrentId) {
if err := t.api.DeleteTorrent(torrentId); err != nil {
t.log.Errorf("Failed to delete torrent %s: %v", torrentId, err)
return false
}
t.OnceDoneBin.Remove(torrentId)
t.persistBins()
return true
}
// special case: yyy-xxx means if yyy is done, delete xxx
specialCase := fmt.Sprintf("%s-", torrentId)
for _, entry := range binnedIDs {
if !t.OnceDoneBin.Contains(specialCase) {
return false
}
hasError := false
t.OnceDoneBin.Each(func(entry string) bool {
if strings.Contains(entry, specialCase) {
idToDelete := strings.Split(entry, "-")[1]
if err := t.api.DeleteTorrent(idToDelete); err != nil {
t.log.Errorf("Failed to delete torrent %s: %v", idToDelete, err)
continue
hasError = true
return true
}
t.OnceDoneBin.Remove(entry)
found = true
} else if entry == torrentId {
if err := t.api.DeleteTorrent(torrentId); err != nil {
t.log.Errorf("Failed to delete torrent %s: %v", torrentId, err)
return false
}
t.OnceDoneBin.Remove(torrentId)
return true
}
return false
})
if !hasError {
t.OnceDoneBin.Remove(specialCase)
}
return found
t.persistBins()
return true
}