From 7470629486c34a25a19061871c6db325e9f7d9fa Mon Sep 17 00:00:00 2001 From: Ben Sarmiento Date: Tue, 28 May 2024 03:51:01 +0200 Subject: [PATCH] Immediate cleanup to prevent inconsistencies --- internal/config/v1.go | 4 +++- internal/torrent/dump.go | 19 +++++++++++++++++++ internal/torrent/manager.go | 8 +++++++- internal/torrent/refresh.go | 6 ++++-- pkg/realdebrid/api.go | 2 +- 5 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 internal/torrent/dump.go diff --git a/internal/config/v1.go b/internal/config/v1.go index 20b816d..48db77c 100644 --- a/internal/config/v1.go +++ b/internal/config/v1.go @@ -15,6 +15,7 @@ import ( const ( ALL_TORRENTS = "__all__" UNPLAYABLE_TORRENTS = "__unplayable__" + DUMPED_TORRENTS = "__dump__" DOWNLOADS = "__downloads__" ) @@ -42,7 +43,7 @@ func (z *ZurgConfigV1) GetVersion() string { } func (z *ZurgConfigV1) GetDirectories() []string { - rootDirectories := make([]string, len(z.Directories)+2) + rootDirectories := make([]string, len(z.Directories)+3) i := 0 for directory := range z.Directories { rootDirectories[i] = directory @@ -50,6 +51,7 @@ func (z *ZurgConfigV1) GetDirectories() []string { } rootDirectories[i] = ALL_TORRENTS rootDirectories[i+1] = UNPLAYABLE_TORRENTS + rootDirectories[i+2] = DUMPED_TORRENTS return rootDirectories } diff --git a/internal/torrent/dump.go b/internal/torrent/dump.go new file mode 100644 index 0000000..d02fb0b --- /dev/null +++ b/internal/torrent/dump.go @@ -0,0 +1,19 @@ +package torrent + +import "github.com/debridmediamanager/zurg/internal/config" + +func (t *TorrentManager) loadDumpedTorrents() { + // TODO: Paywall + count := 0 + t.getTorrentFiles("data").Each(func(filePath string) bool { + torrent := t.readTorrentFromFile(filePath) + if torrent != nil { + accessKey := t.GetKey(torrent) + torrents, _ := t.DirectoryMap.Get(config.DUMPED_TORRENTS) + torrents.Set(accessKey, torrent) + count++ + } + return false + }) + t.log.Infof("Loaded %d dumped torrents", count) +} diff --git a/internal/torrent/manager.go b/internal/torrent/manager.go index b755c43..dcb12eb 100644 --- a/internal/torrent/manager.go +++ b/internal/torrent/manager.go @@ -91,8 +91,12 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, r t.initializeDirectoryMaps() var wg sync.WaitGroup - wg.Add(2) + wg.Add(3) + t.workerPool.Submit(func() { + defer wg.Done() + t.loadDumpedTorrents() + }) t.workerPool.Submit(func() { defer wg.Done() @@ -417,8 +421,10 @@ func (t *TorrentManager) StartDumpJob() { select { case <-dumpTicker.C: t.dumpTorrents() + t.loadDumpedTorrents() case <-t.DumpTrigger: t.dumpTorrents() + t.loadDumpedTorrents() } } }) diff --git a/internal/torrent/refresh.go b/internal/torrent/refresh.go index f07ee63..9908c36 100644 --- a/internal/torrent/refresh.go +++ b/internal/torrent/refresh.go @@ -79,6 +79,10 @@ func (t *TorrentManager) refreshTorrents() []string { }) } + t.workerPool.Submit(func() { + t.cleanupBins(freshIDs) + }) + wg.Wait() close(mergeChan) @@ -136,8 +140,6 @@ func (t *TorrentManager) refreshTorrents() []string { } return false }) - - t.cleanupBins(freshIDs) }) return updatedPaths.ToSlice() diff --git a/pkg/realdebrid/api.go b/pkg/realdebrid/api.go index 117c2b3..d5cfad8 100644 --- a/pkg/realdebrid/api.go +++ b/pkg/realdebrid/api.go @@ -175,7 +175,7 @@ func (rd *RealDebrid) GetTorrents(onlyOne bool) ([]Torrent, int, error) { // compute ceiling of totalCount / limit maxPages := (totalCount + rd.cfg.GetTorrentsCount() - 1) / rd.cfg.GetTorrentsCount() rd.log.Debugf("Torrents total count is %d, max pages is %d", totalCount, maxPages) - maxParallelThreads := 4 + maxParallelThreads := 2 if maxPages < maxParallelThreads { maxParallelThreads = maxPages }