Refactor workers
This commit is contained in:
@@ -37,74 +37,35 @@ func (t *TorrentManager) SetNewLatestState(checksum LibraryState) {
|
||||
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 all goroutines
|
||||
defer close(torrentChan)
|
||||
defer close(activeChan)
|
||||
defer close(countChan)
|
||||
defer close(errChan)
|
||||
var state LibraryState
|
||||
|
||||
_ = t.workerPool.Submit(func() {
|
||||
torrents, totalCount, err := t.Api.GetTorrents(1, false)
|
||||
if err != nil {
|
||||
errChan <- err
|
||||
return
|
||||
}
|
||||
torrentChan <- torrentsResp{torrents: torrents, totalCount: totalCount}
|
||||
})
|
||||
torrents, totalCount, err := t.Api.GetTorrents(1, false)
|
||||
if err != nil {
|
||||
t.log.Warnf("Checksum API Error (GetTorrents): %v", err)
|
||||
return LibraryState{}
|
||||
}
|
||||
if len(torrents) > 0 {
|
||||
state.FirstTorrent = &torrents[0]
|
||||
}
|
||||
state.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
|
||||
})
|
||||
|
||||
var first, active *realdebrid.Torrent
|
||||
var totalCount, count int
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
select {
|
||||
case firstResp := <-torrentChan:
|
||||
if len(firstResp.torrents) > 0 {
|
||||
first = &firstResp.torrents[0]
|
||||
}
|
||||
totalCount = firstResp.totalCount
|
||||
case activeResp := <-activeChan:
|
||||
if len(activeResp.torrents) > 0 {
|
||||
active = &activeResp.torrents[0]
|
||||
}
|
||||
case count = <-countChan:
|
||||
case err := <-errChan:
|
||||
t.log.Warnf("Checksum API Error: %v", err)
|
||||
return LibraryState{}
|
||||
}
|
||||
activeTorrents, _, err := t.Api.GetTorrents(1, true)
|
||||
if err != nil {
|
||||
t.log.Warnf("Checksum API Error (GetTorrents): %v", err)
|
||||
return LibraryState{}
|
||||
}
|
||||
if len(activeTorrents) > 0 {
|
||||
state.FirstActiveTorrent = &activeTorrents[0]
|
||||
}
|
||||
|
||||
return LibraryState{
|
||||
TotalCount: totalCount,
|
||||
ActiveCount: count,
|
||||
FirstTorrent: first,
|
||||
FirstActiveTorrent: active,
|
||||
count, err := t.Api.GetActiveTorrentCount()
|
||||
if err != nil {
|
||||
t.log.Warnf("Checksum API Error (GetActiveTorrentCount): %v", err)
|
||||
return LibraryState{}
|
||||
}
|
||||
state.ActiveCount = count.DownloadingCount
|
||||
|
||||
return state
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user