Surefire way of no dupes

This commit is contained in:
Ben Sarmiento
2024-01-22 22:36:29 +01:00
parent d601e384f7
commit 6cd9db58d2
2 changed files with 34 additions and 18 deletions

View File

@@ -23,18 +23,27 @@ func (t *TorrentManager) RefreshTorrents() []string {
var wg sync.WaitGroup var wg sync.WaitGroup
allTorrents, _ := t.DirectoryMap.Get(INT_ALL) allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
expiredFixers := mapset.NewSet[string](t.fixers.Keys()...)
for i := range instances { for i := range instances {
idx := i idx := i
expiredFixers.Remove(instances[idx].ID)
wg.Add(1) wg.Add(1)
_ = t.workerPool.Submit(func() { _ = t.workerPool.Submit(func() {
defer wg.Done() defer wg.Done()
if instances[idx].IsDone() && t.fixers.Has(instances[idx].ID) { if !t.fixers.Has(instances[idx].ID) {
infoChan <- t.getMoreInfo(instances[idx])
return
} else if instances[idx].IsDone() {
fixer := instances[idx] fixer := instances[idx]
torrent, _ := t.fixers.Pop(fixer.ID) torrent, _ := t.fixers.Pop(fixer.ID)
t.log.Debugf("Fixer %s is done, let's check if it fixed torrent %s by redownloading", instances[idx].ID, t.GetKey(torrent)) t.log.Debugf("Fixer %s is done, let's check if it fixed torrent %s by redownloading", instances[idx].ID, t.GetKey(torrent))
brokenFiles := getBrokenFiles(torrent) brokenFiles := getBrokenFiles(torrent)
info, err := t.redownloadTorrent(torrent, "") info, err := t.redownloadTorrent(torrent, "")
if err == nil { if err != nil {
t.log.Warnf("Cannot redownload torrent %s after fixer is done: %v", t.GetKey(torrent), err)
infoChan <- nil
return
}
if info.IsDone() { if info.IsDone() {
if t.isStillBroken(info, brokenFiles) { if t.isStillBroken(info, brokenFiles) {
t.log.Warnf("Fixer is done but torrent %s is still broken; let's keep the fixer", t.GetKey(torrent)) t.log.Warnf("Fixer is done but torrent %s is still broken; let's keep the fixer", t.GetKey(torrent))
@@ -49,11 +58,7 @@ func (t *TorrentManager) RefreshTorrents() []string {
t.Api.DeleteTorrent(fixer.ID) // delete the fixer t.Api.DeleteTorrent(fixer.ID) // delete the fixer
infoChan <- nil infoChan <- nil
return return
} else {
t.log.Warnf("Cannot redownload torrent %s after fixer is done: %v", t.GetKey(torrent), err)
}
} else if !t.fixers.Has(instances[idx].ID) {
infoChan <- t.getMoreInfo(instances[idx])
} }
}) })
} }
@@ -62,6 +67,13 @@ func (t *TorrentManager) RefreshTorrents() []string {
close(infoChan) close(infoChan)
t.log.Debugf("Fetched info for %d torrents", len(instances)) t.log.Debugf("Fetched info for %d torrents", len(instances))
// delete expired fixers
expiredFixers.Each(func(fixerID string) bool {
t.log.Debugf("Deleting expired fixer %s", fixerID)
t.fixers.Remove(fixerID)
return false
})
freshKeys := mapset.NewSet[string]() freshKeys := mapset.NewSet[string]()
noInfoCount := 0 noInfoCount := 0
for info := range infoChan { for info := range infoChan {

View File

@@ -297,6 +297,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string)
err = t.Api.SelectTorrentFiles(newTorrentID, brokenFiles) err = t.Api.SelectTorrentFiles(newTorrentID, brokenFiles)
if err != nil { if err != nil {
t.Api.DeleteTorrent(newTorrentID) t.Api.DeleteTorrent(newTorrentID)
t.fixers.Set(newTorrentID, torrent)
return nil, fmt.Errorf("cannot start redownloading: %v", err) return nil, fmt.Errorf("cannot start redownloading: %v", err)
} }
@@ -304,6 +305,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string)
info, err := t.Api.GetTorrentInfo(newTorrentID) info, err := t.Api.GetTorrentInfo(newTorrentID)
if err != nil { if err != nil {
t.Api.DeleteTorrent(newTorrentID) t.Api.DeleteTorrent(newTorrentID)
t.fixers.Set(newTorrentID, torrent)
return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err) return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err)
} }
@@ -319,6 +321,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string)
} }
if !isOkStatus { if !isOkStatus {
t.Api.DeleteTorrent(newTorrentID) t.Api.DeleteTorrent(newTorrentID)
t.fixers.Set(newTorrentID, torrent)
return nil, fmt.Errorf("the redownloaded torrent %s (id=%s) is in error state: %s", t.GetKey(torrent), newTorrentID, info.Status) return nil, fmt.Errorf("the redownloaded torrent %s (id=%s) is in error state: %s", t.GetKey(torrent), newTorrentID, info.Status)
} }
@@ -337,6 +340,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string)
brokenCount := len(strings.Split(brokenFiles, ",")) brokenCount := len(strings.Split(brokenFiles, ","))
if len(info.Links) != brokenCount { if len(info.Links) != brokenCount {
t.Api.DeleteTorrent(newTorrentID) t.Api.DeleteTorrent(newTorrentID)
t.fixers.Set(newTorrentID, torrent)
return nil, fmt.Errorf("it did not fix the issue for %s (id=%s), only got %d files but we need %d, undoing", t.GetKey(torrent), info.ID, len(info.Links), brokenCount) return nil, fmt.Errorf("it did not fix the issue for %s (id=%s), only got %d files but we need %d, undoing", t.GetKey(torrent), info.ID, len(info.Links), brokenCount)
} }