From 9dcd58a6317de32475c37c08b88771325891a3b4 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Thu, 6 Mar 2014 22:28:06 -0800 Subject: [PATCH] Prevent queue/history from blowing up --- .../Download/Clients/Nzbget/Nzbget.cs | 22 +++++++++++--- .../Download/Clients/Sabnzbd/Sabnzbd.cs | 29 ++++++++++++++++--- .../Download/Clients/Sabnzbd/SabnzbdProxy.cs | 13 +++++---- .../MediaFiles/EpisodeFileMovingService.cs | 10 ++++++- 4 files changed, 60 insertions(+), 14 deletions(-) diff --git a/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs b/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs index 8af1726e2..07221549d 100644 --- a/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs +++ b/src/NzbDrone.Core/Download/Clients/Nzbget/Nzbget.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using NLog; using NzbDrone.Core.Messaging.Commands; using NzbDrone.Core.Parser; @@ -42,9 +43,21 @@ public override string DownloadNzb(RemoteEpisode remoteEpisode) public override IEnumerable GetQueue() { - var items = _proxy.GetQueue(Settings); + List queue; - foreach (var nzbGetQueueItem in items) + try + { + queue = _proxy.GetQueue(Settings); + } + catch (DownloadClientException ex) + { + _logger.ErrorException(ex.Message, ex); + return Enumerable.Empty(); + } + + var queueItems = new List(); + + foreach (var nzbGetQueueItem in queue) { var queueItem = new QueueItem(); queueItem.Id = nzbGetQueueItem.NzbId.ToString(); @@ -60,9 +73,10 @@ public override IEnumerable GetQueue() if (remoteEpisode.Series == null) continue; queueItem.RemoteEpisode = remoteEpisode; - - yield return queueItem; + queueItems.Add(queueItem); } + + return queueItems; } public override IEnumerable GetHistory(int start = 0, int limit = 10) diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs index f0ccf188d..d157820f0 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/Sabnzbd.cs @@ -60,11 +60,21 @@ public override IEnumerable GetQueue() { return _queueCache.Get("queue", () => { - var sabQueue = _sabnzbdProxy.GetQueue(0, 0, Settings).Items; + SabnzbdQueue sabQueue; + + try + { + sabQueue = _sabnzbdProxy.GetQueue(0, 0, Settings); + } + catch (DownloadClientException ex) + { + _logger.ErrorException(ex.Message, ex); + return Enumerable.Empty(); + } var queueItems = new List(); - foreach (var sabQueueItem in sabQueue) + foreach (var sabQueueItem in sabQueue.Items) { var queueItem = new QueueItem(); queueItem.Id = sabQueueItem.Id; @@ -91,10 +101,21 @@ public override IEnumerable GetQueue() public override IEnumerable GetHistory(int start = 0, int limit = 10) { - var items = _sabnzbdProxy.GetHistory(start, limit, Settings).Items; + SabnzbdHistory sabHistory; + + try + { + sabHistory = _sabnzbdProxy.GetHistory(start, limit, Settings); + } + catch (DownloadClientException ex) + { + _logger.ErrorException(ex.Message, ex); + return Enumerable.Empty(); + } + var historyItems = new List(); - foreach (var sabHistoryItem in items) + foreach (var sabHistoryItem in sabHistory.Items) { var historyItem = new HistoryItem(); historyItem.Id = sabHistoryItem.Id; diff --git a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs index 2a1573ba7..c5ca48167 100644 --- a/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs +++ b/src/NzbDrone.Core/Download/Clients/Sabnzbd/SabnzbdProxy.cs @@ -2,6 +2,7 @@ using System.IO; using Newtonsoft.Json.Linq; using NLog; +using NzbDrone.Common; using NzbDrone.Common.Serializer; using NzbDrone.Core.Download.Clients.Sabnzbd.Responses; using RestSharp; @@ -112,14 +113,16 @@ private IRestClient BuildClient(string action, SabnzbdSettings settings) { var protocol = settings.UseSsl ? "https" : "http"; - var url = string.Format(@"{0}://{1}:{2}/api?{3}&apikey={4}&ma_username={5}&ma_password={6}&output=json", + var authentication = settings.ApiKey.IsNullOrWhiteSpace() ? + String.Format("ma_username={0}&ma_password={1}", settings.Username, settings.Password) : + String.Format("apikey={0}", settings.ApiKey); + + var url = string.Format(@"{0}://{1}:{2}/api?{3}&{4}&output=json", protocol, settings.Host, settings.Port, action, - settings.ApiKey, - settings.Username, - settings.Password); + authentication); _logger.Trace(url); @@ -130,7 +133,7 @@ private void CheckForError(IRestResponse response) { if (response.ResponseStatus != ResponseStatus.Completed) { - throw new ApplicationException("Unable to connect to SABnzbd, please check your settings"); + throw new DownloadClientException("Unable to connect to SABnzbd, please check your settings"); } SabnzbdJsonError result; diff --git a/src/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs b/src/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs index 916dd3067..904fbb29e 100644 --- a/src/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs +++ b/src/NzbDrone.Core/MediaFiles/EpisodeFileMovingService.cs @@ -81,7 +81,15 @@ private EpisodeFile MoveFile(EpisodeFile episodeFile, Series series, string dest if (!_diskProvider.FolderExists(directoryName)) { - _diskProvider.CreateFolder(directoryName); + try + { + _diskProvider.CreateFolder(directoryName); + } + catch (IOException ex) + { + _logger.ErrorException("Unable to create directory: " + directoryName, ex); + } + SetFolderPermissions(directoryName); if (!directoryName.PathEquals(series.Path))