Implement new fsm

This commit is contained in:
Ben Sarmiento
2024-05-21 01:59:53 +02:00
parent d5dd9426ed
commit 2c5e7a1db0
14 changed files with 121 additions and 59 deletions

View File

@@ -1,6 +1,7 @@
package torrent
import (
"context"
"fmt"
"math"
"strings"
@@ -100,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.IsBroken && !file.IsDeleted {
if file.State.Is("broken") {
brokenFileIDs.Add(file.ID)
}
})
@@ -266,7 +267,7 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
// 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)) {
file.Link = link
file.IsBroken = false
file.State.SetState("ok")
assigned = true
assignedCount++
}
@@ -308,9 +309,9 @@ func (t *TorrentManager) assignUnassignedLinks(torrent *Torrent) bool {
Bytes: unassigned.Filesize,
Selected: 0,
},
Ended: torrent.Added,
Link: unassigned.Link,
IsBroken: false,
Ended: torrent.Added,
Link: unassigned.Link,
State: NewFileState("ok"),
}
torrent.SelectedFiles.Set(unassigned.Filename, newFile)
})
@@ -473,6 +474,11 @@ func (t *TorrentManager) canCapacityHandle() bool {
func (t *TorrentManager) markAsUnplayable(torrent *Torrent, reason string) {
t.log.Warnf("Marking torrent %s as unplayable - %s", t.GetKey(torrent), reason)
err := torrent.State.Event(context.Background(), "mark_as_unplayable")
if err != nil {
t.log.Errorf("Failed to mark torrent %s as unplayable: %v", t.GetKey(torrent), err)
return
}
t.DirectoryMap.IterCb(func(directory string, torrents cmap.ConcurrentMap[string, *Torrent]) {
torrents.Remove(t.GetKey(torrent))
})
@@ -491,7 +497,7 @@ func getBrokenFiles(torrent *Torrent) ([]*File, bool) {
var brokenFiles []*File
allBroken := true
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if file.IsBroken && !file.IsDeleted {
if file.State.Is("broken") {
brokenFiles = append(brokenFiles, file)
} else {
allBroken = false
@@ -509,17 +515,17 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles
continue
}
selectedFiles = append(selectedFiles, &File{
File: file,
Ended: info.Ended,
Link: "", // no link yet
IsBroken: true,
File: file,
Ended: info.Ended,
Link: "", // no link yet
State: NewFileState("broken"),
})
}
if len(selectedFiles) == len(info.Links) {
// all links are still intact! good!
for i, file := range selectedFiles {
file.Link = info.Links[i]
file.IsBroken = false
file.State.SetState("ok")
}
} else {
// if we can't assign links, it's still broken