Fix the fixer

This commit is contained in:
Ben Sarmiento
2024-02-05 00:42:28 +01:00
parent 306d415acc
commit 6726b1e54e
3 changed files with 22 additions and 14 deletions

View File

@@ -4,6 +4,7 @@ import (
"io"
"os"
"github.com/debridmediamanager/zurg/pkg/realdebrid"
cmap "github.com/orcaman/concurrent-map/v2"
)
@@ -20,36 +21,43 @@ func (t *TorrentManager) fixerAddCommand(trigger, command string) {
t.writeFixersToFile()
}
func (t *TorrentManager) handleFixers() {
func (t *TorrentManager) handleFixers(instances []realdebrid.Torrent) {
var toDelete []string
var toRedownload []*Torrent
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
infoCache.IterCb(func(id string, torrent *Torrent) {
if !t.fixers.Has(id) || torrent.AnyInProgress() {
for _, instance := range instances {
id := instance.ID
if !t.fixers.Has(id) {
return
}
command, _ := t.fixers.Pop(id) // delete the fixer if it's done
switch command {
case "replaced": // id is old torrent id
t.log.Debugf("Deleting old id=%s because it's redundant to fixed %s ", id, t.GetKey(torrent))
t.log.Debugf("Deleting old id=%s because it's redundant to fixed %s ", id, instance.Name)
toDelete = append(toDelete, id)
case "download_failed": // id is failed fixer id
t.log.Debugf("Deleting failed fixer id=%s of torrent %s", id, t.GetKey(torrent))
t.log.Debugf("Deleting failed fixer id=%s of torrent %s", id, instance.Name)
toDelete = append(toDelete, id)
case "repaired": // id is fixer id
case "repaired": // this torrent contains broken files
if instance.Progress != 100 {
return
}
torrent := t.getMoreInfo(instance)
t.log.Debugf("Repairing torrent %s again now that fixer id=%s is done", t.GetKey(torrent), id)
repairMe, _ := allTorrents.Get(t.GetKey(torrent))
repairMe.DownloadedIDs.Remove(id)
toDelete = append(toDelete, id)
t.TriggerRepair(repairMe)
toRedownload = append(toRedownload, repairMe)
}
})
}
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
for _, id := range toDelete {
t.Api.DeleteTorrent(id)
infoCache.Remove(id)
t.deleteTorrentFile(id)
}
for _, torrent := range toRedownload {
t.redownloadTorrent(torrent, "")
}
// remove expired fixers
var expired []string
t.fixers.IterCb(func(trigger string, command string) {