Cleanup bins

This commit is contained in:
Ben Sarmiento
2024-05-28 01:05:27 +02:00
parent 5e204695df
commit 16e51d50b0
2 changed files with 49 additions and 10 deletions

View File

@@ -86,6 +86,34 @@ func (t *TorrentManager) setXToBinOnceYDone(deleteId, completeId string) {
t.persistBins()
}
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
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])) {
t.OnceDoneBin.Remove(entry)
}
return false
}
if !freshIDs.Contains(entry) {
t.OnceDoneBin.Remove(entry)
}
return false
})
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 {
@@ -98,6 +126,7 @@ func (t *TorrentManager) binImmediately(torrentId string) bool {
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" {
return false
@@ -105,11 +134,14 @@ func (t *TorrentManager) binOnceDoneErrorCheck(torrentId, status string) bool {
return t.binOnceDone(torrentId)
}
// 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(torrentId string) bool {
if t.OnceDoneBin.Contains(torrentId) {
if err := t.api.DeleteTorrent(torrentId); err != nil {
t.repairLog.Warnf("Failed to delete torrent %s: %v", torrentId, err)
}
t.deleteInfoFile(torrentId)
t.OnceDoneBin.Remove(torrentId)
t.persistBins()
return true
@@ -130,6 +162,7 @@ func (t *TorrentManager) binOnceDone(torrentId string) bool {
}
return false
})
t.deleteInfoFile(torrentId)
t.OnceDoneBin.Remove(specialCase)
t.persistBins()
return true

View File

@@ -38,6 +38,7 @@ func (t *TorrentManager) refreshTorrents() []string {
freshAccessKeys := mapset.NewSet[string]()
for i := range instances {
freshIDs.Add(instances[i].ID)
wg.Add(1)
idx := i
_ = t.workerPool.Submit(func() {
@@ -52,8 +53,6 @@ func (t *TorrentManager) refreshTorrents() []string {
return
}
freshIDs.Add(instances[idx].ID)
tInfo := t.getMoreInfo(instances[idx])
torrent := t.convertToTorrent(tInfo)
accessKey := t.GetKey(torrent)
@@ -124,14 +123,21 @@ func (t *TorrentManager) refreshTorrents() []string {
t.log.Infof("Compiled into %d unique torrents", allTorrents.Count())
// delete info files that are no longer present
t.getInfoFiles().Each(func(path string) bool {
path = filepath.Base(path)
torrentID := strings.TrimSuffix(path, ".zurginfo")
if !t.binOnceDone(torrentID) && !freshIDs.Contains(torrentID) {
t.deleteInfoFile(torrentID)
}
return false
t.workerPool.Submit(func() {
// delete info files that are no longer present
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, then we delete the info file
if !t.binOnceDone(torrentID) && !freshIDs.Contains(torrentID) {
t.deleteInfoFile(torrentID)
}
return false
})
t.cleanupBins(freshIDs)
})
return updatedPaths.ToSlice()