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,37 +23,42 @@ func (t *TorrentManager) RefreshTorrents() []string {
var wg sync.WaitGroup
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
expiredFixers := mapset.NewSet[string](t.fixers.Keys()...)
for i := range instances {
idx := i
expiredFixers.Remove(instances[idx].ID)
wg.Add(1)
_ = t.workerPool.Submit(func() {
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]
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))
brokenFiles := getBrokenFiles(torrent)
info, err := t.redownloadTorrent(torrent, "")
if err == nil {
if info.IsDone() {
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))
infoChan <- t.getMoreInfo(fixer)
return
} else {
t.log.Infof("Fixer resolved issues for torrent %s, broken files are repaired", t.GetKey(torrent))
}
} else {
t.log.Warnf("Torrent %s is still not done after redownload; likely the fixer did its job", t.GetKey(torrent))
}
t.Api.DeleteTorrent(fixer.ID) // delete the fixer
if err != nil {
t.log.Warnf("Cannot redownload torrent %s after fixer is done: %v", t.GetKey(torrent), err)
infoChan <- nil
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])
if info.IsDone() {
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))
infoChan <- t.getMoreInfo(fixer)
return
} else {
t.log.Infof("Fixer resolved issues for torrent %s, broken files are repaired", t.GetKey(torrent))
}
} else {
t.log.Warnf("Torrent %s is still not done after redownload; likely the fixer did its job", t.GetKey(torrent))
}
t.Api.DeleteTorrent(fixer.ID) // delete the fixer
infoChan <- nil
return
}
})
}
@@ -62,6 +67,13 @@ func (t *TorrentManager) RefreshTorrents() []string {
close(infoChan)
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]()
noInfoCount := 0
for info := range infoChan {

View File

@@ -297,6 +297,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string)
err = t.Api.SelectTorrentFiles(newTorrentID, brokenFiles)
if err != nil {
t.Api.DeleteTorrent(newTorrentID)
t.fixers.Set(newTorrentID, torrent)
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)
if err != nil {
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)
}
@@ -319,6 +321,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string)
}
if !isOkStatus {
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)
}
@@ -337,6 +340,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string)
brokenCount := len(strings.Split(brokenFiles, ","))
if len(info.Links) != brokenCount {
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)
}