Zfs fixes

This commit is contained in:
Ben Sarmiento
2023-11-12 02:05:45 +01:00
parent 2657eff11c
commit 0c2cff2387
14 changed files with 79 additions and 1155 deletions

View File

@@ -18,12 +18,12 @@ import (
)
type TorrentManager struct {
config config.ConfigInterface
TorrentMap *orderedmap.OrderedMap[string, *Torrent] // accessKey -> Torrent
repairMap *orderedmap.OrderedMap[string, time.Time] // accessKey -> time last repaired
requiredVersion string
rd *realdebrid.RealDebrid
checksum string
config config.ConfigInterface
api *realdebrid.RealDebrid
workerPool chan bool
mu *sync.Mutex
log *zap.SugaredLogger
@@ -32,13 +32,13 @@ type TorrentManager struct {
// NewTorrentManager creates a new torrent manager
// it will fetch all torrents and their info in the background
// and store them in-memory and cached in files
func NewTorrentManager(config config.ConfigInterface, rd *realdebrid.RealDebrid) *TorrentManager {
func NewTorrentManager(config config.ConfigInterface, api *realdebrid.RealDebrid) *TorrentManager {
t := &TorrentManager{
config: config,
TorrentMap: orderedmap.NewOrderedMap[string, *Torrent](),
repairMap: orderedmap.NewOrderedMap[string, time.Time](),
requiredVersion: "10.11.2023",
rd: rd,
config: config,
api: api,
workerPool: make(chan bool, config.GetNumOfWorkers()),
mu: &sync.Mutex{},
log: logutil.NewLogger().Named("manager"),
@@ -46,7 +46,7 @@ func NewTorrentManager(config config.ConfigInterface, rd *realdebrid.RealDebrid)
t.mu.Lock()
newTorrents, _, err := t.rd.GetTorrents(0)
newTorrents, _, err := t.api.GetTorrents(0)
if err != nil {
t.log.Fatalf("Cannot get torrents: %v\n", err)
}
@@ -123,7 +123,7 @@ func (t *TorrentManager) mergeToMain(t1, t2 *Torrent) *Torrent {
// proxy
func (t *TorrentManager) UnrestrictUntilOk(link string) *realdebrid.UnrestrictResponse {
t.workerPool <- true
ret := t.rd.UnrestrictUntilOk(link)
ret := t.api.UnrestrictUntilOk(link)
<-t.workerPool
return ret
}
@@ -141,7 +141,7 @@ func (t *TorrentManager) getChecksum() string {
// GetTorrents request
go func() {
torrents, totalCount, err := t.rd.GetTorrents(1)
torrents, totalCount, err := t.api.GetTorrents(1)
if err != nil {
errChan <- err
return
@@ -151,7 +151,7 @@ func (t *TorrentManager) getChecksum() string {
// GetActiveTorrentCount request
go func() {
count, err := t.rd.GetActiveTorrentCount()
count, err := t.api.GetActiveTorrentCount()
if err != nil {
errChan <- err
return
@@ -196,7 +196,7 @@ func (t *TorrentManager) startRefreshJob() {
t.mu.Lock()
newTorrents, _, err := t.rd.GetTorrents(0)
newTorrents, _, err := t.api.GetTorrents(0)
if err != nil {
t.log.Warnf("Cannot get torrents: %v\n", err)
continue
@@ -262,13 +262,13 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
var err error
// file cache
torrentFromFile := t.readFromFile(rdTorrent.ID)
if torrentFromFile != nil && len(torrentFromFile.ID) > 0 && len(torrentFromFile.Links) == len(rdTorrent.Links) {
if torrentFromFile != nil && len(torrentFromFile.ID) > 0 && len(torrentFromFile.Links) == len(rdTorrent.Links) && torrentFromFile.Links[0] == rdTorrent.Links[0] {
// see if api data and file data still match
// then it means data is still usable
info = torrentFromFile
}
if info == nil {
info, err = t.rd.GetTorrentInfo(rdTorrent.ID)
info, err = t.api.GetTorrentInfo(rdTorrent.ID)
if err != nil {
t.log.Warnf("Cannot get info for id=%s: %v\n", rdTorrent.ID, err)
return nil
@@ -386,7 +386,7 @@ func (t *TorrentManager) getDirectories(torrent *realdebrid.TorrentInfo) []strin
default:
t.log.Error("Unknown config version")
}
t.log.Debugf("Torrent %s is in directories %v", t.getName(torrent.Name, torrent.OriginalName), ret)
// t.log.Debugf("Torrent %s is in directories %v", t.getName(torrent.Name, torrent.OriginalName), ret)
return ret
}
@@ -446,7 +446,7 @@ func (t *TorrentManager) organizeChaos(links []string, selectedFiles *orderedmap
go func(lnk string) {
defer wg.Done()
t.workerPool <- true
resp := t.rd.UnrestrictUntilOk(lnk)
resp := t.api.UnrestrictUntilOk(lnk)
<-t.workerPool
resultsChan <- Result{Response: resp}
}(link)
@@ -613,7 +613,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
}
// redownload torrent
resp, err := t.rd.AddMagnetHash(torrent.Instances[0].Hash)
resp, err := t.api.AddMagnetHash(torrent.Instances[0].Hash)
if err != nil {
t.log.Warnf("Cannot redownload torrent: %v", err)
return false
@@ -622,25 +622,25 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
// select files
newTorrentID := resp.ID
err = t.rd.SelectTorrentFiles(newTorrentID, missingFiles)
err = t.api.SelectTorrentFiles(newTorrentID, missingFiles)
if err != nil {
t.log.Warnf("Cannot start redownloading: %v", err)
t.rd.DeleteTorrent(newTorrentID)
t.api.DeleteTorrent(newTorrentID)
return false
}
time.Sleep(10 * time.Second)
// see if the torrent is ready
info, err := t.rd.GetTorrentInfo(newTorrentID)
info, err := t.api.GetTorrentInfo(newTorrentID)
if err != nil {
t.log.Warnf("Cannot get info on redownloaded torrent id=%s : %v", newTorrentID, err)
t.rd.DeleteTorrent(newTorrentID)
t.api.DeleteTorrent(newTorrentID)
return false
}
if info.Status == "magnet_error" || info.Status == "error" || info.Status == "virus" || info.Status == "dead" {
t.log.Warnf("The redownloaded torrent id=%s is in error state: %s", newTorrentID, info.Status)
t.rd.DeleteTorrent(newTorrentID)
t.api.DeleteTorrent(newTorrentID)
return false
}
@@ -652,7 +652,7 @@ func (t *TorrentManager) reinsertTorrent(torrent *Torrent, missingFiles string)
missingCount := len(strings.Split(missingFiles, ","))
if len(info.Links) != missingCount {
t.log.Infof("It did not fix the issue for id=%s, only got %d files but we need %d, undoing", info.ID, len(info.Links), missingCount)
t.rd.DeleteTorrent(newTorrentID)
t.api.DeleteTorrent(newTorrentID)
return false
}
@@ -667,7 +667,7 @@ func (t *TorrentManager) canCapacityHandle() bool {
const maxDelay = 60 * time.Second
retryCount := 0
for {
count, err := t.rd.GetActiveTorrentCount()
count, err := t.api.GetActiveTorrentCount()
if err != nil {
t.log.Warnf("Cannot get active downloads count: %v", err)
if retryCount >= maxRetries {