Rework repair logic

This commit is contained in:
Ben Sarmiento
2024-01-26 08:36:30 +01:00
parent b2bd188736
commit 92a507c693
5 changed files with 179 additions and 137 deletions

View File

@@ -23,43 +23,23 @@ func (t *TorrentManager) RefreshTorrents() []string {
var wg sync.WaitGroup
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
expiredFixers := t.ensureDelete.Clone()
doesNotExist := t.deleteOnceDone.Clone()
for i := range instances {
idx := i
expiredFixers.Remove(instances[idx].ID)
doesNotExist.Remove(instances[idx].ID)
wg.Add(1)
_ = t.workerPool.Submit(func() {
defer wg.Done()
if !t.fixers.Has(instances[idx].ID) {
// not a fixer, just regular torrent
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 {
t.log.Warnf("Cannot redownload torrent %s after fixer is done: %v", t.GetKey(torrent), err)
infoChan <- nil
return
}
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
// fixer is done, let's check if it fixed the torrent
infoChan <- t.handleFixers(instances[idx])
return
}
infoChan <- nil
})
}
@@ -68,21 +48,21 @@ func (t *TorrentManager) RefreshTorrents() []string {
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)
doesNotExist.Each(func(fixerID string) bool {
t.fixers.Remove(fixerID)
t.ensureDelete.Remove(fixerID)
t.deleteOnceDone.Remove(fixerID)
return false
})
// ensure delete
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
t.ensureDelete.Each(func(fixerID string) bool {
t.deleteOnceDone.Each(func(fixerID string) bool {
torrent, exists := infoCache.Get(fixerID)
if exists && torrent.AnyInProgress() {
return false
}
t.log.Debugf("Ensuring that torrent id=%s is deleted", fixerID)
t.Delete(t.GetKey(torrent), true)
t.Api.DeleteTorrent(fixerID)
return false
})
@@ -260,12 +240,13 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) Torrent {
mainTorrent := Torrent{
Name: existing.Name,
OriginalName: existing.OriginalName,
Rename: existing.Rename,
Hash: existing.Hash,
DownloadedIDs: mapset.NewSet[string](),
InProgressIDs: mapset.NewSet[string](),
Name: existing.Name,
OriginalName: existing.OriginalName,
Rename: existing.Rename,
Hash: existing.Hash,
DownloadedIDs: mapset.NewSet[string](),
InProgressIDs: mapset.NewSet[string](),
// UnassignedLinks: mapset.NewSet[string](),
UnassignedLinks: existing.UnassignedLinks.Union(toMerge.UnassignedLinks),
BrokenLinks: existing.BrokenLinks.Union(toMerge.BrokenLinks),
}