Add context deadline to request

This commit is contained in:
Ben Sarmiento
2024-01-27 21:09:00 +01:00
parent 1aabcfd322
commit ce4b794098
4 changed files with 54 additions and 40 deletions

View File

@@ -58,12 +58,15 @@ func MainApp(configPath string) {
}
defer workerPool.Release()
repairPool, err := ants.NewPool(1, ants.WithMaxBlockingTasks(1))
if err != nil {
zurglog.Errorf("Failed to create repair pool: %v", err)
os.Exit(1)
var repairPool *ants.Pool
if config.EnableRepair() {
repairPool, err := ants.NewPool(1)
if err != nil {
zurglog.Errorf("Failed to create repair pool: %v", err)
os.Exit(1)
}
defer repairPool.Release()
}
defer repairPool.Release()
utils.EnsureDirExists("data") // Ensure the data directory exists
torrentMgr := torrent.NewTorrentManager(config, rd, workerPool, repairPool, log.Named("manager"))

View File

@@ -26,7 +26,6 @@ type TorrentManager struct {
DownloadCache cmap.ConcurrentMap[string, *realdebrid.Download]
DownloadMap cmap.ConcurrentMap[string, *realdebrid.Download]
fixers cmap.ConcurrentMap[string, *Torrent]
repairs mapset.Set[string]
deleteOnceDone mapset.Set[string]
allAccessKeys mapset.Set[string]
latestState *LibraryState
@@ -50,7 +49,6 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, w
DownloadCache: cmap.New[*realdebrid.Download](),
DownloadMap: cmap.New[*realdebrid.Download](),
fixers: cmap.New[*Torrent](),
repairs: mapset.NewSet[string](),
deleteOnceDone: mapset.NewSet[string](),
allAccessKeys: mapset.NewSet[string](),
latestState: &LibraryState{},

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"math"
"strings"
"sync"
"time"
"github.com/debridmediamanager/zurg/internal/config"
@@ -70,26 +71,25 @@ func (t *TorrentManager) TriggerRepair(torrent *Torrent) {
}
func (t *TorrentManager) repairAll(torrent *Torrent) {
t.log.Info("Periodic repair invoked; searching for broken torrents")
// todo: a more elegant way to do this
var allTorrents cmap.ConcurrentMap[string, *Torrent]
var haystack cmap.ConcurrentMap[string, *Torrent]
if torrent == nil {
allTorrents, _ = t.DirectoryMap.Get(INT_ALL)
haystack, _ = t.DirectoryMap.Get(INT_ALL)
t.log.Info("Periodic repair started; searching for broken torrents")
} else {
allTorrents = cmap.New[*Torrent]()
allTorrents.Set("", torrent)
haystack = cmap.New[*Torrent]()
haystack.Set("", torrent)
t.log.Infof("Repair invoked for torrent %s", t.GetKey(torrent))
}
// collect all torrents that need to be repaired
toRepair := mapset.NewSet[*Torrent]()
allTorrents.IterCb(func(_ string, torrent *Torrent) {
haystack.IterCb(func(_ string, torrent *Torrent) {
if torrent.AnyInProgress() || torrent.AllInProgress() || torrent.UnrepairableReason != "" {
return
}
// check 2: for broken files
// check 1: for broken files
brokenFileIDs := mapset.NewSet[int]()
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if !strings.HasPrefix(file.Link, "http") && file.Link != "unselect" {
@@ -101,8 +101,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
toRepair.Add(torrent)
return
}
// check 3: for expired links
// check 2: for expired links
if torrent.UnassignedLinks.Cardinality() > 0 {
t.log.Debugf("Torrent %s has unassigned links, adding to repair list", t.GetKey(torrent))
toRepair.Add(torrent)
@@ -110,46 +109,43 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
}
})
if toRepair.Cardinality() == 0 {
t.log.Info("Periodic repair found no broken torrents to repair")
} else {
t.log.Info("Periodic repair found %d broken torrents to repair in total", toRepair.Cardinality())
t.log.Infof("Found %d broken torrents to repair in total", toRepair.Cardinality())
toRepair.Each(func(torrent *Torrent) bool {
t.Repair(torrent)
return false
})
}
var wg sync.WaitGroup
toRepair.Each(func(torrent *Torrent) bool {
wg.Add(1)
t.Repair(torrent, &wg)
return false
})
wg.Wait()
t.log.Infof("Finished repairing %d broken torrents", toRepair.Cardinality())
}
func (t *TorrentManager) Repair(torrent *Torrent) {
func (t *TorrentManager) Repair(torrent *Torrent, wg *sync.WaitGroup) {
if torrent.UnrepairableReason != "" {
t.log.Warnf("Torrent %s is unfixable (%s), skipping repair", t.GetKey(torrent), torrent.UnrepairableReason)
return
}
if t.repairs.Contains(t.GetKey(torrent)) {
t.log.Warnf("Torrent %s is already being repaired, skipping repair", t.GetKey(torrent))
wg.Done()
return
}
if torrent.AnyInProgress() || torrent.AllInProgress() {
t.log.Infof("Torrent %s is in progress, skipping repair until download is done", t.GetKey(torrent))
wg.Done()
return
}
t.repairs.Add(t.GetKey(torrent))
t.log.Infof("Attempting repair for torrent %s", t.GetKey(torrent))
// blocks for approx 45 minutes if active torrents are full
if !t.canCapacityHandle() {
t.log.Error("Blocked for too long due to limit of active torrents, cannot continue with the repair")
wg.Done()
return
}
// assign to a worker
_ = t.workerPool.Submit(func() {
defer wg.Done()
t.repair(torrent)
t.repairs.Remove(t.GetKey(torrent))
})
}