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
@@ -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())