Rework repairs again
This commit is contained in:
96
internal/torrent/fixer.go
Normal file
96
internal/torrent/fixer.go
Normal file
@@ -0,0 +1,96 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
|
||||
cmap "github.com/orcaman/concurrent-map/v2"
|
||||
)
|
||||
|
||||
// fixers are commands that will be run on the next refresh
|
||||
// they are stored in a file so that they can be run on startup
|
||||
// they follow the format of:
|
||||
// <id_trigger>: <command> <id>
|
||||
// id_trigger = this means a specific torrent id's completion
|
||||
// commands: delete | repair
|
||||
|
||||
func (t *TorrentManager) fixerAddCommand(trigger, command string) {
|
||||
t.fixers.Set(trigger, command)
|
||||
t.writeFixersToFile()
|
||||
}
|
||||
|
||||
func (t *TorrentManager) handleFixers() {
|
||||
var toDelete []string
|
||||
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
|
||||
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
|
||||
infoCache.IterCb(func(id string, torrent *Torrent) {
|
||||
if !t.fixers.Has(id) || torrent.AnyInProgress() {
|
||||
return
|
||||
}
|
||||
command, _ := t.fixers.Pop(id)
|
||||
switch command {
|
||||
case "delete":
|
||||
toDelete = append(toDelete, id)
|
||||
case "repair":
|
||||
toDelete = append(toDelete, id)
|
||||
t.log.Debugf("Repairing torrent %s again now that fixer is done", t.GetKey(torrent))
|
||||
repairMe, _ := allTorrents.Get(t.GetKey(torrent))
|
||||
t.TriggerRepair(repairMe)
|
||||
}
|
||||
})
|
||||
for _, id := range toDelete {
|
||||
t.log.Debugf("Deleting fixer torrent id=%s", id)
|
||||
t.Api.DeleteTorrent(id)
|
||||
infoCache.Remove(id)
|
||||
t.deleteTorrentFile(id)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TorrentManager) writeFixersToFile() {
|
||||
filePath := "data/fixers.json"
|
||||
file, err := os.Create(filePath)
|
||||
if err != nil {
|
||||
t.log.Warnf("Cannot create fixer file %s: %v", filePath, err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
fileData, err := t.fixers.MarshalJSON()
|
||||
if err != nil {
|
||||
t.log.Warnf("Cannot marshal fixers: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = file.Write(fileData)
|
||||
if err != nil {
|
||||
t.log.Warnf("Cannot write to fixer file %s: %v", filePath, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TorrentManager) readFixersFromFile() (ret cmap.ConcurrentMap[string, string]) {
|
||||
ret = cmap.New[string]()
|
||||
filePath := "data/fixers.json"
|
||||
file, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return
|
||||
}
|
||||
t.log.Warnf("Cannot open fixer file %s: %v", filePath, err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
fileData, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
t.log.Warnf("Cannot read fixer file %s: %v", filePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
err = ret.UnmarshalJSON(fileData)
|
||||
if err != nil {
|
||||
t.log.Warnf("Cannot unmarshal fixer file %s: %v", filePath, err)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Reference in New Issue
Block a user