diff --git a/internal/torrent/delete.go b/internal/torrent/delete.go index 1cd0bfc..a23a907 100644 --- a/internal/torrent/delete.go +++ b/internal/torrent/delete.go @@ -35,7 +35,7 @@ func (t *TorrentManager) Delete(accessKey string, deleteInRD bool) { if deleteInRD { infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) if torrent, ok := allTorrents.Get(accessKey); ok { - torrent.DownloadedIDs.Each(func(id string) bool { + torrent.DownloadedIDs.Union(torrent.InProgressIDs).Each(func(id string) bool { t.log.Debugf("Deleting torrent %s %s in RD", id, accessKey) t.Api.DeleteTorrent(id) infoCache.Remove(id) diff --git a/internal/torrent/repair.go b/internal/torrent/repair.go index c45c79f..dfb0b4c 100644 --- a/internal/torrent/repair.go +++ b/internal/torrent/repair.go @@ -117,6 +117,7 @@ func (t *TorrentManager) Repair(torrent *Torrent) { return } t.repairs.Add(t.GetKey(torrent)) + // save the broken files to the file cache infoCache, _ := t.DirectoryMap.Get(INT_INFO_CACHE) torrent.DownloadedIDs.Each(func(id string) bool { @@ -132,13 +133,7 @@ func (t *TorrentManager) Repair(torrent *Torrent) { t.writeTorrentToFile(id, info) return false }) - _ = t.workerPool.Submit(func() { - t.repair(torrent) - t.repairs.Remove(t.GetKey(torrent)) - }) -} -func (t *TorrentManager) repair(torrent *Torrent) { t.log.Infof("Attempting repair for torrent %s", t.GetKey(torrent)) if torrent.AnyInProgress() { t.log.Infof("Torrent %s is in progress, skipping repair until download is done", t.GetKey(torrent)) @@ -151,6 +146,15 @@ func (t *TorrentManager) repair(torrent *Torrent) { return } + _ = t.workerPool.Submit(func() { + t.repair(torrent) + t.repairs.Remove(t.GetKey(torrent)) + }) +} + +func (t *TorrentManager) repair(torrent *Torrent) { + t.log.Infof("Started repair process for torrent %s", t.GetKey(torrent)) + // handle torrents with incomplete links for selected files assignedCount := 0 @@ -287,7 +291,6 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) } return nil, fmt.Errorf("cannot redownload torrent: %v", err) } - time.Sleep(1 * time.Second) // select files newTorrentID := resp.ID @@ -296,7 +299,6 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) t.Api.DeleteTorrent(newTorrentID) return nil, fmt.Errorf("cannot start redownloading: %v", err) } - time.Sleep(1 * time.Second) // see if the torrent is ready info, err := t.Api.GetTorrentInfo(newTorrentID) @@ -323,10 +325,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) if info.Progress != 100 { t.log.Infof("Torrent %s (id=%s) is not cached anymore so we have to wait until completion (this should fix the issue already)", t.GetKey(torrent), info.ID) if len(oldTorrentIDs) > 0 { - // only triggered when brokenFiles == "" - for _, id := range oldTorrentIDs { - t.Api.DeleteTorrent(id) - } + t.Delete(t.GetKey(torrent), true) } else { t.fixers.Set(newTorrentID, torrent) } @@ -341,9 +340,7 @@ func (t *TorrentManager) redownloadTorrent(torrent *Torrent, brokenFiles string) if len(oldTorrentIDs) > 0 { // only triggered when brokenFiles == "" - for _, id := range oldTorrentIDs { - t.Api.DeleteTorrent(id) - } + t.Delete(t.GetKey(torrent), true) } else { t.fixers.Set(newTorrentID, torrent) }