Small repairs on logic

This commit is contained in:
Ben Adrian Sarmiento
2024-06-18 19:32:56 +02:00
parent d53ba8c973
commit 34a7d6a432
8 changed files with 78 additions and 74 deletions

View File

@@ -164,7 +164,7 @@ func (t *TorrentManager) executeRepairJob(torrent *Torrent) {
func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
defer wg.Done()
if err := torrent.State.Event(context.Background(), "repair_torrent"); err != nil && t.inProgressHashes.Contains(torrent.Hash) {
if err := torrent.State.Event(context.Background(), "repair_torrent"); err != nil {
// t.repairLog.Errorf("Failed to mark torrent %s as under repair: %v", t.GetKey(torrent), err)
return
}
@@ -213,18 +213,18 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
return
}
oldDownloadedIDs := torrent.DownloadedIDs.Clone()
// first step: redownload the whole torrent
t.repairLog.Debugf("Torrent %s has %d broken files (out of %d), repairing by redownloading whole torrent", t.GetKey(torrent), len(brokenFiles), torrent.SelectedFiles.Count())
t.repairLog.Debugf("Torrent %s has %d broken files (out of %d); repairing by redownloading whole torrent", t.GetKey(torrent), len(brokenFiles), torrent.SelectedFiles.Count())
info, err := t.redownloadTorrent(torrent, []string{}) // reinsert the whole torrent, passing empty selection
if info != nil && info.Progress == 100 {
if !t.isStillBroken(info, brokenFiles) {
// successful repair
torrent.State.Event(context.Background(), "mark_as_repaired")
t.repairLog.Infof("Successfully repaired torrent %s by redownloading whole torrent", t.GetKey(torrent))
// delete the torrents it replaced
torrent.DownloadedIDs.Clone().Each(func(torrentID string) bool {
oldDownloadedIDs.Each(func(torrentID string) bool {
if torrentID != info.ID {
t.setXToBinOnceYDone(torrentID, info.ID)
}
@@ -248,7 +248,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
}
if torrent.UnrepairableReason != "" {
t.repairLog.Debugf("Torrent %s has been marked as unfixable during redownloading whole torrent (%s), ending repair process early", t.GetKey(torrent), torrent.UnrepairableReason)
t.repairLog.Debugf("Torrent %s has been marked as unfixable after redownloading torrent %s; ending repair process early", t.GetKey(torrent), torrent.UnrepairableReason)
return
}
@@ -365,63 +365,62 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
t.writeTorrentToFile(torrent)
}
if assignedCount == 0 && rarCount == 1 {
action := t.Config.GetRarAction()
if assignedCount != 0 || rarCount != 1 {
return true // continue repair
}
if action == "delete" {
t.repairLog.Warnf("Torrent %s is rar'ed and we cannot repair it, deleting it as configured", t.GetKey(torrent))
t.Delete(t.GetKey(torrent), true)
return false
}
action := t.Config.GetRarAction()
if action == "delete" {
t.repairLog.Warnf("Torrent %s is rar'ed and we cannot repair it, deleting it as configured", t.GetKey(torrent))
t.Delete(t.GetKey(torrent), true)
return false // end repair
}
newUnassignedLinks.IterCb(func(_ string, unassigned *realdebrid.Download) {
newFile := &File{
File: realdebrid.File{
ID: 0,
Path: unassigned.Filename,
Bytes: unassigned.Filesize,
Selected: 0,
},
Ended: torrent.Added,
Link: unassigned.Link,
State: NewFileState("ok_file"),
}
torrent.SelectedFiles.Set(unassigned.Filename, newFile)
newUnassignedLinks.IterCb(func(_ string, unassigned *realdebrid.Download) {
torrent.SelectedFiles.Set(unassigned.Filename, &File{
File: realdebrid.File{
ID: 0,
Path: unassigned.Filename,
Bytes: unassigned.Filesize,
Selected: 0,
},
Ended: torrent.Added,
Link: unassigned.Link,
State: NewFileState("ok_file"),
})
})
if action == "extract" {
videoFiles := []string{}
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if utils.IsPlayable(file.Path) {
videoFiles = append(videoFiles, fmt.Sprintf("%d", file.ID))
} else if file.ID != 0 {
t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent))
info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)})
if info != nil {
t.setToBinOnceDone(info.ID)
}
}
})
if len(videoFiles) > 0 {
info, _ := t.redownloadTorrent(torrent, videoFiles)
if action == "extract" {
videoFiles := []string{}
torrent.SelectedFiles.IterCb(func(_ string, file *File) {
if utils.IsPlayable(file.Path) {
videoFiles = append(videoFiles, fmt.Sprintf("%d", file.ID))
} else if file.ID != 0 {
t.repairLog.Debugf("Extracting file %s from rar'ed torrent %s", file.Path, t.GetKey(torrent))
info, _ := t.redownloadTorrent(torrent, []string{fmt.Sprintf("%d", file.ID)})
if info != nil {
t.setToBinOnceDone(info.ID)
}
}
} else {
t.repairLog.Warnf("Torrent %s is rar'ed and we cannot repair it", t.GetKey(torrent))
t.markAsUnfixable(torrent, "rar'ed by RD")
t.markAsUnplayable(torrent, "rar'ed by RD")
})
if len(videoFiles) > 0 {
t.repairLog.Debugf("Extracting %d video files from rar'ed torrent %s", len(videoFiles), t.GetKey(torrent))
info, _ := t.redownloadTorrent(torrent, videoFiles)
if info != nil {
t.setToBinOnceDone(info.ID)
}
}
torrent.UnassignedLinks = mapset.NewSet[string]()
torrent.State.Event(context.Background(), "mark_as_repaired")
t.writeTorrentToFile(torrent)
return false // end repair
} else {
t.repairLog.Warnf("Torrent %s is rar'ed and we cannot repair it", t.GetKey(torrent))
t.markAsUnfixable(torrent, "rar'ed by RD")
t.markAsUnplayable(torrent, "rar'ed by RD")
}
return true // continue repair
torrent.UnassignedLinks = mapset.NewSet[string]()
// torrent.State.Event(context.Background(), "mark_as_repaired")
t.writeTorrentToFile(torrent)
return false // end repair
}
func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string) (*realdebrid.TorrentInfo, error) {
@@ -630,7 +629,7 @@ func (t *TorrentManager) isStillBroken(info *realdebrid.TorrentInfo, brokenFiles
brokenFiles = append(brokenFiles, selectedFiles[len(selectedFiles)-1])
}
// check if the broken files can now be unrestricted
// check if the broken files can now be unrestricted and downloaded
for _, oldFile := range brokenFiles {
for idx, newFile := range selectedFiles {
if oldFile.ID == newFile.ID && t.UnrestrictFileUntilOk(selectedFiles[idx], true) == nil {