added exception logging to Tasks.

pull/3113/head
kay.one 11 years ago
parent 508b087c46
commit d607b831c9

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

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

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

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

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

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

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

Loading…
Cancel
Save