From 0649a083bd1f85980f405fadb142ffe9cee16850 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Thu, 1 Feb 2024 20:39:12 +0100 Subject: [PATCH] Small tweak to repair --- internal/torrent/fixer.go | 10 ++++++---- internal/torrent/manager.go | 17 +++++++++++------ internal/torrent/repair.go | 24 ++++++++++-------------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/internal/torrent/fixer.go b/internal/torrent/fixer.go index 141202f..1794494 100644 --- a/internal/torrent/fixer.go +++ b/internal/torrent/fixer.go @@ -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() { diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 84f301c..5fd4d97 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -60,14 +60,19 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w workerPool: workerPool, log: log, } + t.fixers = t.readFixersFromFile() t.initializeDirectories() - t.refreshTorrents() - t.setNewLatestState(t.getCurrentState()) - t.StartRefreshJob() - t.StartRepairJob() - t.mountDownloads() - t.StartDownloadsJob() + t.workerPool.Submit(func() { + t.refreshTorrents() + t.setNewLatestState(t.getCurrentState()) + t.StartRefreshJob() + t.StartRepairJob() + }) + t.workerPool.Submit(func() { + t.mountDownloads() + t.StartDownloadsJob() + }) return t } diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index 7de6556..0a5d636 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -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) - for _, id := range oldTorrentIDs { - t.fixerAddCommand(id, "delete_replaced") - } + // looks like it's fixed even if it's not done yet + for _, id := range oldTorrentIDs { + t.fixerAddCommand(id, "replaced") } return info, nil }