From 95017884d7c8912bb6a1efbafc89dfa3c93b7f69 Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 2 Oct 2015 14:34:38 +0200 Subject: [PATCH 1/3] rTorrent: Fixed race condition --- .../Download/Clients/rTorrent/RTorrent.cs | 65 ++++++++++++------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs index 523d2033e..2f04229e7 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs @@ -40,23 +40,9 @@ namespace NzbDrone.Core.Download.Clients.RTorrent { _proxy.AddTorrentFromUrl(magnetLink, Settings); - // Wait until url has been resolved before returning - var TRIES = 5; - var RETRY_DELAY = 500; //ms - var ready = false; - - for (var i = 0; i < TRIES; i++) - { - ready = _proxy.HasHashTorrent(hash, Settings); - if (ready) - { - break; - } - - Thread.Sleep(RETRY_DELAY); - } - - if (ready) + var tries = 10; + var retryDelay = 500; + if (WaitForTorrent(hash, tries, retryDelay)) { _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); @@ -69,8 +55,8 @@ namespace NzbDrone.Core.Download.Clients.RTorrent } else { - _logger.Debug("Magnet {0} could not be resolved in {1} tries at {2} ms intervals.", magnetLink, TRIES, RETRY_DELAY); - // Remove from client, since it is discarded + _logger.Debug("rTorrent could not resolve magnet {0}. Removing", magnetLink); + RemoveItem(hash, true); return null; @@ -80,14 +66,28 @@ namespace NzbDrone.Core.Download.Clients.RTorrent protected override string AddFromTorrentFile(RemoteEpisode remoteEpisode, string hash, string filename, byte[] fileContent) { _proxy.AddTorrentFromFile(filename, fileContent, Settings); - _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); - SetPriority(remoteEpisode, hash); - SetDownloadDirectory(hash); + var tries = 2; + var retryDelay = 100; + if (WaitForTorrent(hash, tries, retryDelay)) + { + _proxy.SetTorrentLabel(hash, Settings.TvCategory, Settings); - _proxy.StartTorrent(hash, Settings); + SetPriority(remoteEpisode, hash); + SetDownloadDirectory(hash); - return hash; + _proxy.StartTorrent(hash, Settings); + + return hash; + } + else + { + _logger.Debug("rTorrent could not add file"); + + RemoveItem(hash, true); + + return null; + } } public override string Name @@ -251,5 +251,22 @@ namespace NzbDrone.Core.Download.Clients.RTorrent _proxy.SetTorrentDownloadDirectory(hash, Settings.TvDirectory, Settings); } } + + private bool WaitForTorrent(string hash, int tries, int retryDelay) + { + for (var i = 0; i < tries; i++) + { + if (_proxy.HasHashTorrent(hash, Settings)) + { + return true; + } + + Thread.Sleep(retryDelay); + } + + _logger.Debug("Could not find hash {0} in {1} tries at {2} ms intervals.", hash, tries, retryDelay); + + return false; + } } } From 7426efd423702a38daf42de6695d061bae2a273c Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 2 Oct 2015 14:35:09 +0200 Subject: [PATCH 2/3] rTorrent: Fixed label bug --- .../Download/Clients/rTorrent/RTorrentProxy.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs index 1ac0032d1..d16ea589a 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentProxy.cs @@ -97,11 +97,13 @@ namespace NzbDrone.Core.Download.Clients.RTorrent var items = new List(); foreach (object[] torrent in ret) { + var labelDecoded = System.Web.HttpUtility.UrlDecode((string) torrent[3]); + var item = new RTorrentTorrent(); item.Name = (string) torrent[0]; item.Hash = (string) torrent[1]; item.Path = (string) torrent[2]; - item.Category = (string) torrent[3]; + item.Category = labelDecoded; item.TotalSize = (long) torrent[4]; item.RemainingSize = (long) torrent[5]; item.DownRate = (long) torrent[6]; @@ -172,10 +174,12 @@ namespace NzbDrone.Core.Download.Clients.RTorrent { _logger.Debug("Executing remote method: d.set_custom1"); + var labelEncoded = System.Web.HttpUtility.UrlEncode(label); + var client = BuildClient(settings); - var setLabel = client.SetLabel(hash, label); - if (setLabel != label) + var setLabel = client.SetLabel(hash, labelEncoded); + if (setLabel != labelEncoded) { throw new DownloadClientException("Could set label on torrent: {0}.", hash); } From 6ed7a8b4717cd61fae58b7390e81064c9ff03a7a Mon Sep 17 00:00:00 2001 From: Lars Date: Fri, 2 Oct 2015 14:35:31 +0200 Subject: [PATCH 3/3] rTorrent: Url Path displayed by default, misc --- src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs | 2 +- src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs index 2f04229e7..b43092da0 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrent.cs @@ -145,7 +145,7 @@ namespace NzbDrone.Core.Download.Clients.RTorrent else if (torrent.IsActive) item.Status = DownloadItemStatus.Downloading; else if (!torrent.IsActive) item.Status = DownloadItemStatus.Paused; - // Since we do not know the user's intent, do not let Sonarr to remove the torrent + // No stop ratio data is present, so do not delete item.IsReadOnly = true; items.Add(item); diff --git a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs index 5983c3e40..81715246c 100644 --- a/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs +++ b/src/NzbDrone.Core/Download/Clients/rTorrent/RTorrentSettings.cs @@ -37,7 +37,7 @@ namespace NzbDrone.Core.Download.Clients.RTorrent [FieldDefinition(1, Label = "Port", Type = FieldType.Textbox)] public int Port { get; set; } - [FieldDefinition(2, Label = "Url Base", Type = FieldType.Textbox, Advanced = true, HelpText = "Adds a suffix the rpc url, see http://[host]:[port]/[urlBase], by default this should be RPC2")] + [FieldDefinition(2, Label = "Url Path", Type = FieldType.Textbox, HelpText = "Path to the XMLRPC endpoint, see http(s)://[host]:[port]/[urlPath]. When using ruTorrent this usually is RPC2 or (path to ruTorrent)/plugins/rpc/rpc.php")] public string UrlBase { get; set; } [FieldDefinition(3, Label = "Use SSL", Type = FieldType.Checkbox)]