77 lines
2.5 KiB
C#
77 lines
2.5 KiB
C#
using Hcs.WebApp.Data.Hcs;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace Hcs.WebApp.BackgroundServices
|
|
{
|
|
public class OperationExecutionState
|
|
{
|
|
public delegate void OperationStarted(int operationId, int campaignId, DateTime startedAt);
|
|
public delegate void OperationExecuted(int operationId, int campaignId, string messageGuid);
|
|
public delegate void OperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason);
|
|
|
|
private readonly ConcurrentQueue<Operation> operationsInQueue = new();
|
|
private readonly HashSet<Operation> operationsInProcess = [];
|
|
|
|
public bool Ready { get; set; }
|
|
|
|
public event Action<Operation> OnOperationCreated;
|
|
public event OperationStarted OnOperationStarted;
|
|
public event OperationExecuted OnOperationExecuted;
|
|
public event OperationEnded OnOperationEnded;
|
|
|
|
public void EnqueueOperation(Operation operation)
|
|
{
|
|
operationsInQueue.Enqueue(operation);
|
|
|
|
OnOperationCreated?.Invoke(operation);
|
|
}
|
|
|
|
public bool TryDequeueOperation(out Operation operation)
|
|
{
|
|
return operationsInQueue.TryDequeue(out operation);
|
|
}
|
|
|
|
public void SetProcessingOperation(Operation operation)
|
|
{
|
|
operationsInProcess.Add(operation);
|
|
}
|
|
|
|
public void UnsetProcessingOperation(Operation operation)
|
|
{
|
|
operationsInProcess.Remove(operation);
|
|
}
|
|
|
|
public bool HasCampaignOperation(int campaignId)
|
|
{
|
|
return operationsInQueue.Any(x => x.CampaignId == campaignId) || operationsInProcess.Any(x => x.CampaignId == campaignId);
|
|
}
|
|
|
|
public void InvokeOnOperationStarted(int operationId, int campaignId, DateTime startedAt)
|
|
{
|
|
try
|
|
{
|
|
OnOperationStarted?.Invoke(operationId, campaignId, startedAt);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public void InvokeOnOperationExecuted(int operationId, int campaignId, string messageGuid)
|
|
{
|
|
try
|
|
{
|
|
OnOperationExecuted?.Invoke(operationId, campaignId, messageGuid);
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
public void InvokeOnOperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason)
|
|
{
|
|
try
|
|
{
|
|
OnOperationEnded?.Invoke(operationId, campaignId, endedAt, failureReason);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|