mirror of
https://github.com/collinbarrett/FilterLists.git
synced 2026-03-11 09:04:27 +00:00
add email service again to SnapshotService
This commit is contained in:
parent
9210d6b75e
commit
6460bef6c9
3 changed files with 33 additions and 11 deletions
|
|
@ -33,6 +33,7 @@ public static void AddFilterListsAgentServices(this IServiceCollection services,
|
|||
o.UseMySql(config.GetConnectionString("FilterListsConnection"),
|
||||
m => m.MigrationsAssembly("FilterLists.Api")));
|
||||
services.TryAddScoped<SnapshotService>();
|
||||
services.TryAddSingleton<EmailService>();
|
||||
services.AddAutoMapper();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<SnapshotRule>()
|
||||
};
|
||||
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<IEnumerable<string>> 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();
|
||||
|
|
|
|||
|
|
@ -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<Snapshot> CreateSnapshots(IEnumerable<FilterListViewUrlDto> lists) =>
|
||||
lists.Select(l => new Snapshot(DbContext, l));
|
||||
lists.Select(l => new Snapshot(DbContext, emailService, l));
|
||||
|
||||
private static async Task SaveSnapshots(IEnumerable<Snapshot> snapshots)
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue