From 3590fedeaf5b0372930f56d8f02aedc77b3c5c94 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Fri, 10 Mar 2017 20:34:36 +0100 Subject: [PATCH] Fixed: Timing issue in rtorrent handling of magnet links. ref #1745 --- .../Download/Clients/rTorrent/RTorrent.cs | 74 ++++++++++--------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs index d2f30fd9a..c9ab52c19 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs @@ -46,52 +46,60 @@ namespace NzbDrone.Core.Download.Clients.RTorrent SetPriority(remoteEpisode, hash); SetDownloadDirectory(hash); - // Once the magnet meta download finishes, rTorrent replaces it with the actual torrent download with default settings. - // Schedule an event to apply the appropriate settings when that happens. - var priority = (RTorrentPriority)(remoteEpisode.IsRecentEpisode() ? Settings.RecentTvPriority : Settings.OlderTvPriority); - _proxy.SetDeferredMagnetProperties(hash, Settings.TvCategory, Settings.TvDirectory, priority, Settings); + _proxy.StartTorrent(hash, Settings); + + var tries = 10; + var retryDelay = 500; + + // Wait a bit for the magnet to be resolved. + if (!WaitForTorrent(hash, tries - 2, retryDelay)) + { + // Once the magnet meta download finishes, rTorrent replaces it with the actual torrent download with default settings. + // Schedule an event to apply the appropriate settings when that happens. + var priority = (RTorrentPriority)(remoteEpisode.IsRecentEpisode() ? Settings.RecentTvPriority : Settings.OlderTvPriority); + _proxy.SetDeferredMagnetProperties(hash, Settings.TvCategory, Settings.TvDirectory, priority, Settings); + + // Wait a bit longer for the magnet to be resolved. + if (!WaitForTorrent(hash, 2, retryDelay)) + { + // The SetDeferredMagnetProperties will try set the label, priority & directory when rtorrent finishes the magnet, unless rtorrent restarts before that happens. + + _logger.Warn("rTorrent could not resolve magnet within {0} seconds, download may remain stuck: {1}.", tries * retryDelay / 1000, magnetLink); + + return hash; + } + } + + // Make sure the label is set and the torrent started, because we can't rely on SetDeferredMagnetProperties for magnets that get resolved quickly. + _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); + SetPriority(remoteEpisode, hash); + SetDownloadDirectory(hash); _proxy.StartTorrent(hash, Settings); - // Wait for the magnet to be resolved. - var tries = 10; - var retryDelay = 500; - if (WaitForTorrent(hash, tries, retryDelay)) - { - return hash; - } - else - { - _logger.Warn("rTorrent could not resolve magnet within {0} seconds, download may remain stuck: {1}.", tries * retryDelay / 1000, magnetLink); - - return hash; - } + return hash; } protected override string AddFromTorrentFile(RemoteEpisode remoteEpisode, string hash, string filename, byte[] fileContent) { _proxy.AddTorrentFromFile(filename, fileContent, Settings); - var tries = 5; - var retryDelay = 200; - if (WaitForTorrent(hash, tries, retryDelay)) + var tries = 10; + var retryDelay = 500; + if (!WaitForTorrent(hash, tries, retryDelay)) { - _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); + _logger.Debug("rTorrent didn't add the torrent within {0} seconds: {1}.", tries * retryDelay / 1000, filename); - SetPriority(remoteEpisode, hash); - SetDownloadDirectory(hash); - - _proxy.StartTorrent(hash, Settings); - - return hash; - } - else - { - _logger.Debug("rTorrent could not add file"); - - RemoveItem(hash, true); throw new ReleaseDownloadException(remoteEpisode.Release, "Downloading torrent failed"); } + + _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); + SetPriority(remoteEpisode, hash); + SetDownloadDirectory(hash); + + _proxy.StartTorrent(hash, Settings); + + return hash; } public override string Name => "rTorrent";