Upkeep for data directory

This commit is contained in:
Ben Sarmiento
2024-05-21 06:50:46 +02:00
parent 57df90620e
commit 6c24d74f61
4 changed files with 67 additions and 9 deletions

View File

@@ -39,8 +39,28 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
var updatedPaths []string
noInfoCount := 0
allTorrents, _ := t.DirectoryMap.Get(INT_ALL)
freshAccessKeys := mapset.NewSet[string]()
allHashes := mapset.NewSet[string]()
t.getTorrentFiles().Each(func(path string) bool {
path = filepath.Base(path)
hash := strings.TrimSuffix(path, ".torrent_zurg")
allHashes.Add(hash)
return false
})
freshHashes := mapset.NewSet[string]()
allIDs := mapset.NewSet[string]()
t.getInfoFiles().Each(func(path string) bool {
path = filepath.Base(path)
torrentID := strings.TrimSuffix(path, ".info_zurg")
allIDs.Add(torrentID)
return false
})
freshIDs := mapset.NewSet[string]()
for torrent := range torChan {
if torrent == nil {
noInfoCount++
@@ -54,6 +74,8 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
}
accessKey := t.GetKey(torrent)
freshAccessKeys.Add(accessKey)
freshHashes.Add(torrent.Hash)
freshIDs.Add(tInfo.ID)
// update allTorrents
isNewID := false
@@ -77,8 +99,13 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
updatedPaths = append(updatedPaths, fmt.Sprintf("%s/%s", directory, accessKey))
})
// write torrent to file
if !allHashes.Contains(mainTorrent.Hash) {
t.writeTorrentToFile(mainTorrent)
}
}
}
// removed torrents
allAccessKeys := mapset.NewSet[string](allTorrents.Keys()...)
allAccessKeys.Difference(freshAccessKeys).Each(func(accessKey string) bool {
@@ -88,6 +115,18 @@ func (t *TorrentManager) refreshTorrents(isInitialRun bool) []string {
t.log.Infof("Compiled into %d torrents, %d were missing info", allTorrents.Count(), noInfoCount)
// data directory cleanup
allHashes.Difference(freshHashes).Each(func(hash string) bool {
t.log.Infof("Deleting stale torrent file %s", hash)
t.deleteTorrentFile(hash)
return false
})
allIDs.Difference(freshIDs).Each(func(id string) bool {
t.log.Infof("Deleting stale info file %s", id)
t.deleteInfoFile(id)
return false
})
if t.Config.EnableRepair() {
if isInitialRun {
t.removeExpiredFixers(instances)
@@ -136,6 +175,7 @@ func (t *TorrentManager) getMoreInfo(rdTorrent realdebrid.Torrent) *Torrent {
diskTor := t.readTorrentFromFile(rdTorrent.Hash)
if diskTor != nil {
if diskInfo, ok := diskTor.Components[rdTorrent.ID]; ok && diskInfo.Progress == 100 {
diskTor.Components = map[string]*realdebrid.TorrentInfo{rdTorrent.ID: diskInfo}
return diskTor
}
}