try to fix memory hogging

This commit is contained in:
Mukhtar Akere
2025-02-28 16:05:04 +01:00
parent fce2ce28c7
commit 84bd93805f
6 changed files with 33 additions and 8 deletions

View File

@@ -10,6 +10,7 @@ import (
"github.com/sirrobot01/debrid-blackhole/internal/request" "github.com/sirrobot01/debrid-blackhole/internal/request"
"github.com/sirrobot01/debrid-blackhole/internal/utils" "github.com/sirrobot01/debrid-blackhole/internal/utils"
"github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent"
"slices"
"net/http" "net/http"
gourl "net/url" gourl "net/url"
@@ -192,7 +193,7 @@ func (ad *AllDebrid) CheckStatus(torrent *torrent.Torrent, isSymlink bool) (*tor
} }
} }
break break
} else if status == "downloading" { } else if slices.Contains(ad.GetDownloadingStatus(), status) {
if !ad.DownloadUncached { if !ad.DownloadUncached {
return torrent, fmt.Errorf("torrent: %s not cached", torrent.Name) 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") return nil, fmt.Errorf("not implemented")
} }
func (ad *AllDebrid) GetDownloadingStatus() []string {
return []string{"downloading"}
}
func New(dc config.Debrid, cache *cache.Cache) *AllDebrid { func New(dc config.Debrid, cache *cache.Cache) *AllDebrid {
rl := request.ParseRateLimit(dc.RateLimit) rl := request.ParseRateLimit(dc.RateLimit)
headers := map[string]string{ headers := map[string]string{

View File

@@ -11,6 +11,7 @@ import (
"github.com/sirrobot01/debrid-blackhole/internal/request" "github.com/sirrobot01/debrid-blackhole/internal/request"
"github.com/sirrobot01/debrid-blackhole/internal/utils" "github.com/sirrobot01/debrid-blackhole/internal/utils"
"github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent" "github.com/sirrobot01/debrid-blackhole/pkg/debrid/torrent"
"slices"
"net/http" "net/http"
"os" "os"
@@ -216,7 +217,7 @@ func (dl *DebridLink) CheckStatus(torrent *torrent.Torrent, isSymlink bool) (*to
return torrent, err return torrent, err
} }
break break
} else if status == "downloading" { } else if slices.Contains(dl.GetDownloadingStatus(), status) {
if !dl.DownloadUncached { if !dl.DownloadUncached {
return torrent, fmt.Errorf("torrent: %s not cached", torrent.Name) 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 return &dlLink
} }
func (dl *DebridLink) GetDownloadingStatus() []string {
return []string{"downloading"}
}
func (dl *DebridLink) GetCheckCached() bool { func (dl *DebridLink) GetCheckCached() bool {
return dl.CheckCached return dl.CheckCached
} }

View File

@@ -17,4 +17,5 @@ type Service interface {
GetTorrents() ([]*torrent.Torrent, error) GetTorrents() ([]*torrent.Torrent, error)
GetName() string GetName() string
GetLogger() zerolog.Logger GetLogger() zerolog.Logger
GetDownloadingStatus() []string
} }

View File

@@ -216,7 +216,6 @@ func (r *RealDebrid) CheckStatus(t *torrent.Torrent, isSymlink bool) (*torrent.T
t.Status = status t.Status = status
t.Debrid = r.Name t.Debrid = r.Name
t.MountPath = r.MountPath t.MountPath = r.MountPath
downloadingStatus := []string{"downloading", "magnet_conversion", "queued", "compressing", "uploading"}
if status == "waiting_files_selection" { if status == "waiting_files_selection" {
files := GetTorrentFiles(data, true) // Validate files to be selected files := GetTorrentFiles(data, true) // Validate files to be selected
t.Files = files t.Files = files
@@ -247,7 +246,7 @@ func (r *RealDebrid) CheckStatus(t *torrent.Torrent, isSymlink bool) (*torrent.T
} }
} }
break break
} else if slices.Contains(downloadingStatus, status) { } else if slices.Contains(r.GetDownloadingStatus(), status) {
if !r.DownloadUncached { if !r.DownloadUncached {
return t, fmt.Errorf("torrent: %s not cached", t.Name) 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 { func New(dc config.Debrid, cache *cache.Cache) *RealDebrid {
rl := request.ParseRateLimit(dc.RateLimit) rl := request.ParseRateLimit(dc.RateLimit)
headers := map[string]string{ headers := map[string]string{

View File

@@ -232,7 +232,7 @@ func (tb *Torbox) CheckStatus(torrent *torrent.Torrent, isSymlink bool) (*torren
} }
} }
break break
} else if status == "downloading" { } else if slices.Contains(tb.GetDownloadingStatus(), status) {
if !tb.DownloadUncached { if !tb.DownloadUncached {
return torrent, fmt.Errorf("torrent: %s not cached", torrent.Name) 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 { func (tb *Torbox) GetCheckCached() bool {
return tb.CheckCached return tb.CheckCached
} }

View File

@@ -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) { func (q *QBit) ProcessFiles(torrent *Torrent, debridTorrent *debrid.Torrent, arr *arr.Arr, isSymlink bool) {
debridClient := service.GetDebrid().GetByName(debridTorrent.Debrid) debridClient := service.GetDebrid().GetByName(debridTorrent.Debrid)
for debridTorrent.Status != "downloaded" { for debridTorrent.Status != "downloaded" {
progress := debridTorrent.Progress q.logger.Debug().Msgf("%s <- (%s) Download Progress: %.2f%%", debridTorrent.Debrid, debridTorrent.Name, debridTorrent.Progress)
q.logger.Debug().Msgf("%s -> (%s) Download Progress: %.2f%%", debridTorrent.Debrid, debridTorrent.Name, progress)
time.Sleep(10 * time.Second)
dbT, err := debridClient.CheckStatus(debridTorrent, isSymlink) dbT, err := debridClient.CheckStatus(debridTorrent, isSymlink)
if err != nil { if err != nil {
q.logger.Error().Msgf("Error checking status: %v", err) 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() _ = arr.Refresh()
return return
} }
debridTorrent = dbT debridTorrent = dbT
torrent = q.UpdateTorrentMin(torrent, debridTorrent) 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 ( var (
torrentSymlinkPath string torrentSymlinkPath string