Prepare materials for auto heal functionality

This commit is contained in:
Ben Sarmiento
2023-10-23 16:13:04 +02:00
parent 693f8dde8a
commit 6298830ea6
5 changed files with 312 additions and 123 deletions

View File

@@ -22,6 +22,102 @@ type TorrentManager struct {
workerPool chan bool
}
// NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background
// and store them in-memory
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
handler := &TorrentManager{
config: config,
cache: cache,
workerPool: make(chan bool, config.GetNumOfWorkers()),
}
// Initialize torrents for the first time
handler.torrents = handler.getAll()
for _, torrent := range handler.torrents {
go func(id string) {
handler.workerPool <- true
handler.getInfo(id)
<-handler.workerPool
time.Sleep(1 * time.Second) // sleep for 1 second to avoid rate limiting
}(torrent.ID)
}
// Start the periodic refresh
go handler.refreshTorrents()
return handler
}
// GetByDirectory returns all torrents that have a file in the specified directory
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
var torrents []Torrent
for i := range t.torrents {
for _, dir := range t.torrents[i].Directories {
if dir == directory {
torrents = append(torrents, t.torrents[i])
}
}
}
return torrents
}
// RefreshInfo refreshes the info for a torrent
func (t *TorrentManager) RefreshInfo(torrentID string) {
filePath := fmt.Sprintf("data/%s.bin", torrentID)
// Check the last modified time of the .bin file
fileInfo, err := os.Stat(filePath)
if err == nil {
modTime := fileInfo.ModTime()
// If the file was modified less than an hour ago, don't refresh
if time.Since(modTime) < time.Duration(t.config.GetCacheTimeHours())*time.Hour {
return
}
err = os.Remove(filePath)
if err != nil && !os.IsNotExist(err) { // File doesn't exist or other error
log.Printf("Cannot remove file: %v\n", err)
}
} else if !os.IsNotExist(err) { // Error other than file not existing
log.Printf("Error checking file info: %v\n", err)
return
}
info := t.getInfo(torrentID)
log.Println("Refreshed info for", info.Name)
}
// MarkFileAsDeleted marks a file as deleted
func (t *TorrentManager) MarkFileAsDeleted(torrent *Torrent, file *File) {
log.Println("Marking file as deleted", file.Path)
file.Link = ""
t.writeToFile(torrent.ID, torrent)
}
// GetInfo returns the info for a torrent
func (t *TorrentManager) GetInfo(torrentID string) *Torrent {
for i := range t.torrents {
if t.torrents[i].ID == torrentID {
return &t.torrents[i]
}
}
return t.getInfo(torrentID)
}
// getChecksum returns the checksum based on the total count and the first torrent's ID
func (t *TorrentManager) getChecksum() string {
torrents, totalCount, err := realdebrid.GetTorrents(t.config.GetToken(), 1)
if err != nil {
log.Printf("Cannot get torrents: %v\n", err)
return t.checksum
}
if len(torrents) == 0 {
log.Println("Huh, no torrents returned")
return t.checksum
}
return fmt.Sprintf("%d-%s", totalCount, torrents[0].ID)
}
// refreshTorrents periodically refreshes the torrents
func (t *TorrentManager) refreshTorrents() {
log.Println("Starting periodic refresh")
for {
@@ -73,47 +169,7 @@ func (t *TorrentManager) refreshTorrents() {
}
}
// NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background
// and store them in-memory
func NewTorrentManager(config config.ConfigInterface, cache *expirable.LRU[string, string]) *TorrentManager {
handler := &TorrentManager{
config: config,
cache: cache,
workerPool: make(chan bool, config.GetNumOfWorkers()),
}
// Initialize torrents for the first time
handler.torrents = handler.getAll()
for _, torrent := range handler.torrents {
go func(id string) {
handler.workerPool <- true
handler.getInfo(id)
<-handler.workerPool
time.Sleep(1 * time.Second) // sleep for 1 second to avoid rate limiting
}(torrent.ID)
}
// Start the periodic refresh
go handler.refreshTorrents()
return handler
}
func (t *TorrentManager) getChecksum() string {
torrents, totalCount, err := realdebrid.GetTorrents(t.config.GetToken(), 1)
if err != nil {
log.Printf("Cannot get torrents: %v\n", err)
return t.checksum
}
if len(torrents) == 0 {
log.Println("Huh, no torrents returned")
return t.checksum
}
return fmt.Sprintf("%d-%s", totalCount, torrents[0].ID)
}
// getAll returns all torrents
func (t *TorrentManager) getAll() []Torrent {
log.Println("Getting all torrents")
@@ -159,40 +215,7 @@ func (t *TorrentManager) getAll() []Torrent {
return torrentsV2
}
func (t *TorrentManager) GetByDirectory(directory string) []Torrent {
var torrents []Torrent
for i := range t.torrents {
for _, dir := range t.torrents[i].Directories {
if dir == directory {
torrents = append(torrents, t.torrents[i])
}
}
}
return torrents
}
func (t *TorrentManager) RefreshInfo(torrentID string) {
filePath := fmt.Sprintf("data/%s.bin", torrentID)
// Check the last modified time of the .bin file
fileInfo, err := os.Stat(filePath)
if err == nil {
modTime := fileInfo.ModTime()
// If the file was modified less than an hour ago, don't refresh
if time.Since(modTime) < time.Duration(t.config.GetCacheTimeHours())*time.Hour {
return
}
err = os.Remove(filePath)
if err != nil && !os.IsNotExist(err) { // File doesn't exist or other error
log.Printf("Cannot remove file: %v\n", err)
}
} else if !os.IsNotExist(err) { // Error other than file not existing
log.Printf("Error checking file info: %v\n", err)
return
}
info := t.getInfo(torrentID)
log.Println("Refreshed info for", info.Name)
}
// getInfo returns the info for a torrent
func (t *TorrentManager) getInfo(torrentID string) *Torrent {
torrentFromFile := t.readFromFile(torrentID)
if torrentFromFile != nil {
@@ -290,21 +313,7 @@ func (t *TorrentManager) getInfo(torrentID string) *Torrent {
return torrent
}
func (t *TorrentManager) MarkFileAsDeleted(torrent *Torrent, file *File) {
log.Println("Marking file as deleted", file.Path)
file.Link = ""
t.writeToFile(torrent.ID, torrent)
}
func (t *TorrentManager) GetInfo(torrentID string) *Torrent {
for i := range t.torrents {
if t.torrents[i].ID == torrentID {
return &t.torrents[i]
}
}
return t.getInfo(torrentID)
}
// getByID returns a torrent by its ID
func (t *TorrentManager) getByID(torrentID string) *Torrent {
for i := range t.torrents {
if t.torrents[i].ID == torrentID {
@@ -314,6 +323,7 @@ func (t *TorrentManager) getByID(torrentID string) *Torrent {
return nil
}
// writeToFile writes a torrent to a file
func (t *TorrentManager) writeToFile(torrentID string, torrent *Torrent) {
filePath := fmt.Sprintf("data/%s.bin", torrentID)
file, err := os.Create(filePath)
@@ -327,6 +337,7 @@ func (t *TorrentManager) writeToFile(torrentID string, torrent *Torrent) {
dataEncoder.Encode(torrent)
}
// readFromFile reads a torrent from a file
func (t *TorrentManager) readFromFile(torrentID string) *Torrent {
filePath := fmt.Sprintf("data/%s.bin", torrentID)
fileInfo, err := os.Stat(filePath)