Rewrite state machines

This commit is contained in:
Ben Sarmiento
2024-05-21 17:07:40 +02:00
parent 6c24d74f61
commit 0743b01223
12 changed files with 69 additions and 55 deletions

View File

@@ -1,6 +1,7 @@
package torrent
import (
"context"
"fmt"
"path/filepath"
"strings"
@@ -130,12 +131,10 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
if t.Config.EnableRepair() {
if isInitialRun {
t.removeExpiredFixers(instances)
t.processFixers(instances)
} else {
t.workerPool.Submit(func() {
t.processFixers(instances)
})
}
t.workerPool.Submit(func() {
t.processFixers(instances)
})
}
return updatedPaths
@@ -198,7 +197,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
OriginalName: info.OriginalName,
Added: info.Added,
Hash: info.Hash,
State: NewTorrentState("ok"),
State: NewTorrentState("broken_torrent"),
}
// SelectedFiles is a subset of Files with only the selected ones
@@ -214,16 +213,23 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
File: file,
Ended: info.Ended,
Link: "", // no link yet, consider it broken
State: NewFileState("broken"),
State: NewFileState("broken_file"),
})
}
if len(selectedFiles) == len(info.Links) {
// all links are still intact! good!
for i, file := range selectedFiles {
file.Link = info.Links[i]
file.State.SetState("ok")
err := file.State.Event(context.Background(), "repair_file")
if err != nil {
t.log.Warnf("Cannot repair file %s: %v", file.Path, err)
}
}
torrent.UnassignedLinks = mapset.NewSet[string]()
err := torrent.State.Event(context.Background(), "repair_torrent")
if err != nil {
t.log.Warnf("Cannot repair torrent %s: %v", torrent.Hash, err)
}
} else {
torrent.UnassignedLinks = mapset.NewSet[string](info.Links...)
}
@@ -315,9 +321,12 @@ func (t *TorrentManager) mergeToMain(existing, toMerge *Torrent) *Torrent {
older.SelectedFiles.IterCb(func(key string, olderFile *File) {
if !mainTorrent.SelectedFiles.Has(key) {
mainTorrent.SelectedFiles.Set(key, olderFile)
} else if olderFile.State.Is("deleted") {
} else if olderFile.State.Is("deleted_file") {
newerFile, _ := mainTorrent.SelectedFiles.Get(key)
newerFile.State.SetState("deleted")
err := newerFile.State.Event(context.Background(), "delete_file")
if err != nil {
t.log.Warnf("Cannot delete file %s: %v", key, err)
}
}
})
t.CheckDeletedStatus(&mainTorrent)