Refactor workers
This commit is contained in:
@@ -3,13 +3,11 @@ package torrent
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/debridmediamanager/zurg/internal/config"
|
||||
"github.com/debridmediamanager/zurg/pkg/logutil"
|
||||
"github.com/debridmediamanager/zurg/pkg/realdebrid"
|
||||
"github.com/debridmediamanager/zurg/pkg/utils"
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
@@ -58,51 +56,12 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
|
||||
log: log,
|
||||
}
|
||||
|
||||
// create internal directories
|
||||
t.DirectoryMap.Set(INT_ALL, cmap.New[*Torrent]()) // key is GetAccessKey()
|
||||
t.DirectoryMap.Set(INT_INFO_CACHE, cmap.New[*Torrent]()) // key is Torrent ID
|
||||
// create directory maps
|
||||
for _, directory := range cfg.GetDirectories() {
|
||||
t.DirectoryMap.Set(directory, cmap.New[*Torrent]())
|
||||
}
|
||||
|
||||
// Fetch downloads
|
||||
if t.Config.EnableDownloadMount() {
|
||||
_ = t.workerPool.Submit(func() {
|
||||
page := 1
|
||||
offset := 0
|
||||
for {
|
||||
downloads, totalDownloads, err := t.Api.GetDownloads(page, offset)
|
||||
if err != nil {
|
||||
t.log.Fatalf("Cannot get downloads: %v", err)
|
||||
}
|
||||
for i := range downloads {
|
||||
t.DownloadMap.Set(downloads[i].Filename, &downloads[i])
|
||||
}
|
||||
offset += len(downloads)
|
||||
page++
|
||||
if offset >= totalDownloads {
|
||||
t.log.Infof("Compiled into %d downloads", t.DownloadCache.Count())
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
t.RefreshTorrents()
|
||||
t.initializeDirectories()
|
||||
t.mountDownloads()
|
||||
t.refreshTorrents()
|
||||
t.SetNewLatestState(t.getCurrentState())
|
||||
|
||||
if t.Config.EnableRepair() {
|
||||
t.RepairAll() // initial repair
|
||||
} else {
|
||||
t.log.Info("Repair is disabled, skipping repair check")
|
||||
}
|
||||
|
||||
t.log.Info("Finished initializing torrent manager")
|
||||
|
||||
_ = t.workerPool.Submit(func() {
|
||||
t.startRefreshJob()
|
||||
})
|
||||
t.startRefreshJob()
|
||||
t.startRepairJob()
|
||||
|
||||
return t
|
||||
}
|
||||
@@ -127,45 +86,6 @@ func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.Download {
|
||||
return ret
|
||||
}
|
||||
|
||||
func (t *TorrentManager) TriggerHookOnLibraryUpdate(updatedPaths []string) {
|
||||
t.log.Debugf("Triggering hook on_library_update for %d path(s)", len(updatedPaths))
|
||||
_ = t.workerPool.Submit(func() {
|
||||
OnLibraryUpdateHook(updatedPaths, t.Config, t.log)
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TorrentManager) assignedDirectoryCb(tor *Torrent, cb func(string)) {
|
||||
torrentIDs := tor.DownloadedIDs.Union(tor.InProgressIDs).ToSlice()
|
||||
// get filenames needed for directory conditions
|
||||
var filenames []string
|
||||
var fileSizes []int64
|
||||
unplayable := true
|
||||
tor.SelectedFiles.IterCb(func(key string, file *File) {
|
||||
filenames = append(filenames, filepath.Base(file.Path))
|
||||
fileSizes = append(fileSizes, file.Bytes)
|
||||
if unplayable && utils.IsStreamable(file.Path) {
|
||||
unplayable = false
|
||||
}
|
||||
})
|
||||
// Map torrents to directories
|
||||
switch t.Config.GetVersion() {
|
||||
case "v1":
|
||||
configV1 := t.Config.(*config.ZurgConfigV1)
|
||||
for _, directories := range configV1.GetGroupMap() {
|
||||
for _, directory := range directories {
|
||||
if unplayable {
|
||||
cb(config.UNPLAYABLE_TORRENTS)
|
||||
break
|
||||
}
|
||||
if t.Config.MeetsConditions(directory, t.GetKey(tor), tor.ComputeTotalSize(), torrentIDs, filenames, fileSizes) {
|
||||
cb(directory)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TorrentManager) GetKey(torrent *Torrent) string {
|
||||
if !t.Config.ShouldIgnoreRenames() && torrent.Rename != "" {
|
||||
return torrent.Rename
|
||||
@@ -242,3 +162,38 @@ func (t *TorrentManager) deleteTorrentFile(torrentID string) {
|
||||
t.log.Warnf("Cannot delete file %s: %v", filePath, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TorrentManager) mountDownloads() {
|
||||
if !t.Config.EnableDownloadMount() {
|
||||
return
|
||||
}
|
||||
_ = t.workerPool.Submit(func() {
|
||||
page := 1
|
||||
offset := 0
|
||||
for {
|
||||
downloads, totalDownloads, err := t.Api.GetDownloads(page, offset)
|
||||
if err != nil {
|
||||
t.log.Fatalf("Cannot get downloads: %v", err)
|
||||
}
|
||||
for i := range downloads {
|
||||
t.DownloadMap.Set(downloads[i].Filename, &downloads[i])
|
||||
}
|
||||
offset += len(downloads)
|
||||
page++
|
||||
if offset >= totalDownloads {
|
||||
break
|
||||
}
|
||||
}
|
||||
t.log.Infof("Compiled into %d downloads", t.DownloadCache.Count())
|
||||
})
|
||||
}
|
||||
|
||||
func (t *TorrentManager) initializeDirectories() {
|
||||
// create internal directories
|
||||
t.DirectoryMap.Set(INT_ALL, cmap.New[*Torrent]()) // key is GetAccessKey()
|
||||
t.DirectoryMap.Set(INT_INFO_CACHE, cmap.New[*Torrent]()) // key is Torrent ID
|
||||
// create directory maps
|
||||
for _, directory := range t.Config.GetDirectories() {
|
||||
t.DirectoryMap.Set(directory, cmap.New[*Torrent]())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user