Add Hangfire
Adjust namespace
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using LeafWeb.Core.Entities;
|
||||
using LeafWeb.Core.Parsers;
|
||||
using LeafWeb.WebCms.App_Start;
|
||||
|
||||
namespace LeafWeb.WebCms.Services.PiscalQueue
|
||||
{
|
||||
public class FinishComplete : PiscalQueueWorker
|
||||
{
|
||||
protected override void DoWorkInternal(LeafInput leafInput)
|
||||
{
|
||||
Logger.DebugFormat("LeafInput: {0}, RetrieveOutputFiles", leafInput.Id);
|
||||
|
||||
var leafOutputFiles = PiscalService.RetrieveOutputFiles(leafInput).ToList();
|
||||
|
||||
Logger.DebugFormat("LeafInput: {0}, RetrieveOutputFiles saving output files", leafInput.Id);
|
||||
|
||||
foreach (var outputFile in leafOutputFiles)
|
||||
{
|
||||
if (leafInput.OutputFiles.All(file => file.Filename != outputFile.Filename))
|
||||
{
|
||||
DataService.AddLeafOutputFile(outputFile);
|
||||
|
||||
// parse cleaned input file data
|
||||
if (outputFile.FileType == LeafOutputFileType.CleanedInput)
|
||||
AddLeafInputData(outputFile, leafInput);
|
||||
}
|
||||
else
|
||||
Logger.WarnFormat("LeafInput: {0}, RetrieveOutputFiles duplicate file name: {1}", leafInput.Id, outputFile.Filename);
|
||||
}
|
||||
|
||||
Logger.InfoFormat("LeafInput: {0}, RetrieveOutputFiles output files: {1}", leafInput.Id,
|
||||
string.Join(", ", leafOutputFiles.Select(o => o.Filename)));
|
||||
|
||||
Logger.DebugFormat("LeafInput: {0}, Set Complete", leafInput.Id);
|
||||
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Complete);
|
||||
|
||||
BackgroundJobEnqueueRetry<EmailNotificationService>(email => email.SendLeafWebComplete(leafInput.Id));
|
||||
|
||||
Logger.InfoFormat("LeafInput: {0}, Cleanup", leafInput.Id);
|
||||
PiscalService.Cleanup(leafInput);
|
||||
|
||||
HangfireStartup.TriggerPiscalProcessQueue();
|
||||
}
|
||||
|
||||
private void AddLeafInputData(LeafOutputFile outputFile, LeafInput leafInput)
|
||||
{
|
||||
try
|
||||
{
|
||||
var parser = new LeafInputCsvParser(outputFile.FileContents.Contents);
|
||||
var data = parser.Parse();
|
||||
data.LeafInput = leafInput;
|
||||
data.LeafOutputFile = outputFile;
|
||||
leafInput.LeafInputData.Add(data);
|
||||
DataService.UpdateLeafInput(leafInput);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Logger.Error($"LeafInput: {leafInput.Id}, while parsing CleanedInput file: {outputFile.Filename}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using Hangfire;
|
||||
using log4net;
|
||||
using LeafWeb.Core.DAL;
|
||||
using LeafWeb.Core.Entities;
|
||||
using LeafWeb.Core.Remote;
|
||||
using Polly;
|
||||
|
||||
namespace LeafWeb.WebCms.Services.PiscalQueue
|
||||
{
|
||||
public abstract class PiscalQueueBase : IDisposable
|
||||
{
|
||||
protected readonly DataService DataService;
|
||||
protected readonly PiscalService PiscalService;
|
||||
protected readonly ILog Logger;
|
||||
private readonly Policy _retryPolicy;
|
||||
|
||||
protected PiscalQueueBase(DataService dataService, PiscalService piscalService)
|
||||
{
|
||||
DataService = dataService;
|
||||
PiscalService = piscalService;
|
||||
Logger = LogManager.GetLogger(GetType().Name);
|
||||
|
||||
_retryPolicy =
|
||||
Policy
|
||||
.Handle<TimeoutException>()
|
||||
.Retry(3,
|
||||
(exception, i) => Logger.Warn($"Retry {i} after exception: {exception.Message}"));
|
||||
}
|
||||
|
||||
protected PiscalQueueBase() : this(new DataService(), new PiscalService()) { }
|
||||
|
||||
protected string FormatException(Exception ex)
|
||||
{
|
||||
return
|
||||
(ex is PiscalClientException ? $"LeafInput: {((PiscalClientException) ex).LeafInputId}{Environment.NewLine}" : "") +
|
||||
$"Class: {GetType().Name}{Environment.NewLine}" +
|
||||
$"Exception message: {ex.Message}{Environment.NewLine}" +
|
||||
(ex.InnerException != null ? $"InnerException: {ex.InnerException}{Environment.NewLine}" : string.Empty)
|
||||
+ $"StackTrace: {ex.StackTrace}";
|
||||
}
|
||||
|
||||
protected void PiscalExceptionHandler(PiscalClientException ex, LeafInput leafInput)
|
||||
{
|
||||
var errorMessage = FormatException(ex);
|
||||
Logger.Error(errorMessage);
|
||||
|
||||
// send admin an email
|
||||
BackgroundJobEnqueueRetry<EmailNotificationService>(
|
||||
email => email.SendAdministratorMessage($"LeafWeb: PiscalQueue {GetType().Name} Exception", errorMessage));
|
||||
|
||||
// send user email too
|
||||
BackgroundJobEnqueueRetry<EmailNotificationService>(
|
||||
email => email.SendLeafWebSystemException(leafInput.Identifier, leafInput.Email));
|
||||
|
||||
if (leafInput != null)
|
||||
{
|
||||
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Exception, "Error occurred processing LeafInput",
|
||||
ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
DataService.Dispose();
|
||||
}
|
||||
|
||||
protected string BackgroundJobEnqueueRetry<T>(Expression<Action<T>> a)
|
||||
{
|
||||
return _retryPolicy.Execute(() => BackgroundJob.Enqueue(a));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using LeafWeb.Core.Entities;
|
||||
using LeafWeb.Core.Remote;
|
||||
|
||||
namespace LeafWeb.WebCms.Services.PiscalQueue
|
||||
{
|
||||
public class PiscalQueueManager : PiscalQueueBase
|
||||
{
|
||||
private static readonly object ProcessQueueLock = new object();
|
||||
|
||||
public void ProcessQueue()
|
||||
{
|
||||
// prevent multiple entry into processing the queue
|
||||
if (Monitor.TryEnter(ProcessQueueLock))
|
||||
{
|
||||
Logger.DebugFormat("ProcessQueue entered");
|
||||
|
||||
try
|
||||
{
|
||||
UpdateRunning();
|
||||
|
||||
StartNextPending();
|
||||
|
||||
// TODO: handle starting and finishing
|
||||
}
|
||||
finally
|
||||
{
|
||||
Logger.DebugFormat("ProcessQueue exit");
|
||||
|
||||
Monitor.Exit(ProcessQueueLock);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Logger.DebugFormat("ProcessQueue locked, queue already processing");
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: clear any stalled processes
|
||||
private void ClearStalled()
|
||||
{
|
||||
var leafInputs =
|
||||
DataService.GetLeafInputs(
|
||||
LeafInputStatusType.Starting,
|
||||
LeafInputStatusType.Finishing
|
||||
)
|
||||
.Where(li =>
|
||||
li.StatusHistory.OrderBy(sh => sh.DateTime).First().DateTime
|
||||
> DateTime.Now.Subtract(TimeSpan.FromHours(1)))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
private void StartNextPending()
|
||||
{
|
||||
var runningLeafInputs =
|
||||
DataService.GetLeafInputs(
|
||||
LeafInputStatusType.Starting,
|
||||
LeafInputStatusType.Running,
|
||||
LeafInputStatusType.Finishing
|
||||
).ToList();
|
||||
|
||||
if (runningLeafInputs.Any())
|
||||
{
|
||||
Logger.DebugFormat("Leaf input(s) currently running");
|
||||
return;
|
||||
}
|
||||
|
||||
var pendingInput =
|
||||
DataService
|
||||
.GetLeafInputs(LeafInputStatusType.Pending)
|
||||
.OrderBy(l => l.StatusHistory.Min(sh => sh.DateTime))
|
||||
.FirstOrDefault();
|
||||
|
||||
if (pendingInput == null)
|
||||
{
|
||||
Logger.DebugFormat("No pending leaf input");
|
||||
return;
|
||||
}
|
||||
|
||||
var pendingInputId = pendingInput.Id;
|
||||
Logger.InfoFormat("LeafInput: {0}, Starting", pendingInputId);
|
||||
try
|
||||
{
|
||||
DataService.SetLeafInputStatus(pendingInput, LeafInputStatusType.Starting);
|
||||
BackgroundJobEnqueueRetry<StartPending>(c => c.DoWork(pendingInputId));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMessage = FormatException(ex);
|
||||
Logger.Error(errorMessage);
|
||||
DataService.SetLeafInputStatus(pendingInput, LeafInputStatusType.Exception, ex.Message, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateRunning()
|
||||
{
|
||||
var running = DataService.GetLeafInputs(LeafInputStatusType.Running).ToList();
|
||||
foreach (var leafInput in running)
|
||||
{
|
||||
try
|
||||
{
|
||||
var status = PiscalService.GetStatus(leafInput);
|
||||
var leafInputId = leafInput.Id;
|
||||
switch (status)
|
||||
{
|
||||
case PiscalStatus.NotStarted:
|
||||
// if it's not started - this is unusual state
|
||||
Logger.WarnFormat("LeafInput: {0}, Piscal Not Started", leafInput.Id);
|
||||
break;
|
||||
|
||||
case PiscalStatus.Running:
|
||||
Logger.DebugFormat("LeafInput: {0}, Piscal Running", leafInput.Id);
|
||||
// continue running
|
||||
break;
|
||||
|
||||
case PiscalStatus.Complete:
|
||||
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Finishing);
|
||||
BackgroundJobEnqueueRetry<FinishComplete>(s => s.DoWork(leafInputId));
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (PiscalClientException ex)
|
||||
{
|
||||
PiscalExceptionHandler(ex, leafInput);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMessage = FormatException(ex);
|
||||
Logger.Error(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using LeafWeb.Core.Entities;
|
||||
using LeafWeb.Core.Remote;
|
||||
using LeafWeb.WebCms.App_Start;
|
||||
|
||||
namespace LeafWeb.WebCms.Services.PiscalQueue
|
||||
{
|
||||
public abstract class PiscalQueueWorker : PiscalQueueBase
|
||||
{
|
||||
public void DoWork(int leafInputId)
|
||||
{
|
||||
LeafInput leafInput = null;
|
||||
try
|
||||
{
|
||||
leafInput = DataService.GetLeafInput(leafInputId);
|
||||
DoWorkInternal(leafInput);
|
||||
}
|
||||
catch (PiscalClientException ex)
|
||||
{
|
||||
PiscalExceptionHandler(ex, leafInput);
|
||||
|
||||
// signal to process next item
|
||||
HangfireStartup.TriggerPiscalProcessQueue();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
var errorMessage = FormatException(ex);
|
||||
Logger.Error(errorMessage);
|
||||
throw; // this will retry via HangFire
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void DoWorkInternal(LeafInput leafInputId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using LeafWeb.Core.Entities;
|
||||
using LeafWeb.Core.Remote;
|
||||
|
||||
namespace LeafWeb.WebCms.Services.PiscalQueue
|
||||
{
|
||||
/// <summary>
|
||||
/// Thin layer over PiscalClient to translate Core entities to Piscal objects
|
||||
/// </summary>
|
||||
public class PiscalService
|
||||
{
|
||||
private readonly IPiscalClient _piscalClient;
|
||||
private readonly string _notifyCompleteUrl;
|
||||
|
||||
public PiscalService(IPiscalClient piscalClient)
|
||||
{
|
||||
_piscalClient = piscalClient;
|
||||
_notifyCompleteUrl =
|
||||
ConfigurationManager.AppSettings["LeafWebUrl"] +
|
||||
ConfigurationManager.AppSettings["PiscalNotifyCompleteUrlPath"];
|
||||
}
|
||||
|
||||
public PiscalService() : this(new PiscalSshClient(ConfigurationManager.ConnectionStrings["PiscalServer"].ConnectionString))
|
||||
{
|
||||
}
|
||||
|
||||
public void Run(LeafInput leafInput)
|
||||
{
|
||||
var inputFile = new PiscalLeafInput(leafInput);
|
||||
|
||||
if (!string.IsNullOrEmpty(_notifyCompleteUrl))
|
||||
inputFile.NotifyCompleteUrl = _notifyCompleteUrl;
|
||||
|
||||
// TODO: remove this, just for testing
|
||||
if (string.Equals(leafInput.Email, "james.kolpack@gmail.com", StringComparison.InvariantCultureIgnoreCase))
|
||||
inputFile.SuppressStorageCopy = true;
|
||||
_piscalClient.RunLeafInput(inputFile);
|
||||
}
|
||||
|
||||
public PiscalStatus GetStatus(LeafInput leafInput)
|
||||
{
|
||||
var inputFile = new PiscalLeafInput(leafInput);
|
||||
return _piscalClient.GetLeafInputStatus(inputFile);
|
||||
}
|
||||
|
||||
public IEnumerable<LeafOutputFile> RetrieveOutputFiles(LeafInput leafInput)
|
||||
{
|
||||
var input = new PiscalLeafInput(leafInput);
|
||||
var piscalLeafOutputFiles = _piscalClient.RetrieveLeafOutput(input);
|
||||
foreach (var file in piscalLeafOutputFiles)
|
||||
{
|
||||
var leafOutputFile = file.GetLeafOutputFile();
|
||||
leafOutputFile.LeafInput = leafInput;
|
||||
yield return leafOutputFile;
|
||||
}
|
||||
}
|
||||
|
||||
public void Cleanup(LeafInput leafInput)
|
||||
{
|
||||
var input = new PiscalLeafInput(leafInput);
|
||||
_piscalClient.CleanupLeafProcess(input);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using LeafWeb.Core.Entities;
|
||||
|
||||
namespace LeafWeb.WebCms.Services.PiscalQueue
|
||||
{
|
||||
public class StartPending : PiscalQueueWorker
|
||||
{
|
||||
protected override void DoWorkInternal(LeafInput leafInput)
|
||||
{
|
||||
Logger.DebugFormat("LeafInput: {0}, Run", leafInput.Id);
|
||||
|
||||
PiscalService.Run(leafInput);
|
||||
|
||||
DataService.SetLeafInputStatus(leafInput, LeafInputStatusType.Running);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user