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

@@ -101,7 +101,7 @@ func (t *TorrentManager) repairAll(torrent *Torrent) {
// check 1: for broken files
brokenFileIDs := mapset.NewSet[int]()
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.State.Is("broken") {
if file.State.Is("broken_file") {
brokenFileIDs.Add(file.ID)
}
})
@@ -257,7 +257,6 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
unrestrict := t.UnrestrictLinkUntilOk(link)
if unrestrict == nil {
expiredCount++
// newUnassignedLinks.Set(link, nil)
return false // next
}
@@ -265,9 +264,13 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
assigned := false
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
// base it on size because why not?
if (unrestrict.Filesize > 1_000_000 && file.Bytes == unrestrict.Filesize) || strings.HasSuffix(strings.ToLower(file.Path), strings.ToLower(unrestrict.Filename)) {
if !assigned && file.State.Is("broken_file") && ((unrestrict.Filesize > 1_000_000 && file.Bytes == unrestrict.Filesize) || strings.HasSuffix(strings.ToLower(file.Path), strings.ToLower(unrestrict.Filename))) {
file.Link = link
file.State.SetState("ok")
err := file.State.Event(context.Background(), "repair_done_file")
if err != nil {
t.log.Errorf("Failed to mark file %s as repaired: %v", file.Path, err)
return
}
assigned = true
assignedCount++
}
@@ -290,6 +293,10 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
return false
})
t.log.Debugf("Assigned %d links to the %d selected files of torrent %s", assignedCount, torrent.SelectedFiles.Count(), t.GetKey(torrent))
t.log.Debugf("Expired %d links to the %d selected files of torrent %s", expiredCount, torrent.SelectedFiles.Count(), t.GetKey(torrent))
t.log.Debugf("Rar'ed %d links to the %d selected files of torrent %s", rarCount, torrent.SelectedFiles.Count(), t.GetKey(torrent))
t.log.Debugf("Unassigned %d links to the %d selected files of torrent %s", newUnassignedLinks.Count(), torrent.SelectedFiles.Count(), t.GetKey(torrent))
if assignedCount == 0 && rarCount == 1 {
// this is a rar'ed torrent, nothing we can do
@@ -311,7 +318,7 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
},
Ended: torrent.Added,
Link: unassigned.Link,
State: NewFileState("ok"),
State: NewFileState("ok_file"),
}
torrent.SelectedFiles.Set(unassigned.Filename, newFile)
})
@@ -473,11 +480,11 @@ func (t *TorrentManager) canCapacityHandle() bool {
}
func (t *TorrentManager) markAsUnplayable(torrent *Torrent, reason string) {
if torrent.State.Is("unplayable") {
if torrent.State.Is("unplayable_torrent") {
return
}
t.log.Warnf("Marking torrent %s as unplayable - %s", t.GetKey(torrent), reason)
err := torrent.State.Event(context.Background(), "mark_as_unplayable")
err := torrent.State.Event(context.Background(), "mark_as_unplayable_torrent")
if err != nil {
t.log.Errorf("Failed to mark torrent %s as unplayable: %v", t.GetKey(torrent), err)
return
@@ -500,7 +507,7 @@ func getBrokenFiles(torrent *Torrent) ([]*File, bool) {
var brokenFiles []*File
allBroken := true
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.State.Is("broken") {
if file.State.Is("broken_file") {
brokenFiles = append(brokenFiles, file)
} else {
allBroken = false
@@ -520,18 +527,22 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles
selectedFiles = append(selectedFiles, &File{
File: file,
Ended: info.Ended,
Link: "", // no link yet
State: NewFileState("broken"),
Link: "",
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.Errorf("Failed to mark file %s as repaired: %v", file.Path, err)
return true
}
}
} else {
// if we can't assign links, it's still broken
// if we can't assign links then it's still broken
return true
}