52 lines
1.5 KiB
C#
52 lines
1.5 KiB
C#
using Hcs.WebApp.Data.Hcs;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace Hcs.WebApp.BackgroundServices
|
|
{
|
|
public class ResultWaitState
|
|
{
|
|
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 OperationEnded OnOperationEnded;
|
|
|
|
public void EnqueueOperation(Operation operation)
|
|
{
|
|
operationsInQueue.Enqueue(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 InvokeOnOperationEnded(int operationId, int campaignId, DateTime endedAt, string failureReason)
|
|
{
|
|
try
|
|
{
|
|
OnOperationEnded?.Invoke(operationId, campaignId, endedAt, failureReason);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
}
|