Fix issue with bins

This commit is contained in:
Ben Sarmiento
2024-05-27 07:01:59 +02:00
parent ac89f53896
commit 9e44721fa2
3 changed files with 29 additions and 56 deletions

View File

@@ -41,6 +41,32 @@ func (t *TorrentManager) initializeBins() {
t.log.Debugf("Bin immediately: %v", t.ImmediateBin.ToSlice())
t.log.Debugf("Bin once done: %v", t.OnceDoneBin.ToSlice())
}
func (t *TorrentManager) persistBins() {
data := map[string]interface{}{
"trash_bin": t.ImmediateBin.ToSlice(), // Assuming trashBin is a mapset.Set[string]
"repair_bin": t.OnceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string]
}
jsonData, err := json.Marshal(data)
if err != nil {
t.log.Errorf("Failed to marshal bin data: %v", err)
return
}
file, err := os.Create(BINS_FILE)
if err != nil {
t.log.Errorf("Failed to create bins.json file: %v", err)
return
}
defer file.Close()
_, err = file.Write(jsonData)
if err != nil {
t.log.Errorf("Failed to write to bins.json file: %v", err)
}
}
func (t *TorrentManager) setToBinImmediately(torrentId string) {
t.log.Debugf("Set to delete immediately: %s", torrentId)
t.ImmediateBin.Add(torrentId)
@@ -99,40 +125,3 @@ func (t *TorrentManager) binOnceDone(torrentId string) bool {
return found
}
func (t *TorrentManager) persistBins() {
data := map[string]interface{}{
"trash_bin": t.ImmediateBin.ToSlice(), // Assuming trashBin is a mapset.Set[string]
"repair_bin": t.OnceDoneBin.ToSlice(), // Assuming repairBin is a mapset.Set[string]
}
jsonData, err := json.Marshal(data)
if err != nil {
t.log.Errorf("Failed to marshal bin data: %v", err)
return
}
file, err := os.Create(BINS_FILE)
if err != nil {
t.log.Errorf("Failed to create bins.json file: %v", err)
return
}
defer file.Close()
_, err = file.Write(jsonData)
if err != nil {
t.log.Errorf("Failed to write to bins.json file: %v", err)
}
}
func (t *TorrentManager) cleanupBins(freshIDs mapset.Set[string]) {
t.ImmediateBin.Difference(freshIDs).Each(func(id string) bool {
t.ImmediateBin.Remove(id)
return false
})
t.OnceDoneBin.Difference(freshIDs).Each(func(id string) bool {
t.OnceDoneBin.Remove(id)
return false
})
t.persistBins()
}

View File

@@ -145,8 +145,6 @@ func (t *TorrentManager) refreshTorrents() []string {
return false
})
t.cleanupBins(freshIDs)
return updatedPaths.ToSlice()
}

View File

@@ -196,7 +196,7 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// delete the torrents it replaced
torrent.DownloadedIDs.Each(func(torrentID string) bool {
if torrentID != info.ID {
t.setToBinImmediately(torrentID)
t.setToBinOnceDone(fmt.Sprintf("%s-%s", info.ID, torrentID))
}
return false
})
@@ -240,9 +240,9 @@ func (t *TorrentManager) repair(torrent *Torrent) {
batchNum := 1
brokenFileIDs := getFileIDs(brokenFiles)
var group []string
for _, fileIDStr := range brokenFileIDs {
for i, fileIDStr := range brokenFileIDs {
group = append(group, fileIDStr)
if len(group) >= 100 {
if len(group) >= 100 || i == len(brokenFileIDs)-1 {
t.repairLog.Debugf("Downloading batch %d/%d of broken files of torrent %s", batchNum, totalBatches, t.GetKey(torrent))
batchNum++
redownloadedInfo, err := t.redownloadTorrent(torrent, group)
@@ -259,20 +259,6 @@ func (t *TorrentManager) repair(torrent *Torrent) {
}
}
if len(group) > 0 {
t.repairLog.Debugf("Downloading batch %d/%d of broken files of torrent %s", batchNum, totalBatches, t.GetKey(torrent))
redownloadedInfo, err := t.redownloadTorrent(torrent, group)
if err != nil {
t.repairLog.Warnf("Cannot repair torrent %s by downloading broken files (error=%v) giving up", t.GetKey(torrent), err)
// delete the newly downloaded torrents because the operation failed
for _, newId := range newlyDownloadedIds {
t.setToBinImmediately(newId)
}
return
}
newlyDownloadedIds = append(newlyDownloadedIds, redownloadedInfo.ID)
}
// once done, we can delete the newly downloaded torrents because we only need the links
for _, newId := range newlyDownloadedIds {
t.setToBinOnceDone(newId)