From 6460bef6c981b1f5ece2aec0bd6c7567d6b0fbd0 Mon Sep 17 00:00:00 2001 From: Collin Barrett Date: Mon, 20 Aug 2018 18:51:08 -0500 Subject: [PATCH] add email service again to SnapshotService --- .../Extensions/ConfigureServicesCollection.cs | 1 + src/FilterLists.Services/Snapshot/Snapshot.cs | 33 +++++++++++++++---- .../Snapshot/SnapshotService.cs | 10 +++--- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs b/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs index ee93937d8..769134cb7 100644 --- a/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs +++ b/src/FilterLists.Services/DependencyInjection/Extensions/ConfigureServicesCollection.cs @@ -33,6 +33,7 @@ public static void AddFilterListsAgentServices(this IServiceCollection services, o.UseMySql(config.GetConnectionString("FilterListsConnection"), m => m.MigrationsAssembly("FilterLists.Api"))); services.TryAddScoped(); + services.TryAddSingleton(); services.AddAutoMapper(); } } diff --git a/src/FilterLists.Services/Snapshot/Snapshot.cs b/src/FilterLists.Services/Snapshot/Snapshot.cs index 3d4bc04af..e710a756b 100644 --- a/src/FilterLists.Services/Snapshot/Snapshot.cs +++ b/src/FilterLists.Services/Snapshot/Snapshot.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Net; using System.Net.Http; +using System.Text; using System.Threading; using System.Threading.Tasks; using FilterLists.Data; @@ -19,20 +20,22 @@ public class Snapshot { private const int BatchSize = 500; private readonly FilterListsDbContext dbContext; + private readonly EmailService emailService; private readonly FilterListViewUrlDto list; private readonly Data.Entities.Snapshot snapEntity; private readonly TelemetryClient telemetryClient; - public Snapshot(FilterListsDbContext dbContext, FilterListViewUrlDto list) + public Snapshot(FilterListsDbContext dbContext, EmailService emailService, FilterListViewUrlDto list) { this.dbContext = dbContext; + this.emailService = emailService; this.list = list; - telemetryClient = new TelemetryClient(); snapEntity = new Data.Entities.Snapshot { FilterListId = list.Id, AddedSnapshotRules = new List() }; + telemetryClient = new TelemetryClient(); } //TODO: add better compliance with Try/Parse pattern (https://stackoverflow.com/q/37810660/2343739) @@ -45,7 +48,7 @@ public async Task TrySaveAsync() } catch (Exception e) { - TrackException(e); + await TrackException(e); } } @@ -79,13 +82,13 @@ private async Task> TryGetLines() } catch (HttpRequestException hre) { - TrackException(hre); + await TrackException(hre); return null; } catch (WebException we) { snapEntity.HttpStatusCode = ((int)((HttpWebResponse)we.Response).StatusCode).ToString(); - TrackException(we); + await TrackException(we); return null; } } @@ -161,7 +164,25 @@ private async Task SetSuccessful() await dbContext.SaveChangesAsync(); } - private void TrackException(Exception e) + private async Task TrackException(Exception e) + { + await SendExceptionEmail(e); + TrackExceptionInApplicationInsights(e); + } + + private async Task SendExceptionEmail(Exception e) + { + var msg = new StringBuilder(); + msg.AppendLine("FilterListId: " + list.Id); + msg.AppendLine("Exception:"); + msg.AppendLine(e.Message); + msg.AppendLine(e.StackTrace); + msg.AppendLine(e.InnerException?.Message); + msg.AppendLine(e.InnerException?.StackTrace); + await emailService.SendEmailAsync("Snapshot Exception", msg.ToString()); + } + + private void TrackExceptionInApplicationInsights(Exception e) { telemetryClient.TrackException(e); telemetryClient.Flush(); diff --git a/src/FilterLists.Services/Snapshot/SnapshotService.cs b/src/FilterLists.Services/Snapshot/SnapshotService.cs index 35e062a5a..b77d7eb6a 100644 --- a/src/FilterLists.Services/Snapshot/SnapshotService.cs +++ b/src/FilterLists.Services/Snapshot/SnapshotService.cs @@ -12,12 +12,12 @@ namespace FilterLists.Services.Snapshot { public class SnapshotService : Service { + private readonly EmailService emailService; private readonly DateTime yesterday = DateTime.UtcNow.AddDays(-1); - public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig) - : base(dbContext, mapConfig) - { - } + public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider mapConfig, + EmailService emailService) + : base(dbContext, mapConfig) => this.emailService = emailService; public async Task CaptureAsync(int batchSize) { @@ -45,7 +45,7 @@ await DbContext .ToListAsync(); private IEnumerable CreateSnapshots(IEnumerable lists) => - lists.Select(l => new Snapshot(DbContext, l)); + lists.Select(l => new Snapshot(DbContext, emailService, l)); private static async Task SaveSnapshots(IEnumerable snapshots) {