Do not repair if uncached check fails
This commit is contained in:
56
internal/torrent/uncached.go
Normal file
56
internal/torrent/uncached.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
mapset "github.com/deckarep/golang-set/v2"
|
||||
)
|
||||
|
||||
func (t *TorrentManager) getUncachedTorrents() ([]*Torrent, error) {
|
||||
t.log.Debug("Checking if torrents are still cached")
|
||||
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
|
||||
|
||||
var hashGroups []mapset.Set[string]
|
||||
const maxGroupSize = 399
|
||||
|
||||
currentGroup := mapset.NewSet[string]()
|
||||
hashGroups = append(hashGroups, currentGroup)
|
||||
|
||||
allTorrents.IterCb(func(_ string, torrent *Torrent) {
|
||||
if torrent.AnyInProgress() || torrent.UnrepairableReason != "" {
|
||||
return
|
||||
}
|
||||
|
||||
if currentGroup.Cardinality() >= maxGroupSize {
|
||||
currentGroup = mapset.NewSet[string]()
|
||||
hashGroups = append(hashGroups, currentGroup)
|
||||
}
|
||||
currentGroup.Add(torrent.Hash)
|
||||
})
|
||||
|
||||
var availabilityChecks = make(map[string]bool)
|
||||
for i := range hashGroups {
|
||||
if hashGroups[i].Cardinality() == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
resp, err := t.Api.AvailabilityCheck(hashGroups[i].ToSlice())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("availability check is incomplete, skipping uncached check: %v", err)
|
||||
}
|
||||
for hash, hosterHash := range resp {
|
||||
// Check if HosterHash is a map (Variants field is used)
|
||||
availabilityChecks[hash] = len(hosterHash.Variants) > 0
|
||||
}
|
||||
}
|
||||
|
||||
var uncachedTorrents []*Torrent
|
||||
allTorrents.IterCb(func(_ string, torrent *Torrent) {
|
||||
if _, ok := availabilityChecks[torrent.Hash]; !ok || !availabilityChecks[torrent.Hash] {
|
||||
uncachedTorrents = append(uncachedTorrents, torrent)
|
||||
}
|
||||
})
|
||||
t.log.Debugf("Found %d torrents that are no longer cached", len(uncachedTorrents))
|
||||
|
||||
return uncachedTorrents, nil
|
||||
}
|
||||
Reference in New Issue
Block a user