Add job for monitoring bw limit status of tokens

This commit is contained in:
Ben Adrian Sarmiento
2024-06-28 18:55:02 +02:00
parent c3aea427d0
commit 67111696a2
13 changed files with 136 additions and 55 deletions

View File

@@ -219,7 +219,7 @@ func (t *TorrentManager) applyMediaInfoDetails(torrent *Torrent) error {
return
}
unrestrict, err := t.UnrestrictFile(file)
if utils.IsBWLimitExceeded(err) {
if utils.AreAllTokensExpired(err) {
bwLimitReached = true
return
}
@@ -337,6 +337,7 @@ func (t *TorrentManager) mountNewDownloads() {
downloads := t.rd.GetDownloads()
mountedCount := 0
for i := range downloads {
downloads[i].Token = token
isRealDebrid := strings.HasPrefix(downloads[i].Link, "https://real-debrid.com/d/")
if !isRealDebrid {
filename := filepath.Base(downloads[i].Filename)

View File

@@ -294,16 +294,15 @@ func (t *TorrentManager) mergeTorrents(existing, toMerge *Torrent) *Torrent {
newer.SelectedFiles.IterCb(func(key string, file *File) {
mergedTorrent.SelectedFiles.SetIfAbsent(key, file)
})
older.SelectedFiles.IterCb(func(key string, file *File) {
mediaInfo := file.MediaInfo
f, ok := mergedTorrent.SelectedFiles.Get(key)
if !ok || !f.State.Is("ok_file") {
mergedTorrent.SelectedFiles.Set(key, file)
older.SelectedFiles.IterCb(func(key string, olderFile *File) {
file, ok := mergedTorrent.SelectedFiles.Get(key)
if !ok || (file.State.Is("broken_file") && olderFile.State.Is("ok_file")) {
mergedTorrent.SelectedFiles.Set(key, olderFile)
}
// get the file again, set the media info
f, ok = mergedTorrent.SelectedFiles.Get(key)
if ok && f.MediaInfo == nil && mediaInfo != nil {
f.MediaInfo = mediaInfo
file, ok = mergedTorrent.SelectedFiles.Get(key)
if ok && file.MediaInfo == nil && olderFile.MediaInfo != nil {
file.MediaInfo = olderFile.MediaInfo
}
})

View File

@@ -19,6 +19,7 @@ const (
EXPIRED_LINK_TOLERANCE_HOURS = 24
)
// StartRepairJob is a permanent job that runs every periodically to repair broken torrents
func (t *TorrentManager) StartRepairJob() {
if !t.Config.EnableRepair() {
t.repairLog.Warn("Repair is disabled, skipping repair job")
@@ -34,7 +35,6 @@ func (t *TorrentManager) StartRepairJob() {
t.repairLog.Debug("Starting periodic repair job")
repairTicker := time.NewTicker(time.Duration(t.Config.GetRepairEveryMins()) * time.Minute)
defer repairTicker.Stop()
for {
select {
case <-repairTicker.C:
@@ -192,11 +192,12 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
bwLimitReached := false
// check for other broken file
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
// we will only check for working files, we ignore broken and deleted files
if bwLimitReached || !file.State.Is("ok_file") {
return
}
_, err := t.UnrestrictFile(file)
if utils.IsBWLimitExceeded(err) {
if utils.AreAllTokensExpired(err) {
bwLimitReached = true
return
}
@@ -252,7 +253,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
info, err := t.redownloadTorrent(torrent, []string{}) // reinsert the whole torrent, passing empty selection
if info != nil && info.Progress == 100 {
err = t.checkIfBroken(info, brokenFiles)
if utils.IsBWLimitExceeded(err) {
if utils.AreAllTokensExpired(err) {
t.repairLog.Warnf("Your account has reached the bandwidth limit, cannot continue repairing torrent %s", t.GetKey(torrent))
return
}
@@ -332,14 +333,13 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
expiredCount := 0
rarCount := 0
unassignedCount := 0
newUnassignedLinks := cmap.New[*realdebrid.Download]()
var assignedLinks []string
bwLimitReached := false
torrent.UnassignedLinks.Clone().Each(func(link string) bool {
// unrestrict each unassigned link that was filled out during torrent init
unrestrict, err := t.rd.UnrestrictAndVerify(link)
if utils.IsBWLimitExceeded(err) {
if utils.AreAllTokensExpired(err) {
bwLimitReached = true
return true
}
@@ -377,7 +377,18 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
// it's possible that it is already repaired
t.repairLog.Warnf("Cannot assign %s to any file in torrent %s", unrestrict.Filename, t.GetKey(torrent))
}
newUnassignedLinks.Set(link, unrestrict)
torrent.SelectedFiles.Set(unrestrict.Filename, &File{
File: realdebrid.File{
ID: 0,
Path: unrestrict.Filename,
Bytes: unrestrict.Filesize,
Selected: 0,
},
Ended: torrent.Added,
Link: unrestrict.Link,
State: NewFileState("ok_file"),
})
}
processedCount := assignedCount + unassignedCount + expiredCount
@@ -416,20 +427,6 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
return false // end repair
}
newUnassignedLinks.IterCb(func(_ string, unassigned *realdebrid.Download) {
torrent.SelectedFiles.Set(unassigned.Filename, &File{
File: realdebrid.File{
ID: 0,
Path: unassigned.Filename,
Bytes: unassigned.Filesize,
Selected: 0,
},
Ended: torrent.Added,
Link: unassigned.Link,
State: NewFileState("ok_file"),
})
})
if action == "extract" {
videoFiles := []string{}
torrent.SelectedFiles.IterCb(func(_ string, file *File) {