From d607b831c98253150308c31dd8cf697fc5d2194c Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 11 Jul 2013 23:10:34 -0700 Subject: [PATCH] added exception logging to Tasks. --- .../Messaging/MessageAggregator.cs | 4 ++- NzbDrone.Common/NzbDrone.Common.csproj | 3 ++- .../LimitedConcurrencyLevelTaskScheduler.cs | 2 +- NzbDrone.Common/TPL/TaskExtensions.cs | 25 +++++++++++++++++++ .../IndexerSearch/NzbSearchService.cs | 3 ++- .../Indexers/FetchAndParseRssService.cs | 3 ++- NzbDrone.Core/Jobs/Scheduler.cs | 5 +++- 7 files changed, 39 insertions(+), 6 deletions(-) rename NzbDrone.Common/{Messaging => TPL}/LimitedConcurrencyLevelTaskScheduler.cs (99%) create mode 100644 NzbDrone.Common/TPL/TaskExtensions.cs diff --git a/NzbDrone.Common/Messaging/MessageAggregator.cs b/NzbDrone.Common/Messaging/MessageAggregator.cs index 73f9eb335..5e73c5a4b 100644 --- a/NzbDrone.Common/Messaging/MessageAggregator.cs +++ b/NzbDrone.Common/Messaging/MessageAggregator.cs @@ -4,6 +4,7 @@ using NLog; using NzbDrone.Common.EnsureThat; using NzbDrone.Common.Serializer; +using NzbDrone.Common.TPL; namespace NzbDrone.Common.Messaging { @@ -53,7 +54,8 @@ public void PublishEvent(TEvent @event) where TEvent : class ,IEvent _logger.Debug("{0} ~> {1}", eventName, handlerLocal.GetType().Name); handlerLocal.HandleAsync(@event); _logger.Debug("{0} <~ {1}", eventName, handlerLocal.GetType().Name); - }, TaskCreationOptions.PreferFairness); + }, TaskCreationOptions.PreferFairness) + .LogExceptions(); } } diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 85eaa84e2..8c2b2a689 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -110,7 +110,7 @@ - + @@ -160,6 +160,7 @@ + diff --git a/NzbDrone.Common/Messaging/LimitedConcurrencyLevelTaskScheduler.cs b/NzbDrone.Common/TPL/LimitedConcurrencyLevelTaskScheduler.cs similarity index 99% rename from NzbDrone.Common/Messaging/LimitedConcurrencyLevelTaskScheduler.cs rename to NzbDrone.Common/TPL/LimitedConcurrencyLevelTaskScheduler.cs index a8d9238ab..bc17dcc95 100644 --- a/NzbDrone.Common/Messaging/LimitedConcurrencyLevelTaskScheduler.cs +++ b/NzbDrone.Common/TPL/LimitedConcurrencyLevelTaskScheduler.cs @@ -4,7 +4,7 @@ using System.Threading; using System.Threading.Tasks; -namespace NzbDrone.Common.Messaging +namespace NzbDrone.Common.TPL { public class LimitedConcurrencyLevelTaskScheduler : TaskScheduler { diff --git a/NzbDrone.Common/TPL/TaskExtensions.cs b/NzbDrone.Common/TPL/TaskExtensions.cs new file mode 100644 index 000000000..e655ac86f --- /dev/null +++ b/NzbDrone.Common/TPL/TaskExtensions.cs @@ -0,0 +1,25 @@ +using System.Threading.Tasks; +using NLog; + +namespace NzbDrone.Common.TPL +{ + public static class TaskExtensions + { + private static readonly Logger Logger = LogManager.GetLogger("TaskExtensions"); + + public static Task LogExceptions(this Task task) + { + task.ContinueWith(t => + { + var aggregateException = t.Exception.Flatten(); + foreach (var exception in aggregateException.InnerExceptions) + { + Logger.ErrorException("Task Error", exception); + } + + }, TaskContinuationOptions.OnlyOnFaulted); + + return task; + } + } +} \ No newline at end of file diff --git a/NzbDrone.Core/IndexerSearch/NzbSearchService.cs b/NzbDrone.Core/IndexerSearch/NzbSearchService.cs index e3f434c7a..2144986be 100644 --- a/NzbDrone.Core/IndexerSearch/NzbSearchService.cs +++ b/NzbDrone.Core/IndexerSearch/NzbSearchService.cs @@ -9,6 +9,7 @@ using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Tv; using System.Linq; +using NzbDrone.Common.TPL; namespace NzbDrone.Core.IndexerSearch { @@ -158,7 +159,7 @@ private List Dispatch(Func> { _logger.ErrorException("Error while searching for " + criteriaBase, e); } - })); + }).LogExceptions()); } Task.WaitAll(taskList.ToArray()); diff --git a/NzbDrone.Core/Indexers/FetchAndParseRssService.cs b/NzbDrone.Core/Indexers/FetchAndParseRssService.cs index 5eef13992..9ee704910 100644 --- a/NzbDrone.Core/Indexers/FetchAndParseRssService.cs +++ b/NzbDrone.Core/Indexers/FetchAndParseRssService.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using NLog; using NzbDrone.Core.Parser.Model; +using NzbDrone.Common.TPL; namespace NzbDrone.Core.Indexers { @@ -54,7 +55,7 @@ public List Fetch() { result.AddRange(indexerFeed); } - }); + }).LogExceptions(); taskList.Add(task); } diff --git a/NzbDrone.Core/Jobs/Scheduler.cs b/NzbDrone.Core/Jobs/Scheduler.cs index 0fe50bcaf..f294c74a6 100644 --- a/NzbDrone.Core/Jobs/Scheduler.cs +++ b/NzbDrone.Core/Jobs/Scheduler.cs @@ -6,6 +6,7 @@ using NzbDrone.Common.Messaging; using NzbDrone.Core.Lifecycle; using Timer = System.Timers.Timer; +using NzbDrone.Common.TPL; namespace NzbDrone.Core.Jobs { @@ -30,7 +31,9 @@ public void Handle(ApplicationStartedEvent message) { _cancellationTokenSource = new CancellationTokenSource(); Timer.Interval = 1000 * 30; - Timer.Elapsed += (o, args) => Task.Factory.StartNew(ExecuteCommands, _cancellationTokenSource.Token); + Timer.Elapsed += (o, args) => Task.Factory.StartNew(ExecuteCommands, _cancellationTokenSource.Token) + .LogExceptions(); + Timer.Start(); }