Email notification for LeafWeb jobs

This commit is contained in:
2016-03-09 11:43:44 -05:00
parent 314de4dece
commit e80ddbc4d6
10 changed files with 168 additions and 9 deletions
+1 -1
View File
@@ -17,7 +17,7 @@
<rules>
<logger name="Hangfire.*" minlevel="Debug" maxlevel="Info" final="true"/>
<logger name="*" minlevel="Trace" writeTo="debugLogger"/>
<logger name="*" minlevel="Debug" writeTo="debugLogger"/>
<logger name="*" minlevel="Error" writeTo="exceptionLogger" />
</rules>
</nlog>
+93
View File
@@ -0,0 +1,93 @@
using System;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Mail;
using LeafWeb.Core.DAL;
using LeafWeb.Core.Entities;
using NLog;
namespace LeafWeb.Web.Services
{
public class EmailNotificationService : IDisposable
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private readonly string _emaialFromAddress;
private readonly SmtpClient _smtpClient;
private readonly DataService _dataService;
public EmailNotificationService(DataService dataService)
{
_dataService = dataService;
_smtpClient = new SmtpClient();
_emaialFromAddress = ConfigurationManager.AppSettings["EmailFromAddress"];
}
public EmailNotificationService() : this(new DataService())
{ }
public void SendLeafWebError(int leafWebFileId, string errorMessage)
{
var file = _dataService.GetLeafInputFile(leafWebFileId);
var body = $"Your LeafWeb analysis job, {file.LeafInput.Identifier} (with filename {file.Filename}), encountered the following errors." + Environment.NewLine
+ "You will need to correct your input and resubmit." + Environment.NewLine + Environment.NewLine
+ errorMessage;
var message = new MailMessage(_emaialFromAddress, file.LeafInput.Email, "LeafWeb processing error", body);
SendMessage(message);
}
public void SendLeafWebSuccess(int leafWebFileId)
{
var file = _dataService.GetLeafInputFile(leafWebFileId);
var body = $"Your LeafWeb analysis job, {file.LeafInput.Identifier} (with filename {file.Filename}), has completed." + Environment.NewLine;
var message = new MailMessage(_emaialFromAddress, file.LeafInput.Email, "LeafWeb results", body);
var fileStreams =
(from outputFile in
file.LeafOutputFiles
select Tuple.Create(outputFile, new MemoryStream(outputFile.Contents))).ToList();
try
{
foreach (var fileStream in fileStreams)
{
var attachment = new Attachment(fileStream.Item2, fileStream.Item1.Filename);
message.Attachments.Add(attachment);
}
SendMessage(message);
}
finally
{
// can't dispose those memory streams until the message is sent
foreach (var stream in fileStreams.Select(f => f.Item2))
{
stream.Dispose();
}
}
}
private void SendMessage(MailMessage mailMessage)
{
try
{
Logger.Debug("Email sending to " + mailMessage.To + ", subject: " + mailMessage.Subject);
_smtpClient.Send(mailMessage);
}
catch (SmtpException ex)
{
Logger.Error(ex, "Failed to send mail: {0}", ex.Message);
}
}
public void Dispose()
{
_dataService.Dispose();
}
}
}
+13 -2
View File
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading;
using Hangfire;
using LeafWeb.Core.DAL;
using LeafWeb.Core.Entities;
using LeafWeb.Core.Remote;
@@ -12,14 +13,16 @@ namespace LeafWeb.Web.Services
{
private readonly DataService _dataService;
private readonly PiscalService _piscalService;
private readonly EmailNotificationService _emailService;
public PiscalQueueManager(DataService dataService, PiscalService piscalService)
public PiscalQueueManager(DataService dataService, PiscalService piscalService, EmailNotificationService emailService)
{
_dataService = dataService;
_piscalService = piscalService;
_emailService = emailService;
}
public PiscalQueueManager() : this(new DataService(), new PiscalService()) {}
public PiscalQueueManager() : this(new DataService(), new PiscalService(), new EmailNotificationService()) {}
private static readonly object ProcessQueueLock = new object();
@@ -109,6 +112,8 @@ namespace LeafWeb.Web.Services
// update db
_dataService.SetLeafInputFileStatus(file, LeafInputStatusType.Complete);
BackgroundJob.Enqueue(() => _emailService.SendLeafWebSuccess(file.Id));
// remove working data from the server
logger.Info("LeafInputFile: {0}, Cleanup", file.Id);
_piscalService.Cleanup(file);
@@ -127,6 +132,12 @@ namespace LeafWeb.Web.Services
logger.Info("LeafInputFile: {0}, Error Message: {1}", file.Id, errorMessage);
_dataService.SetLeafInputFileStatus(file, LeafInputStatusType.Error, errorMessage);
BackgroundJob.Enqueue(() => _emailService.SendLeafWebError(file.Id, errorMessage));
// remove working data from the server
logger.Info("LeafInputFile: {0}, Cleanup", file.Id);
_piscalService.Cleanup(file);
break;
}
}
+10 -2
View File
@@ -22,8 +22,16 @@
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="TimeZoneId" value="Pacific Standard Time" />
</appSettings>
<add key="TimeZoneId" value="US Eastern Standard Time" />
<add key="EmailFromAddress" value="LeafWeb &lt;noreply@leafweb.org&gt;" />
</appSettings>
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="LeafWeb &lt;noreply@leafweb.org&gt;">
<network host="localhost" />
</smtp>
</mailSettings>
</system.net>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<!-- max 1GB -->
+1
View File
@@ -933,6 +933,7 @@
</Compile>
<Compile Include="Attributes\HttpParamActionAttribute.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Services\EmailNotificationService.cs" />
<Compile Include="Services\PiscalQueueManager.cs" />
<Compile Include="Services\PiscalService.cs" />
<Compile Include="HangfireStartup.cs" />