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

View File

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

View File

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