Add synchronization to PiscalQueueManager

This commit is contained in:
2016-03-02 11:06:51 -05:00
parent 10cd2986cf
commit 8769f5fcd9
+45 -18
View File
@@ -1,5 +1,6 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading;
using LeafWeb.Core.DAL; using LeafWeb.Core.DAL;
using LeafWeb.Core.Entities; using LeafWeb.Core.Entities;
using LeafWeb.Core.Remote; using LeafWeb.Core.Remote;
@@ -20,10 +21,52 @@ namespace LeafWeb.Web.Services
public PiscalQueueManager() : this(new DataService(), new PiscalService()) {} public PiscalQueueManager() : this(new DataService(), new PiscalService()) {}
private static readonly object Lock = new object();
public void ProcessQueue() public void ProcessQueue()
{ {
var logger = LogManager.GetCurrentClassLogger(); var logger = LogManager.GetCurrentClassLogger();
if (Monitor.TryEnter(Lock))
{
logger.Trace("Process entered");
ProcessRunning(logger);
ProcessQueue(logger);
logger.Trace("Process completed");
Monitor.Exit(Lock);
}
else
{
logger.Trace("Process locked, queue already processing");
}
}
private void ProcessQueue(ILogger logger)
{
var runningLeafInputFiles = _dataService.GetLeafInputFiles(LeafInputStatusType.Running).ToList();
if (runningLeafInputFiles.Any())
return;
var queuedFile =
_dataService
.GetLeafInputFiles(LeafInputStatusType.Queued)
.OrderBy(l => l.Id)
.FirstOrDefault();
if (queuedFile == null)
return;
logger.Info("LeafInputFile: {0}, Start", queuedFile.Id);
_piscalService.RunLeafInputFile(queuedFile);
_dataService.SetLeafInputFileStatus(queuedFile, LeafInputStatusType.Running);
}
private void ProcessRunning(ILogger logger)
{
var runningLeafInputFiles = _dataService.GetLeafInputFiles(LeafInputStatusType.Running).ToList(); var runningLeafInputFiles = _dataService.GetLeafInputFiles(LeafInputStatusType.Running).ToList();
foreach (var file in runningLeafInputFiles) foreach (var file in runningLeafInputFiles)
{ {
@@ -42,7 +85,8 @@ namespace LeafWeb.Web.Services
{ {
_dataService.AddLeafOutputFile(outputFile); _dataService.AddLeafOutputFile(outputFile);
} }
logger.Info("LeafInputFile: {0}, output files: {1}", file.Id, string.Join(", ", leafOutputFiles.Select(o => o.Filename))); logger.Info("LeafInputFile: {0}, output files: {1}", file.Id,
string.Join(", ", leafOutputFiles.Select(o => o.Filename)));
_dataService.SetLeafInputFileStatus(file, LeafInputStatusType.Complete); _dataService.SetLeafInputFileStatus(file, LeafInputStatusType.Complete);
break; break;
case PiscalStatus.Error: case PiscalStatus.Error:
@@ -51,23 +95,6 @@ namespace LeafWeb.Web.Services
break; break;
} }
} }
runningLeafInputFiles = _dataService.GetLeafInputFiles(LeafInputStatusType.Running).ToList();
if (!runningLeafInputFiles.Any())
{
var queuedFile =
_dataService
.GetLeafInputFiles(LeafInputStatusType.Queued)
.OrderBy(l => l.Id)
.FirstOrDefault();
if (queuedFile != null)
{
logger.Info("LeafInputFile: {0}, Start", queuedFile.Id);
_piscalService.RunLeafInputFile(queuedFile);
_dataService.SetLeafInputFileStatus(queuedFile, LeafInputStatusType.Running);
}
}
} }
public void Dispose() public void Dispose()