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 System.Threading.Tasks; using NLog; using NzbDrone.Common.EnsureThat; using NzbDrone.Common.Serializer; +using NzbDrone.Common.TPL; namespace NzbDrone.Common.Messaging { @@ -53,7 +54,8 @@ namespace NzbDrone.Common.Messaging _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.Linq; 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.Indexers; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Tv; using System.Linq; +using NzbDrone.Common.TPL; namespace NzbDrone.Core.IndexerSearch { @@ -158,7 +159,7 @@ namespace NzbDrone.Core.IndexerSearch { _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.Linq; using System.Threading.Tasks; using NLog; using NzbDrone.Core.Parser.Model; +using NzbDrone.Common.TPL; namespace NzbDrone.Core.Indexers { @@ -54,7 +55,7 @@ namespace NzbDrone.Core.Indexers { 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.Composition; 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 @@ namespace NzbDrone.Core.Jobs { _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(); }