added exception logging to Tasks.

This commit is contained in:
kay.one 2013-07-11 23:10:34 -07:00
parent 508b087c46
commit d607b831c9
7 changed files with 39 additions and 6 deletions

View File

@ -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();
}
}

View File

@ -110,7 +110,7 @@
<Compile Include="Exceptions\NzbDroneException.cs" />
<Compile Include="Instrumentation\GlobalExceptionHandlers.cs" />
<Compile Include="Instrumentation\ExceptronTarget.cs" />
<Compile Include="Messaging\LimitedConcurrencyLevelTaskScheduler.cs" />
<Compile Include="TPL\LimitedConcurrencyLevelTaskScheduler.cs" />
<Compile Include="Security\IgnoreCertErrorPolicy.cs" />
<Compile Include="StringExtensions.cs" />
<Compile Include="EnsureThat\TypeParam.cs" />
@ -160,6 +160,7 @@
<Compile Include="RestProvider.cs" />
<Compile Include="ServiceProvider.cs" />
<Compile Include="TinyIoC.cs" />
<Compile Include="TPL\TaskExtensions.cs" />
<Compile Include="TryParseExtension.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -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
{

View File

@ -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;
}
}
}

View File

@ -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());

View File

@ -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);
}

View File

@ -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();
}