Small tweak to repair

This commit is contained in:
Ben Sarmiento
2024-02-01 20:39:12 +01:00
parent 68b946c625
commit 0649a083bd
3 changed files with 27 additions and 24 deletions

View File

@@ -28,15 +28,15 @@ func (t *TorrentManager) handleFixers() {
if !t.fixers.Has(id) || torrent.AnyInProgress() {
return
}
command, _ := t.fixers.Pop(id)
command, _ := t.fixers.Pop(id) // delete the fixer if it's done
switch command {
case "delete_replaced":
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))
toDelete = append(toDelete, id)
case "delete_failed":
case "download_failed": // id is failed fixer id
t.log.Debugf("Deleting failed fixer id=%s of torrent %s", id, t.GetKey(torrent))
toDelete = append(toDelete, id)
case "repair":
case "repaired": // id is fixer id
t.log.Debugf("Repairing torrent %s again now that fixer id=%s is done", t.GetKey(torrent), id)
toDelete = append(toDelete, id)
repairMe, _ := allTorrents.Get(t.GetKey(torrent))
@@ -48,6 +48,8 @@ func (t *TorrentManager) handleFixers() {
infoCache.Remove(id)
t.deleteTorrentFile(id)
}
t.writeFixersToFile()
t.log.Debugf("Finished handling fixers")
}
func (t *TorrentManager) writeFixersToFile() {

View File

@@ -60,14 +60,19 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
workerPool: workerPool,
log: log,
}
t.fixers = t.readFixersFromFile()
t.initializeDirectories()
t.workerPool.Submit(func() {
t.refreshTorrents()
t.setNewLatestState(t.getCurrentState())
t.StartRefreshJob()
t.StartRepairJob()
})
t.workerPool.Submit(func() {
t.mountDownloads()
t.StartDownloadsJob()
})
return t
}

View File

@@ -212,13 +212,13 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// second step: download the broken files
if len(brokenFiles) > 0 {
t.log.Infof("Repairing by downloading only the %d broken out of %d files of torrent %s", len(brokenFiles), torrent.SelectedFiles.Count(), t.GetKey(torrent))
redownloadedTorrent, 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())
return
}
if redownloadedTorrent != nil {
t.fixerAddCommand(redownloadedTorrent.ID, "repair")
if redownloadedInfo != nil {
t.fixerAddCommand(redownloadedInfo.ID, "repaired")
return
}
} else {
@@ -347,7 +347,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
// select files
err = t.Api.SelectTorrentFiles(newTorrentID, selection)
if err != nil {
t.fixerAddCommand(newTorrentID, "delete_failed")
t.fixerAddCommand(newTorrentID, "download_failed")
return nil, fmt.Errorf("cannot start redownloading: %v", err)
}
@@ -357,7 +357,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.fixerAddCommand(newTorrentID, "delete_failed")
t.fixerAddCommand(newTorrentID, "download_failed")
return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err)
}
@@ -372,24 +372,20 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
}
}
if !isOkStatus {
t.fixerAddCommand(newTorrentID, "delete_failed")
t.fixerAddCommand(newTorrentID, "download_failed")
return nil, fmt.Errorf("the redownloaded torrent %s (id=%s) is in error state: %s", t.GetKey(torrent), newTorrentID, info.Status)
}
// check if incorrect number of links
selectionCount := len(strings.Split(selection, ","))
if info.Progress == 100 && len(info.Links) != selectionCount {
t.fixerAddCommand(newTorrentID, "delete_failed")
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)
}
// looks like it's fixed
if len(oldTorrentIDs) > 0 {
// replace the old torrent (empty selection)
// looks like it's fixed even if it's not done yet
for _, id := range oldTorrentIDs {
t.fixerAddCommand(id, "delete_replaced")
}
t.fixerAddCommand(id, "replaced")
}
return info, nil
}