Reunify workers, add commands

This commit is contained in:
Ben Sarmiento
2024-01-30 16:27:12 +01:00
parent 4e389fa79c
commit 7794e641ab
7 changed files with 29 additions and 54 deletions

View File

@@ -25,7 +25,7 @@ func (t *TorrentManager) StartRepairJob() {
t.repairTrigger = make(chan *Torrent)
t.repairSet = mapset.NewSet[*Torrent]()
// there is 1 repair worker, with max 1 blocking task
_ = t.repairPool.Submit(func() {
_ = t.workerPool.Submit(func() {
t.log.Info("Starting periodic repair job")
repairTicker := time.NewTicker(time.Duration(t.Config.GetRepairEveryMins()) * time.Minute)
defer repairTicker.Stop()
@@ -211,13 +211,13 @@ func (t *TorrentManager) repair(torrent *Torrent) {
// second step: download the broken files
if len(brokenFiles) > 0 {
t.log.Infof("Repairing by downloading only the %d broken out of %d files of torrent %s", len(brokenFiles), torrent.SelectedFiles.Count(), t.GetKey(torrent))
info, err := t.redownloadTorrent(torrent, brokenFileIDs)
redownloadedTorrent, err := t.redownloadTorrent(torrent, brokenFileIDs)
if err != nil {
t.log.Warnf("Cannot repair torrent %s by downloading broken files (error=%s) giving up", t.GetKey(torrent), err.Error())
return
}
if info != nil {
t.fixerAddCommand(info.ID, "repair")
if redownloadedTorrent != nil {
t.fixerAddCommand(redownloadedTorrent.ID, "repair")
return
}
}
@@ -335,14 +335,15 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
return nil, fmt.Errorf("cannot redownload torrent: %v", err)
}
newTorrentID := resp.ID
// sleep for 1 second to let RD process the magnet
time.Sleep(1 * time.Second)
// select files
newTorrentID := resp.ID
err = t.Api.SelectTorrentFiles(newTorrentID, selection)
if err != nil {
t.fixerAddCommand(newTorrentID, "delete")
t.fixerAddCommand(newTorrentID, "delete_failed")
return nil, fmt.Errorf("cannot start redownloading: %v", err)
}
@@ -352,7 +353,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
// see if the torrent is ready
info, err := t.Api.GetTorrentInfo(newTorrentID)
if err != nil {
t.fixerAddCommand(newTorrentID, "delete")
t.fixerAddCommand(newTorrentID, "delete_failed")
return nil, fmt.Errorf("cannot get info on redownloaded torrent %s (id=%s) : %v", t.GetKey(torrent), newTorrentID, err)
}
@@ -367,14 +368,14 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
}
}
if !isOkStatus {
t.fixerAddCommand(newTorrentID, "delete")
t.fixerAddCommand(newTorrentID, "delete_failed")
return nil, fmt.Errorf("the redownloaded torrent %s (id=%s) is in error state: %s", t.GetKey(torrent), newTorrentID, info.Status)
}
// check if incorrect number of links
selectionCount := len(strings.Split(selection, ","))
if info.Progress == 100 && len(info.Links) != selectionCount {
t.fixerAddCommand(newTorrentID, "delete")
t.fixerAddCommand(newTorrentID, "delete_failed")
return nil, fmt.Errorf("it did not fix the issue for %s (id=%s), only got %d files but we need %d, undoing", t.GetKey(torrent), info.ID, len(info.Links), selectionCount)
}
@@ -382,13 +383,8 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, selection string) (
if len(oldTorrentIDs) > 0 {
// replace the old torrent (empty selection)
infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE)
for _, id := range oldTorrentIDs {
t.log.Debugf("Deleting torrent %s (id=%s) to replace with repaired torrent", t.GetKey(torrent), id)
torrent.DownloadedIDs.Remove(id)
t.Api.DeleteTorrent(id)
infoCache.Remove(id)
t.deleteTorrentFile(id)
t.fixerAddCommand(id, "delete_replaced")
}
}
return info, nil