Add cancel
This commit is contained in:
@@ -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())
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user