Repair adjustments 2

This commit is contained in:
Ben Sarmiento
2024-05-01 14:06:54 +02:00
parent 130cc0d7b3
commit b6b59b22e6
6 changed files with 71 additions and 85 deletions

View File

@@ -15,9 +15,9 @@ import (
// id_trigger: this means a specific torrent id's completion
// commands: delete | repair
func (t *TorrentManager) fixerAddCommand(trigger, command string) {
t.log.Debugf("Adding fixer command: %s %s", trigger, command)
t.fixers.Set(trigger, command)
func (t *TorrentManager) registerFixer(torrentId, command string) {
t.log.Debugf("Adding fixer command: %s %s", torrentId, command)
t.fixers.Set(torrentId, command)
t.writeFixersToFile()
}
@@ -27,59 +27,37 @@ func (t *TorrentManager) processFixers(instances []realdebrid.Torrent) {
var toRedownload []*Torrent
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
for _, instance := range instances {
id := instance.ID
if !t.fixers.Has(id) {
if !t.fixers.Has(instance.ID) {
continue
}
command, _ := t.fixers.Pop(id) // delete the fixer if it's done
oldTorrentId := instance.ID
command, _ := t.fixers.Pop(oldTorrentId) // 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, instance.Name)
toDelete = append(toDelete, id)
t.log.Debugf("Deleting old id=%s because it's redundant to fixed torrent %s ", oldTorrentId, instance.Name)
toDelete = append(toDelete, oldTorrentId)
continue
case "download_failed": // id is failed fixer id
t.log.Debugf("Deleting failed fixer id=%s of torrent %s", id, instance.Name)
toDelete = append(toDelete, id)
t.log.Debugf("Deleting failed fixer id=%s of torrent %s", oldTorrentId, instance.Name)
toDelete = append(toDelete, oldTorrentId)
continue
case "repaired": // this torrent contains broken files
if instance.Progress != 100 {
t.fixers.Set(id, command) // requeue the fixer
t.fixers.Set(oldTorrentId, command) // requeue the fixer, it's not done yet
continue
}
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))
fixedTorrent := t.getMoreInfo(instance)
t.log.Debugf("Repairing torrent %s again now that fixer id=%s is done", t.GetKey(fixedTorrent), oldTorrentId)
repairMe, _ := allTorrents.Get(t.GetKey(fixedTorrent))
toRedownload = append(toRedownload, repairMe)
toDelete = append(toDelete, id)
toDelete = append(toDelete, oldTorrentId)
continue
}
// a new case: repaired_with:<id>
// if strings.HasPrefix(command, "repaired_with:") {
// if instance.Progress != 100 {
// t.fixers.Set(id, command) // requeue the fixer
// continue
// }
// otherId := strings.TrimPrefix(command, "repaired_with:")
// for _, instance2 := range instances {
// if instance2.ID == otherId {
// if instance2.Progress != 100 {
// t.fixers.Set(id, command) // requeue the fixer
// break
// }
// torrent := t.getMoreInfo(instance2)
// t.log.Debugf("Repairing torrent %s again now that fixers ids=%s and %s are done", t.GetKey(torrent), id, otherId)
// repairMe, _ := allTorrents.Get(t.GetKey(torrent))
// toRedownload = append(toRedownload, repairMe)
// toDelete = append(toDelete, id, otherId)
// break
// }
// }
// continue
// }
}
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)

View File

@@ -20,6 +20,7 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
t.log.Warnf("Cannot get torrents: %v", err)
return nil
}
t.log.Infof("Fetched %d torrents", len(instances))
infoChan := make(chan *Torrent, len(instances))
var wg sync.WaitGroup

View File

@@ -211,16 +211,21 @@ func (t *TorrentManager) repair(torrent *Torrent) {
oldTorrentIDs := torrent.DownloadedIDs.Union(torrent.InProgressIDs).ToSlice()
newlyDownloadedIds := make([]string, 0)
group := make([]*File, 0)
for _, file := range brokenFiles {
group = append(group, file)
if len(group) == 100 {
if len(group) >= 200 {
brokenFileIDs := getFileIDs(group)
_, err := t.redownloadTorrent(torrent, brokenFileIDs)
redownloadedInfo, err := t.redownloadTorrent(torrent, brokenFileIDs)
if err != nil {
t.log.Warnf("Cannot repair torrent %s by downloading broken files (error=%s) giving up", t.GetKey(torrent), err.Error())
for _, newId := range newlyDownloadedIds {
t.registerFixer(newId, "download_failed")
}
return
}
newlyDownloadedIds = append(newlyDownloadedIds, redownloadedInfo.ID)
group = make([]*File, 0)
}
}
@@ -230,12 +235,15 @@ func (t *TorrentManager) repair(torrent *Torrent) {
_, err := t.redownloadTorrent(torrent, brokenFileIDs)
if err != nil {
t.log.Warnf("Cannot repair torrent %s by downloading broken files (error=%s) giving up", t.GetKey(torrent), err.Error())
for _, newId := range newlyDownloadedIds {
t.registerFixer(newId, "download_failed")
}
return
}
}
for _, oldId := range oldTorrentIDs {
t.fixerAddCommand(oldId, "replaced")
t.registerFixer(oldId, "replaced")
}
}
@@ -369,51 +377,59 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
newTorrentID := resp.ID
// sleep for 1 second to let RD process the magnet
time.Sleep(1 * time.Second)
time.Sleep(10 * time.Second)
// select files
err = t.Api.SelectTorrentFiles(newTorrentID, selection)
if err != nil {
t.fixerAddCommand(newTorrentID, "download_failed")
return nil, fmt.Errorf("cannot start redownloading: %v", err)
}
var info *realdebrid.TorrentInfo
for {
// select files
err = t.Api.SelectTorrentFiles(newTorrentID, selection)
if err != nil {
t.registerFixer(newTorrentID, "download_failed")
return nil, fmt.Errorf("cannot start redownloading: %v", err)
}
// sleep for 5 second to let RD process the magnet
time.Sleep(10 * time.Second)
// sleep for 1 second to let RD process the magnet
time.Sleep(1 * time.Second)
// see if the torrent is ready
info, err = t.Api.GetTorrentInfo(newTorrentID)
if err != nil {
t.registerFixer(newTorrentID, "download_failed")
return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err)
}
// see if the torrent is ready
info, err := t.Api.GetTorrentInfo(newTorrentID)
if err != nil {
t.fixerAddCommand(newTorrentID, "download_failed")
return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err)
if info.Status != "magnet_conversion" && info.Status != "waiting_files_selection" {
break
}
}
// documented status: magnet_error, magnet_conversion, waiting_files_selection, queued, downloading, downloaded, error, virus, compressing, uploading, dead
okStatuses := []string{"downloading", "downloaded", "uploading"}
// not compressing because we need playable files
isOkStatus := false
okStatuses := []string{"downloading", "downloaded", "uploading", "queued", "compressing"}
// not compressing because we need playable files
for _, status := range okStatuses {
if info.Status == status {
isOkStatus = true
break
}
}
if !isOkStatus {
t.fixerAddCommand(info.ID, "download_failed")
return nil, fmt.Errorf("the redownloaded torrent %s (id=%s) is in a non-OK state: %s", t.GetKey(torrent), info.ID, info.Status)
t.registerFixer(info.ID, "download_failed")
return nil, fmt.Errorf("the redownloaded torrent %s is in a non-OK state: %s", t.GetKey(torrent), info.Status)
}
// check if incorrect number of links
selectionCount := len(strings.Split(selection, ","))
if info.Progress == 100 && len(info.Links) != selectionCount {
t.fixerAddCommand(newTorrentID, "download_failed")
return nil, fmt.Errorf("it did not fix the issue for %s (id=%s), only got %d files but we need %d, undoing", t.GetKey(torrent), info.ID, len(info.Links), selectionCount)
t.registerFixer(newTorrentID, "download_failed")
return nil, fmt.Errorf("torrent %s only got %d links but we need %d", t.GetKey(torrent), len(info.Links), selectionCount)
}
t.log.Infof("Redownloading torrent %s successful (id=%s, progress=%d)", t.GetKey(torrent), info.ID, info.Progress)
for _, id := range oldTorrentIDs {
t.fixerAddCommand(id, "replaced")
t.registerFixer(id, "replaced")
}
return info, nil
}