Remove fixer concept

This commit is contained in:
Ben Sarmiento
2024-05-22 04:27:12 +02:00
parent 9990bf90ca
commit 2a5f12e37f
4 changed files with 40 additions and 198 deletions

View File

@@ -176,6 +176,13 @@ func (t *TorrentManager) repair(torrent *Torrent) {
t.log.Errorf("Cannot repair torrent %s: %v", torrent.Hash, err)
return
}
// delete old torrents
for id := range torrent.Components {
if id == info.ID {
continue
}
t.api.DeleteTorrent(id)
}
t.log.Infof("Successfully repaired torrent %s by redownloading all files", t.GetKey(torrent))
return
} else if info != nil && info.Progress != 100 {
@@ -205,11 +212,6 @@ func (t *TorrentManager) repair(torrent *Torrent) {
t.log.Infof("Repairing by downloading %d batches of the %d broken files of torrent %s", int(math.Ceil(float64(len(brokenFiles))/130)), len(brokenFiles), t.GetKey(torrent))
oldTorrentIDs := []string{}
for id := range torrent.Components {
oldTorrentIDs = append(oldTorrentIDs, id)
}
newlyDownloadedIds := make([]string, 0)
group := make([]*File, 0)
batchNum := 1
@@ -223,7 +225,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
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")
t.trash(newId)
}
return
}
@@ -240,15 +242,13 @@ func (t *TorrentManager) repair(torrent *Torrent) {
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")
t.trash(newId)
}
return
}
}
for _, oldId := range oldTorrentIDs {
t.registerFixer(oldId, "replaced")
}
/// TODO: should we delete the old torrents that were replaced?
}
func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
@@ -350,14 +350,9 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string) (*realdebrid.TorrentInfo, error) {
// broken files means broken links
// if brokenFiles is not provided, we will redownload all files
oldTorrentIDs := make([]string, 0)
finalSelection := strings.Join(selection, ",")
selectionCount := len(selection)
if selectionCount == 0 {
// only delete the old torrent if we are redownloading all files
for id := range torrent.Components {
oldTorrentIDs = append(oldTorrentIDs, id)
}
tmpSelection := ""
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
tmpSelection += fmt.Sprintf("%d,", file.ID) // select all files
@@ -399,7 +394,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
// select files
err = t.api.SelectTorrentFiles(newTorrentID, finalSelection)
if err != nil {
t.registerFixer(newTorrentID, "download_failed")
t.trash(newTorrentID)
return nil, fmt.Errorf("cannot start redownloading torrent %s (id=%s): %v", t.GetKey(torrent), newTorrentID, err)
}
// sleep for 2 second to let RD process the magnet
@@ -408,7 +403,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
// see if the torrent is ready
info, err = t.api.GetTorrentInfo(newTorrentID)
if err != nil {
t.registerFixer(newTorrentID, "download_failed")
t.trash(newTorrentID)
return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err)
}
@@ -434,20 +429,17 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
}
if !isOkStatus {
t.registerFixer(info.ID, "download_failed")
t.trash(info.ID)
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
if info.Progress == 100 && len(info.Links) != selectionCount {
t.registerFixer(newTorrentID, "download_failed")
t.trash(newTorrentID)
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.registerFixer(id, "replaced")
}
return info, nil
}