Allow detection of waiting_file_selection
This commit is contained in:
@@ -22,7 +22,7 @@ func LoadZurgConfig(filename string, log *logutil.Logger) (ConfigInterface, erro
|
||||
|
||||
switch initialConfig.Version {
|
||||
case "v1":
|
||||
log.Debug("Detected config version: v1")
|
||||
log.Debug("Config version: v1")
|
||||
return loadV1Config(content, log)
|
||||
|
||||
default:
|
||||
|
||||
@@ -32,7 +32,7 @@ func HandleDeleteFile(directory, torrentName, fileName string, torMgr *torrent.T
|
||||
return fmt.Errorf("cannot find file %s", fileName)
|
||||
}
|
||||
file.Link = "unselect"
|
||||
if torMgr.CheckDeletedState(torrent) {
|
||||
if torMgr.CheckDeletedStatus(torrent) {
|
||||
torMgr.Delete(torrentName, true)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -2,7 +2,7 @@ package torrent
|
||||
|
||||
import cmap "github.com/orcaman/concurrent-map/v2"
|
||||
|
||||
func (t *TorrentManager) CheckDeletedState(torrent *Torrent) bool {
|
||||
func (t *TorrentManager) CheckDeletedStatus(torrent *Torrent) bool {
|
||||
var unselectedIDs []int
|
||||
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
|
||||
if file.Link == "unselect" {
|
||||
|
||||
@@ -7,23 +7,107 @@ import (
|
||||
)
|
||||
|
||||
type LibraryState struct {
|
||||
TotalCount int
|
||||
FirstTorrent *realdebrid.Torrent
|
||||
DownloadingCount int
|
||||
TotalCount int
|
||||
ActiveCount int
|
||||
FirstTorrent realdebrid.Torrent
|
||||
FirstActiveTorrent realdebrid.Torrent
|
||||
}
|
||||
|
||||
func (ls LibraryState) equal(a LibraryState) bool {
|
||||
return a.TotalCount == ls.TotalCount && a.FirstTorrent.ID == ls.FirstTorrent.ID && a.DownloadingCount == ls.DownloadingCount
|
||||
return a.TotalCount == ls.TotalCount &&
|
||||
a.ActiveCount == ls.ActiveCount &&
|
||||
a.FirstTorrent.ID == ls.FirstTorrent.ID &&
|
||||
a.FirstTorrent.Status == ls.FirstTorrent.Status &&
|
||||
a.FirstActiveTorrent.ID == ls.FirstActiveTorrent.ID &&
|
||||
a.FirstActiveTorrent.Status == ls.FirstActiveTorrent.Status
|
||||
}
|
||||
|
||||
func EmptyState() LibraryState {
|
||||
oldestTime := time.Time{}
|
||||
empty := realdebrid.Torrent{
|
||||
ID: "",
|
||||
Added: (time.Time{}).Format(time.RFC3339),
|
||||
}
|
||||
return LibraryState{
|
||||
TotalCount: 0,
|
||||
FirstTorrent: &realdebrid.Torrent{
|
||||
ID: "",
|
||||
Added: oldestTime.Format(time.RFC3339),
|
||||
},
|
||||
DownloadingCount: 0,
|
||||
TotalCount: 0,
|
||||
ActiveCount: 0,
|
||||
FirstTorrent: empty,
|
||||
FirstActiveTorrent: empty,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TorrentManager) SetNewLatestState(checksum LibraryState) {
|
||||
t.latestState.ActiveCount = checksum.ActiveCount
|
||||
t.latestState.TotalCount = checksum.TotalCount
|
||||
t.latestState.FirstTorrent = checksum.FirstTorrent
|
||||
t.latestState.FirstActiveTorrent = checksum.FirstActiveTorrent
|
||||
}
|
||||
|
||||
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 {
|
||||
torrentChan := make(chan torrentsResp, 1)
|
||||
activeChan := make(chan torrentsResp, 1)
|
||||
countChan := make(chan int, 1)
|
||||
errChan := make(chan error, 3) // accommodate errors from both goroutines
|
||||
defer close(torrentChan)
|
||||
defer close(activeChan)
|
||||
defer close(countChan)
|
||||
defer close(errChan)
|
||||
|
||||
_ = t.workerPool.Submit(func() {
|
||||
torrents, totalCount, err := t.Api.GetTorrents(1, false)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
torrentChan <- torrentsResp{torrents: torrents, totalCount: totalCount}
|
||||
})
|
||||
|
||||
_ = t.workerPool.Submit(func() {
|
||||
torrents, totalCount, err := t.Api.GetTorrents(1, true)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
activeChan <- 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 first realdebrid.Torrent
|
||||
var active realdebrid.Torrent
|
||||
var totalCount, count int
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
select {
|
||||
case firstResp := <-torrentChan:
|
||||
first = firstResp.torrents[0]
|
||||
totalCount = firstResp.totalCount
|
||||
case activeResp := <-activeChan:
|
||||
active = activeResp.torrents[0]
|
||||
case count = <-countChan:
|
||||
case err := <-errChan:
|
||||
t.log.Warnf("Checksum API Error: %v\n", err)
|
||||
return EmptyState()
|
||||
}
|
||||
}
|
||||
|
||||
return LibraryState{
|
||||
TotalCount: totalCount,
|
||||
ActiveCount: count,
|
||||
FirstTorrent: first,
|
||||
FirstActiveTorrent: active,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +85,7 @@ func NewTorrentManager(cfg config.ConfigInterface, api *realdebrid.RealDebrid, p
|
||||
}
|
||||
|
||||
t.RefreshTorrents()
|
||||
t.SetNewLatestState(t.getCurrentState())
|
||||
|
||||
if t.Config.EnableRepair() {
|
||||
repairWorker, err := ants.NewPool(1)
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user