add 1-hr timeout to Agent

ref #251
This commit is contained in:
Collin M. Barrett 2018-08-22 09:26:27 -05:00
parent fd1a7cfb73
commit 7bcd1ba389
3 changed files with 40 additions and 4 deletions

View file

@ -15,4 +15,5 @@
<s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/WRAP_CHAINED_METHOD_CALLS/@EntryValue">CHOP_IF_LONG</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateInstanceFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/PredefinedNamingRules/=PrivateStaticFields/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /&gt;</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=appsettings/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>

View file

@ -16,16 +16,14 @@ public static class Program
private static ServiceProvider serviceProvider;
private static SnapshotService snapshotService;
private static Logger logger;
private static readonly TimeSpan OneHourTimeout = TimeSpan.FromHours(1);
public static async Task Main()
{
BuildConfigRoot();
BuildServiceProvider();
snapshotService = serviceProvider.GetService<SnapshotService>();
using (logger = new Logger(configRoot[AppInsightsKeyConfig]))
{
await CaptureSnapshots(BatchSize);
}
await TryCaptureSnapshots();
}
private static void BuildConfigRoot() =>
@ -41,6 +39,21 @@ private static void BuildServiceProvider()
serviceProvider = serviceCollection.BuildServiceProvider();
}
private static async Task TryCaptureSnapshots()
{
using (logger = new Logger(configRoot[AppInsightsKeyConfig]))
{
try
{
await CaptureSnapshots(BatchSize).TimeoutAfter(OneHourTimeout);
}
catch (TimeoutException)
{
logger.Log("Timeout - Program.CaptureSnapshots()");
}
}
}
private static async Task CaptureSnapshots(int batchSize)
{
logger.Log("Capturing FilterList snapshots...");

View file

@ -0,0 +1,22 @@
using System;
using System.Threading;
using System.Threading.Tasks;
namespace FilterLists.Agent
{
public static class TaskExtensions
{
//https://stackoverflow.com/a/22078975/2343739
public static async Task TimeoutAfter(this Task task, TimeSpan timeout)
{
using (var cancellationTokenSource = new CancellationTokenSource())
{
var completedTask = await Task.WhenAny(task, Task.Delay(timeout, cancellationTokenSource.Token));
if (completedTask != task)
throw new TimeoutException("The operation has timed out.");
cancellationTokenSource.Cancel();
await task;
}
}
}
}