Add cancel

This commit is contained in:
2017-01-26 08:36:54 -05:00
parent 295b40bed8
commit 4338b4fee5
30 changed files with 333 additions and 62 deletions
+30 -4
View File
@@ -20,6 +20,7 @@ namespace LeafWeb.WebCms.Services
private const string EmailSuccessSubject = "LeafWeb results";
private const string EmailErrorSubject = "LeafWeb processing error";
private const string EmailSystemErrorSubject = "LeafWeb system error";
private const string EmailCancelledSubject = "LeafWeb cancelled";
/// <summary>
/// Comma separated values
@@ -32,6 +33,11 @@ namespace LeafWeb.WebCms.Services
private readonly DownloadUrlService _downloadUrlService;
private string FormatSubject(string subject, LeafInput leafInput)
{
return subject + $" - '{leafInput.Identifier}'";
}
public EmailNotificationService(DataService dataService)
{
_dataService = dataService;
@@ -60,8 +66,10 @@ namespace LeafWeb.WebCms.Services
var leafInput = _dataService.GetLeafInput(leafInputId);
if (leafInput.CurrentStatus != LeafInputStatusType.Complete)
{
Logger.Error($"Attempting to SendLeafWebComplete when status is not complete for leafInput: {leafInput}, current status: {leafInput.CurrentStatus}");
throw new ArgumentException($"Attempting to SendLeafWebComplete when status is not complete for leafInput: {leafInput}, current status: {leafInput.CurrentStatus}");
var notComplete = "Attempting to SendLeafWebComplete when status is not complete" +
$" for leafInput: {leafInput}, current status: {leafInput.CurrentStatus}";
Logger.Error(notComplete);
throw new ArgumentException(notComplete);
}
var outputErrorMessage = leafInput.OutputErrorMessage;
@@ -71,6 +79,24 @@ namespace LeafWeb.WebCms.Services
SendLeafWebSuccess(leafInput);
}
public void SendLeafWebCancelled(int leafInputId)
{
var leafInput = _dataService.GetLeafInput(leafInputId);
if (leafInput.CurrentStatus != LeafInputStatusType.Cancelled)
{
var notComplete = "Attempting to SendLeafWebCancelled when status is not complete " +
$"for leafInput: {leafInput}, current status: {leafInput.CurrentStatus}";
Logger.Error(notComplete);
throw new ArgumentException(notComplete);
}
var body = $"Your leaf analysis job, {leafInput.Identifier}, has been cancelled. " +
"Contact the administrator with any questions.";
var message = new MailMessage(_emailFromAddress, leafInput.Email, FormatSubject(EmailCancelledSubject, leafInput), body);
SendMessage(message);
}
public void SendAdministratorMessage(string subject, string body)
{
var message = new MailMessage(_emailFromAddress, _adminEmailAddresses, subject, body);
@@ -94,7 +120,7 @@ namespace LeafWeb.WebCms.Services
+ Environment.NewLine + Environment.NewLine
+ downloadUrl;
var message = new MailMessage(_emailFromAddress, leafInput.Email, EmailSuccessSubject, body);
var message = new MailMessage(_emailFromAddress, leafInput.Email, FormatSubject(EmailSuccessSubject, leafInput), body);
SendMessage(message);
}
else
@@ -138,7 +164,7 @@ namespace LeafWeb.WebCms.Services
body += FormatWarningMessage(leafInput);
var message = new MailMessage(_emailFromAddress, leafInput.Email, EmailErrorSubject, body);
var message = new MailMessage(_emailFromAddress, leafInput.Email, FormatSubject(EmailErrorSubject, leafInput), body);
SendMessage(message);
}
@@ -19,11 +19,13 @@ namespace LeafWeb.WebCms.Services.PiscalQueue
try
{
UpdateCancelling();
StartCancelPending();
UpdateRunning();
StartNextPending();
// TODO: handle starting and finishing
}
finally
{
@@ -38,27 +40,100 @@ namespace LeafWeb.WebCms.Services.PiscalQueue
}
}
// TODO: clear any stalled processes
private void ClearStalled()
private void StartCancelPending()
{
var leafInputs =
var cancelPendingLeafInputs =
DataService.GetLeafInputs(
LeafInputStatusType.Starting,
LeafInputStatusType.Finishing
)
.Where(li =>
li.StatusHistory.OrderBy(sh => sh.DateTime).First().DateTime
> DateTime.Now.Subtract(TimeSpan.FromHours(1)))
.ToList();
LeafInputStatusType.CancelPending
).ToList();
foreach (var leafInput in cancelPendingLeafInputs)
{
try
{
var status = PiscalService.GetStatus(leafInput);
switch (status)
{
case PiscalStatus.Running:
Logger.DebugFormat("LeafInput: {0}, Set Cancelling", leafInput.Id);
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Cancelling);
Logger.InfoFormat("LeafInput: {0}, Kill", leafInput.Id);
PiscalService.Kill(leafInput);
break;
case PiscalStatus.Complete:
Logger.DebugFormat("LeafInput: {0}, Piscal Complete after cancelled - " +
"setting to Running to copy output and notify user", leafInput.Id);
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Running,
"Piscal Complete after cancelled - setting to Running to copy output and notify user");
break;
}
}
catch (PiscalClientException ex)
{
PiscalExceptionHandler(ex, leafInput);
}
catch (Exception ex)
{
var errorMessage = FormatException(ex);
Logger.Error(errorMessage);
}
}
}
private void UpdateCancelling()
{
var cancellingLeafInputs =
DataService.GetLeafInputs(
LeafInputStatusType.Cancelling
).ToList();
foreach (var leafInput in cancellingLeafInputs)
{
try
{
var status = PiscalService.GetStatus(leafInput);
switch (status)
{
case PiscalStatus.Running:
Logger.InfoFormat("LeafInput: {0}, Piscal Running - still cancelling", leafInput.Id);
// continue running
break;
case PiscalStatus.Complete:
Logger.DebugFormat("LeafInput: {0}, Set Cancelled", leafInput.Id);
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Cancelled,
"Emailing cancellation notification to user and cleaning up files on Piscal",
$"Email: \'{leafInput.Email}\'");
BackgroundJobEnqueueRetry<EmailNotificationService>(email => email.SendLeafWebCancelled(leafInput.Id));
Logger.InfoFormat("LeafInput: {0}, Cleanup", leafInput.Id);
PiscalService.Cleanup(leafInput);
break;
}
}
catch (PiscalClientException ex)
{
PiscalExceptionHandler(ex, leafInput);
}
catch (Exception ex)
{
var errorMessage = FormatException(ex);
Logger.Error(errorMessage);
}
}
}
private void StartNextPending()
{
var runningLeafInputs =
DataService.GetLeafInputs(
LeafInputStatusType.Starting,
LeafInputStatusType.Running,
LeafInputStatusType.Finishing
LeafInputStatusType.Finishing
).ToList();
if (runningLeafInputs.Any())
+7 -1
View File
@@ -35,7 +35,7 @@ namespace LeafWeb.WebCms.Services.PiscalQueue
if (!string.IsNullOrEmpty(_notifyCompleteUrl))
inputFile.NotifyCompleteUrl = _notifyCompleteUrl;
// TODO: remove this, just for testing
// NOTE: Assume that from this address we don't want to store it
if (string.Equals(leafInput.Email, "james.kolpack@gmail.com", StringComparison.InvariantCultureIgnoreCase))
inputFile.SuppressStorageCopy = true;
_piscalClient.RunLeafInput(inputFile);
@@ -64,5 +64,11 @@ namespace LeafWeb.WebCms.Services.PiscalQueue
var input = new PiscalLeafInput(leafInput);
_piscalClient.CleanupLeafProcess(input);
}
public void Kill(LeafInput leafInput)
{
var input = new PiscalLeafInput(leafInput);
_piscalClient.KillLeafProcess(input);
}
}
}