From 84bd93805f5618cbe07d2bd8e662bed4c152bb4c Mon Sep 17 00:00:00 2001 From: Mukhtar Akere Date: Fri, 28 Feb 2025 16:05:04 +0100 Subject: [PATCH] try to fix memory hogging --- pkg/debrid/alldebrid/alldebrid.go | 7 ++++++- pkg/debrid/debrid_link/debrid_link.go | 7 ++++++- pkg/debrid/engine/service.go | 1 + pkg/debrid/realdebrid/realdebrid.go | 7 +++++-- pkg/debrid/torbox/torbox.go | 6 +++++- pkg/qbit/torrent.go | 13 ++++++++++--- 6 files changed, 33 insertions(+), 8 deletions(-) diff --git a/pkg/debrid/alldebrid/alldebrid.go b/pkg/debrid/alldebrid/alldebrid.go index 286e427..9c46c2c 100644 --- a/pkg/debrid/alldebrid/alldebrid.go +++ b/pkg/debrid/alldebrid/alldebrid.go @@ -10,6 +10,7 @@ import ( "github.com/sirrobot01/debrid-blackhole/internal/request" "github.com/sirrobot01/debrid-blackhole/internal/utils" "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" + "slices" "net/http" gourl "net/url" @@ -192,7 +193,7 @@ func (ad *AllDebrid) CheckStatus(torrent *torrent.Torrent, isSymlink bool) (*tor } } break - } else if status == "downloading" { + } else if slices.Contains(ad.GetDownloadingStatus(), status) { if !ad.DownloadUncached { return torrent, fmt.Errorf("torrent: %s not cached", torrent.Name) } @@ -277,6 +278,10 @@ func (ad *AllDebrid) GetTorrents() ([]*torrent.Torrent, error) { return nil, fmt.Errorf("not implemented") } +func (ad *AllDebrid) GetDownloadingStatus() []string { + return []string{"downloading"} +} + func New(dc config.Debrid, cache *cache.Cache) *AllDebrid { rl := request.ParseRateLimit(dc.RateLimit) headers := map[string]string{ diff --git a/pkg/debrid/debrid_link/debrid_link.go b/pkg/debrid/debrid_link/debrid_link.go index 4c17437..fd4058f 100644 --- a/pkg/debrid/debrid_link/debrid_link.go +++ b/pkg/debrid/debrid_link/debrid_link.go @@ -11,6 +11,7 @@ import ( "github.com/sirrobot01/debrid-blackhole/internal/request" "github.com/sirrobot01/debrid-blackhole/internal/utils" "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" + "slices" "net/http" "os" @@ -216,7 +217,7 @@ func (dl *DebridLink) CheckStatus(torrent *torrent.Torrent, isSymlink bool) (*to return torrent, err } break - } else if status == "downloading" { + } else if slices.Contains(dl.GetDownloadingStatus(), status) { if !dl.DownloadUncached { return torrent, fmt.Errorf("torrent: %s not cached", torrent.Name) } @@ -263,6 +264,10 @@ func (dl *DebridLink) GetDownloadLink(t *torrent.Torrent, file *torrent.File) *t return &dlLink } +func (dl *DebridLink) GetDownloadingStatus() []string { + return []string{"downloading"} +} + func (dl *DebridLink) GetCheckCached() bool { return dl.CheckCached } diff --git a/pkg/debrid/engine/service.go b/pkg/debrid/engine/service.go index 8bf4c7e..05819f0 100644 --- a/pkg/debrid/engine/service.go +++ b/pkg/debrid/engine/service.go @@ -17,4 +17,5 @@ type Service interface { GetTorrents() ([]*torrent.Torrent, error) GetName() string GetLogger() zerolog.Logger + GetDownloadingStatus() []string } diff --git a/pkg/debrid/realdebrid/realdebrid.go b/pkg/debrid/realdebrid/realdebrid.go index 168a8a1..27e219b 100644 --- a/pkg/debrid/realdebrid/realdebrid.go +++ b/pkg/debrid/realdebrid/realdebrid.go @@ -216,7 +216,6 @@ func (r *RealDebrid) CheckStatus(t *torrent.Torrent, isSymlink bool) (*torrent.T t.Status = status t.Debrid = r.Name t.MountPath = r.MountPath - downloadingStatus := []string{"downloading", "magnet_conversion", "queued", "compressing", "uploading"} if status == "waiting_files_selection" { files := GetTorrentFiles(data, true) // Validate files to be selected t.Files = files @@ -247,7 +246,7 @@ func (r *RealDebrid) CheckStatus(t *torrent.Torrent, isSymlink bool) (*torrent.T } } break - } else if slices.Contains(downloadingStatus, status) { + } else if slices.Contains(r.GetDownloadingStatus(), status) { if !r.DownloadUncached { return t, fmt.Errorf("torrent: %s not cached", t.Name) } @@ -379,6 +378,10 @@ func (r *RealDebrid) GetTorrents() ([]*torrent.Torrent, error) { } +func (r *RealDebrid) GetDownloadingStatus() []string { + return []string{"downloading", "magnet_conversion", "queued", "compressing", "uploading"} +} + func New(dc config.Debrid, cache *cache.Cache) *RealDebrid { rl := request.ParseRateLimit(dc.RateLimit) headers := map[string]string{ diff --git a/pkg/debrid/torbox/torbox.go b/pkg/debrid/torbox/torbox.go index 6b1fe6e..e2ebc9b 100644 --- a/pkg/debrid/torbox/torbox.go +++ b/pkg/debrid/torbox/torbox.go @@ -232,7 +232,7 @@ func (tb *Torbox) CheckStatus(torrent *torrent.Torrent, isSymlink bool) (*torren } } break - } else if status == "downloading" { + } else if slices.Contains(tb.GetDownloadingStatus(), status) { if !tb.DownloadUncached { return torrent, fmt.Errorf("torrent: %s not cached", torrent.Name) } @@ -322,6 +322,10 @@ func (tb *Torbox) GetDownloadLink(t *torrent.Torrent, file *torrent.File) *torre } } +func (tb *Torbox) GetDownloadingStatus() []string { + return []string{"downloading"} +} + func (tb *Torbox) GetCheckCached() bool { return tb.CheckCached } diff --git a/pkg/qbit/torrent.go b/pkg/qbit/torrent.go index 78c4ab3..e539994 100644 --- a/pkg/qbit/torrent.go +++ b/pkg/qbit/torrent.go @@ -75,9 +75,7 @@ func (q *QBit) Process(ctx context.Context, magnet *utils.Magnet, category strin func (q *QBit) ProcessFiles(torrent *Torrent, debridTorrent *debrid.Torrent, arr *arr.Arr, isSymlink bool) { debridClient := service.GetDebrid().GetByName(debridTorrent.Debrid) for debridTorrent.Status != "downloaded" { - progress := debridTorrent.Progress - q.logger.Debug().Msgf("%s -> (%s) Download Progress: %.2f%%", debridTorrent.Debrid, debridTorrent.Name, progress) - time.Sleep(10 * time.Second) + q.logger.Debug().Msgf("%s <- (%s) Download Progress: %.2f%%", debridTorrent.Debrid, debridTorrent.Name, debridTorrent.Progress) dbT, err := debridClient.CheckStatus(debridTorrent, isSymlink) if err != nil { q.logger.Error().Msgf("Error checking status: %v", err) @@ -86,8 +84,17 @@ func (q *QBit) ProcessFiles(torrent *Torrent, debridTorrent *debrid.Torrent, arr _ = arr.Refresh() return } + debridTorrent = dbT torrent = q.UpdateTorrentMin(torrent, debridTorrent) + + // Exit the loop for downloading statuses to prevent memory buildup + if slices.Contains(debridClient.GetDownloadingStatus(), debridTorrent.Status) { + q.logger.Debug().Msgf("Torrent is in %s state, exiting polling loop", debridTorrent.Status) + break + } + + time.Sleep(time.Duration(q.RefreshInterval) * time.Second) } var ( torrentSymlinkPath string