add email notifications for SnapshotService errors

ref #12
This commit is contained in:
Collin Barrett 2018-08-13 18:14:37 -05:00
parent 1a0acff9ee
commit 3f46ab6d37
9 changed files with 106 additions and 10 deletions

View file

@ -28,7 +28,7 @@ public static void Main()
private static void BuildConfigurationRoot() =>
configurationRoot = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true)
.AddJsonFile("appsettings.json", false)
.Build();
private static void InstantiateTelemetryClient()

View file

@ -6,10 +6,21 @@
*/
{
"ApplicationInsights": {
"InstrumentationKey": "InstrumentationKeyValue"
"InstrumentationKey": ""
},
"ConnectionStrings": {
"FilterListsConnection": "FilterListsConnectionValue"
"FilterListsConnection": ""
},
"SendGrid": {
"ApiKey": "",
"From": {
"Name": "",
"EmailAddress": ""
},
"To": {
"Name": "",
"EmailAddress": ""
}
},
"DataDirectory": {
"Path": "..\\..\\data"

View file

@ -6,10 +6,21 @@
*/
{
"ApplicationInsights": {
"InstrumentationKey": "InstrumentationKeyValue"
"InstrumentationKey": ""
},
"ConnectionStrings": {
"FilterListsConnection": "FilterListsConnectionValue"
"FilterListsConnection": ""
},
"SendGrid": {
"ApiKey": "",
"From": {
"Name": "",
"EmailAddress": ""
},
"To": {
"Name": "",
"EmailAddress": ""
}
},
"DataDirectory": {
"Path": "..\\..\\data"

View file

@ -35,6 +35,7 @@ public static void AddFilterListsAgentServices(this IServiceCollection services,
x => x.MigrationsAssembly("FilterLists.Api"))
.EnableSensitiveDataLogging());
services.TryAddScoped<SnapshotService>();
services.TryAddScoped<EmailService>();
services.AddAutoMapper();
}
}

View file

@ -0,0 +1,38 @@
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Extensions.Configuration;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace FilterLists.Services
{
[UsedImplicitly]
public class EmailService
{
private readonly SendGridClient sendGridClient;
private readonly EmailAddress fromEmailAddress;
private readonly EmailAddress toEmailAddress;
public EmailService(IConfiguration configuration)
{
var sendGridConfig = configuration.GetSection("SendGrid");
sendGridClient = new SendGridClient(sendGridConfig.GetValue<string>("ApiKey"));
fromEmailAddress = new EmailAddress(sendGridConfig.GetSection("From").GetValue<string>("EmailAddress"),
sendGridConfig.GetSection("From").GetValue<string>("Name"));
toEmailAddress = new EmailAddress(sendGridConfig.GetSection("To").GetValue<string>("EmailAddress"),
sendGridConfig.GetSection("To").GetValue<string>("Name"));
}
public async Task SendEmailAsync(string subjectBody, string message)
{
var msg = new SendGridMessage
{
From = fromEmailAddress,
Subject = "[FilterLists] " + subjectBody,
PlainTextContent = message
};
msg.AddTo(toEmailAddress);
await sendGridClient.SendEmailAsync(msg);
}
}
}

View file

@ -20,6 +20,7 @@
<PackageReference Include="JetBrains.Annotations" Version="2018.2.1" />
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.4.0" />
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.2.12-alpha" />
<PackageReference Include="SendGrid" Version="9.9.0" />
</ItemGroup>
<ItemGroup>

View file

@ -9,6 +9,7 @@ public class Service
{
protected readonly IConfigurationProvider ConfigurationProvider;
protected readonly FilterListsDbContext DbContext;
protected readonly EmailService EmailService;
public Service(FilterListsDbContext dbContext) => DbContext = dbContext;
@ -17,5 +18,13 @@ public Service(FilterListsDbContext dbContext, IConfigurationProvider configurat
DbContext = dbContext;
ConfigurationProvider = configurationProvider;
}
public Service(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider,
EmailService emailService)
{
DbContext = dbContext;
ConfigurationProvider = configurationProvider;
EmailService = emailService;
}
}
}

View file

@ -3,6 +3,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;
@ -18,12 +19,14 @@ public class SnapshotDe
private const int BatchSize = 1000;
private readonly FilterListsDbContext dbContext;
private readonly EmailService emailService;
private readonly FilterListViewUrlDto list;
private Data.Entities.Snapshot snapshot;
public SnapshotDe(FilterListsDbContext dbContext, FilterListViewUrlDto list)
public SnapshotDe(FilterListsDbContext dbContext, EmailService emailService, FilterListViewUrlDto list)
{
this.dbContext = dbContext;
this.emailService = emailService;
this.list = list;
}
@ -62,12 +65,14 @@ private async Task<string> TryGetContent()
catch (WebException we)
{
snapshot.HttpStatusCode = ((int)((HttpWebResponse)we.Response).StatusCode).ToString();
await SendWebExceptionEmail();
return null;
}
catch (Exception)
catch (Exception e)
{
//TODO: log exception (#148)
snapshot.HttpStatusCode = null;
await SendExceptionEmail(e);
return null;
}
}
@ -85,9 +90,28 @@ private async Task<string> GetContent()
}
}
await SendWebExceptionEmail();
return null;
}
private async Task SendWebExceptionEmail()
{
var message = new StringBuilder();
message.AppendLine("Snapshot WebException");
message.AppendLine("FilterListId: " + snapshot.FilterList.Id);
message.AppendLine("HTTP Status Code: " + snapshot.HttpStatusCode);
await emailService.SendEmailAsync("Snapshot WebException", message.ToString());
}
private async Task SendExceptionEmail(Exception e)
{
var message = new StringBuilder();
message.AppendLine("Snapshot Exception");
message.AppendLine("FilterListId: " + snapshot.FilterList.Id);
message.AppendLine("Exception: " + e.Message);
await emailService.SendEmailAsync("Snapshot Exception", message.ToString());
}
private async Task SaveSnapshotInBatches(string content)
{
var rawRules = GetRawRules(content);

View file

@ -19,8 +19,9 @@ public class SnapshotService : Service
private static readonly IList<uint> IgnoreLists =
new ReadOnlyCollection<uint>(new List<uint> {48, 149, 173, 185, 186, 187, 188, 189, 352});
public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider)
: base(dbContext, configurationProvider)
public SnapshotService(FilterListsDbContext dbContext, IConfigurationProvider configurationProvider,
EmailService emailService)
: base(dbContext, configurationProvider, emailService)
{
}
@ -54,7 +55,7 @@ await DbContext
.ToListAsync();
private IEnumerable<SnapshotDe> GetSnapshots(IEnumerable<FilterListViewUrlDto> lists) =>
lists.Select(list => new SnapshotDe(DbContext, list));
lists.Select(list => new SnapshotDe(DbContext, EmailService, list));
private static async Task SaveSnapshots(IEnumerable<SnapshotDe> snapshots)
{