diff --git a/FilterLists.sln.DotSettings b/FilterLists.sln.DotSettings index d8617092d..a74afe9ba 100644 --- a/FilterLists.sln.DotSettings +++ b/FilterLists.sln.DotSettings @@ -15,4 +15,5 @@ CHOP_IF_LONG <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> <Policy Inspect="True" Prefix="" Suffix="" Style="aaBb" /> + True \ No newline at end of file diff --git a/src/FilterLists.Agent/Program.cs b/src/FilterLists.Agent/Program.cs index ec3380c54..b227d8cd7 100644 --- a/src/FilterLists.Agent/Program.cs +++ b/src/FilterLists.Agent/Program.cs @@ -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(); - 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..."); diff --git a/src/FilterLists.Agent/TaskExtensions.cs b/src/FilterLists.Agent/TaskExtensions.cs new file mode 100644 index 000000000..7c4067b6f --- /dev/null +++ b/src/FilterLists.Agent/TaskExtensions.cs @@ -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; + } + } + } +} \ No newline at end of file