Remove immediate bin

This commit is contained in:
Ben Adrian Sarmiento
2024-06-18 23:43:31 +02:00
parent 34a7d6a432
commit acc9b69b5a
8 changed files with 58 additions and 87 deletions

View File

@@ -29,6 +29,7 @@ func (t *TorrentManager) StartRepairJob() {
t.RepairQueue = mapset.NewSet[*Torrent]()
t.RepairAllTrigger = make(chan struct{})
// periodic repair worker
t.workerPool.Submit(func() {
t.repairLog.Debug("Starting periodic repair job")
repairTicker := time.NewTicker(time.Duration(t.Config.GetRepairEveryMins()) * time.Minute)
@@ -46,7 +47,7 @@ func (t *TorrentManager) StartRepairJob() {
}
})
// there is 1 repair worker, with max 1 blocking task
// repair worker
t.workerPool.Submit(func() {
for {
select {
@@ -90,7 +91,6 @@ func (t *TorrentManager) invokeRepair(torrent *Torrent) {
t.repairRunningMu.Unlock()
// Execute the repair job
time.Sleep(10 * time.Second)
t.executeRepairJob(torrent)
// After repair is done
@@ -120,10 +120,11 @@ func (t *TorrentManager) executeRepairJob(torrent *Torrent) {
var wg sync.WaitGroup
haystack.IterCb(func(_ string, torrent *Torrent) {
wg.Add(1)
// temp worker for finding broken torrents
t.workerPool.Submit(func() {
defer wg.Done()
canExtract := t.Config.GetRarAction() == "extract" && strings.Contains(torrent.UnrepairableReason, "rar")
if torrent.UnrepairableReason != "" || !canExtract {
if !canExtract || torrent.UnrepairableReason != "" {
return
}
// check 1: for broken files
@@ -234,12 +235,12 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
}
// if it's still broken, let's delete the newly downloaded torrent
t.setToBinImmediately(info.ID)
t.DeleteByID(info.ID)
err = fmt.Errorf("links are still broken")
} else if info != nil && info.Progress != 100 {
// it's faster to download just the broken files, so let's delete the newly downloaded torrent
t.setToBinImmediately(info.ID)
t.DeleteByID(info.ID)
err = fmt.Errorf("no longer cached")
}
@@ -278,7 +279,7 @@ func (t *TorrentManager) repair(torrent *Torrent, wg *sync.WaitGroup) {
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)
t.DeleteByID(newId)
}
return
}
@@ -414,10 +415,10 @@ func (t *TorrentManager) assignLinks(torrent *Torrent) bool {
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")
torrent.State.Event(context.Background(), "mark_as_repaired")
}
torrent.UnassignedLinks = mapset.NewSet[string]()
// torrent.State.Event(context.Background(), "mark_as_repaired")
t.writeTorrentToFile(torrent)
return false // end repair
@@ -478,20 +479,20 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
for {
retries++
if retries > 10 {
t.setToBinImmediately(newTorrentID)
t.DeleteByID(newTorrentID)
return nil, fmt.Errorf("cannot start redownloading: too many retries")
}
err = t.api.SelectTorrentFiles(newTorrentID, finalSelection)
if err != nil {
t.setToBinImmediately(newTorrentID)
t.DeleteByID(newTorrentID)
return nil, fmt.Errorf("cannot start redownloading: %v", err)
}
time.Sleep(2 * time.Second)
info, err = t.api.GetTorrentInfo(newTorrentID)
if err != nil {
t.setToBinImmediately(newTorrentID)
t.DeleteByID(newTorrentID)
return nil, fmt.Errorf("cannot get info on redownloaded : %v", err)
}
@@ -504,13 +505,13 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection []string)
// documented status: magnet_error, magnet_conversion, waiting_files_selection, queued, downloading, downloaded, error, virus, compressing, uploading, dead
if info.Status != "downloading" && info.Status != "downloaded" && info.Status != "uploading" && info.Status != "queued" && info.Status != "compressing" {
t.setToBinImmediately(newTorrentID)
t.DeleteByID(newTorrentID)
return nil, fmt.Errorf("non-OK state: %s", info.Status)
}
// check if incorrect number of links
if info.Progress == 100 && len(info.Links) != len(selection) {
t.setToBinImmediately(newTorrentID)
t.DeleteByID(newTorrentID)
return nil, fmt.Errorf("only got %d links but we need %d", len(info.Links), len(selection))
} else if info.Progress != 100 {
t.repairLog.Infof("Downloading torrent %s (id=%s, progress=%d)", t.GetKey(torrent), info.ID, info.Progress)