Surefire way of no dupes
This commit is contained in:
@@ -23,37 +23,42 @@ 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 {
|
||||||
if info.IsDone() {
|
t.log.Warnf("Cannot redownload torrent %s after fixer is done: %v", t.GetKey(torrent), err)
|
||||||
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
|
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) {
|
if info.IsDone() {
|
||||||
infoChan <- t.getMoreInfo(instances[idx])
|
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)
|
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 {
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user