From bba4418b8cb7451b422ea027414f98c95bb082d9 Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Sat, 2 Dec 2023 05:46:27 +0100 Subject: [PATCH] More efficient repairs --- internal/torrent/manager.go | 50 ++++++++++++++----------------------- internal/universal/get.go | 3 --- 2 files changed, 19 insertions(+), 34 deletions(-) diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index 331712f..b0d8289 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -97,6 +97,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p } t.RefreshTorrents() + if t.Config.EnableRepair() { repairWorker, err := ants.NewPool(1) if err != nil { @@ -168,7 +169,6 @@ func (t *TorrentManager) RefreshTorrents() { t.accessKeySet.Add(accessKey) return true }) - t.checkForOtherDeletedTorrents() // now we can build the directory responses t.UpdateDirectoryResponsesCache() @@ -537,7 +537,7 @@ func (t *TorrentManager) repairAll() { } forRepair := false torrent.SelectedFiles.IterCb(func(_ string, file *File) { - if file.Link == "repair" && !forRepair { + if file.Link == "repair" { file.Link = "repairing" forRepair = true } @@ -553,19 +553,6 @@ func (t *TorrentManager) repairAll() { } } -func (t *TorrentManager) checkForOtherDeletedTorrents() { - var toDelete []string - allTorrents, _ := t.DirectoryMap.Get(INT_ALL) - allTorrents.IterCb(func(_ string, torrent *Torrent) { - if t.CheckDeletedState(torrent) { - toDelete = append(toDelete, torrent.AccessKey) - } - }) - for _, accessKey := range toDelete { - t.Delete(accessKey, true, false) - } -} - func (t *TorrentManager) CheckDeletedState(torrent *Torrent) bool { var unselectedIDs []int torrent.SelectedFiles.IterCb(func(_ string, file *File) { @@ -818,6 +805,23 @@ func (t *TorrentManager) canCapacityHandle() bool { } } +func (t *TorrentManager) RepairAll() { + _ = t.repairWorker.Submit(func() { + t.log.Info("Checking for torrents to repair") + t.repairAll() + t.log.Info("Finished checking for torrents to repair") + }) +} + +func (t *TorrentManager) Repair(torrent *Torrent) { + _ = t.repairWorker.Submit(func() { + t.log.Info("repairing torrent %s", torrent.AccessKey) + t.repair(torrent) + t.log.Info("Finished repairing torrent %s", torrent.AccessKey) + }) + t.UpdateTorrentResponseCache(torrent) +} + func (t *TorrentManager) UpdateTorrentResponseCache(torrent *Torrent) { dav, html := t.buildTorrentResponses(torrent) t.AssignedDirectoryCb(torrent, func(directory string) { @@ -900,19 +904,3 @@ func (t *TorrentManager) AssignedDirectoryCb(tor *Torrent, cb func(string)) { } } } - -func (t *TorrentManager) RepairAll() { - _ = t.repairWorker.Submit(func() { - t.log.Info("Checking for torrents to repair") - t.repairAll() - t.log.Info("Finished checking for torrents to repair") - }) -} - -func (t *TorrentManager) Repair(torrent *Torrent) { - _ = t.repairWorker.Submit(func() { - t.log.Info("repairing torrent %s", torrent.AccessKey) - t.repair(torrent) - t.log.Info("Finished repairing torrent %s", torrent.AccessKey) - }) -} diff --git a/internal/universal/get.go b/internal/universal/get.go index 75e3dfa..024574a 100644 --- a/internal/universal/get.go +++ b/internal/universal/get.go @@ -69,7 +69,6 @@ func (gf *GetFile) HandleGetRequest(directory, torrentName, fileName string, res // log.Warnf("File %s is no longer available, link %s", filepath.Base(file.Path), link) file.Link = "repairing" torMgr.Repair(torrent) - torMgr.UpdateTorrentResponseCache(torrent) http.Error(resp, "File is not available", http.StatusNotFound) return } else { @@ -151,7 +150,6 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi log.Warnf("Cannot download file %s: %v", file.Path, err) file.Link = "repairing" torMgr.Repair(torrent) - torMgr.UpdateTorrentResponseCache(torrent) } http.Error(resp, "File is not available", http.StatusNotFound) return @@ -163,7 +161,6 @@ func (gf *GetFile) streamFileToResponse(torrent *intTor.Torrent, file *intTor.Fi log.Warnf("Received a %s status code for file %s", download.Status, file.Path) file.Link = "repairing" torMgr.Repair(torrent) - torMgr.UpdateTorrentResponseCache(torrent) } http.Error(resp, "File is not available", http.StatusNotFound) return