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 e2857cf11..7f9d2acdf 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.Tasks; using FilterLists.Data; using FilterLists.Data.Entities.Junctions; @@ -17,12 +18,14 @@ public class Snapshot { private const int BatchSize = 1000; private readonly FilterListsDbContext dbContext; + private readonly EmailService emailService; private readonly FilterListViewUrlDto list; private readonly Data.Entities.Snapshot snapEntity; - public Snapshot(FilterListsDbContext dbContext, FilterListViewUrlDto list) + public Snapshot(FilterListsDbContext dbContext, EmailService emailService, FilterListViewUrlDto list) { this.dbContext = dbContext; + this.emailService = emailService; this.list = list; snapEntity = new Data.Entities.Snapshot { @@ -39,10 +42,10 @@ public async Task TrySaveAsync() { await SaveAsync(); } - catch (Exception) + catch (Exception e) { //allow other snapshots to continue - //TODO: log + await SendExceptionEmail(e); } } @@ -155,5 +158,17 @@ private async Task SetSuccessful() snapEntity.WasSuccessful = true; await dbContext.SaveChangesAsync(); } + + 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()); + } } } \ No newline at end of file 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) {