Allow detection of waiting_file_selection

This commit is contained in:
Ben Sarmiento
2023-12-09 00:50:42 +01:00
parent 4dcd2e64bb
commit 2281b58b59
9 changed files with 115 additions and 87 deletions

View File

@@ -14,13 +14,14 @@ import (
)
func (t *TorrentManager) RefreshTorrents() []string {
instances, _, err := t.Api.GetTorrents(0)
instances, _, err := t.Api.GetTorrents(0, false)
if err != nil {
t.log.Warnf("Cannot get torrents: %v\n", err)
return nil
}
infoChan := make(chan *Torrent, len(instances))
var wg sync.WaitGroup
for i := range instances {
wg.Add(1)
idx := i // capture the loop variable
@@ -29,6 +30,7 @@ func (t *TorrentManager) RefreshTorrents() []string {
infoChan <- t.getMoreInfo(instances[idx])
})
}
wg.Wait()
close(infoChan)
t.log.Debugf("Fetched info for %d torrents", len(instances))
@@ -41,7 +43,9 @@ func (t *TorrentManager) RefreshTorrents() []string {
noInfoCount++
continue
}
freshKeys.Add(info.AccessKey)
if !info.AnyInProgress() {
freshKeys.Add(info.AccessKey)
}
if torrent, exists := allTorrents.Get(info.AccessKey); !exists {
allTorrents.Set(info.AccessKey, info)
} else if !strset.Difference(info.DownloadedIDs, torrent.DownloadedIDs).IsEmpty() {
@@ -80,12 +84,6 @@ func (t *TorrentManager) RefreshTorrents() []string {
return updatedPaths
}
func (t *TorrentManager) SetNewLatestState(checksum LibraryState) {
t.latestState.DownloadingCount = checksum.DownloadingCount
t.latestState.FirstTorrent = checksum.FirstTorrent
t.latestState.TotalCount = checksum.TotalCount
}
// startRefreshJob periodically refreshes the torrents
func (t *TorrentManager) startRefreshJob() {
t.log.Info("Starting periodic refresh")
@@ -115,11 +113,11 @@ func (t *TorrentManager) startRefreshJob() {
// getMoreInfo gets original name, size and files for a torrent
func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
if tor, exists := infoCache.Get(rdTorrent.ID); exists && tor.SelectedFiles.Count() == len(rdTorrent.Links) {
return tor
if torrentFromCache, exists := infoCache.Get(rdTorrent.ID); exists && !torrentFromCache.AnyInProgress() && torrentFromCache.SelectedFiles.Count() == len(rdTorrent.Links) {
return torrentFromCache
}
torrentFromFile := t.readTorrentFromFile(rdTorrent.ID)
if torrentFromFile != nil && torrentFromFile.SelectedFiles.Count() == len(rdTorrent.Links) {
if torrentFromFile != nil && !torrentFromFile.AnyInProgress() && torrentFromFile.SelectedFiles.Count() == len(rdTorrent.Links) {
infoCache.Set(rdTorrent.ID, torrentFromFile)
return torrentFromFile
}
@@ -244,63 +242,3 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
return mainTorrent
}
type torrentsResp struct {
torrents []realdebrid.Torrent
totalCount int
}
// generates a checksum based on the number of torrents, the first torrent id and the number of active torrents
func (t *TorrentManager) getCurrentState() LibraryState {
torrentsChan := make(chan torrentsResp, 1)
countChan := make(chan int, 1)
errChan := make(chan error, 2) // accommodate errors from both goroutines
defer close(torrentsChan)
defer close(countChan)
defer close(errChan)
_ = t.workerPool.Submit(func() {
torrents, totalCount, err := t.Api.GetTorrents(1)
if err != nil {
errChan <- err
return
}
torrentsChan <- torrentsResp{torrents: torrents, totalCount: totalCount}
})
_ = t.workerPool.Submit(func() {
count, err := t.Api.GetActiveTorrentCount()
if err != nil {
errChan <- err
return
}
countChan <- count.DownloadingCount
})
// Existing goroutines for GetTorrents and GetActiveTorrentCount
var torrents []realdebrid.Torrent
var totalCount, count int
for i := 0; i < 2; i++ {
select {
case resp := <-torrentsChan:
torrents = resp.torrents
totalCount = resp.totalCount
case count = <-countChan:
case err := <-errChan:
t.log.Warnf("Checksum API Error: %v\n", err)
return EmptyState()
}
}
if len(torrents) == 0 {
t.log.Error("Huh, no torrents returned")
return EmptyState()
}
return LibraryState{
TotalCount: totalCount,
FirstTorrent: &torrents[0],
DownloadingCount: count,
}
}