DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/NotificationSendingQueueService.cs

42 lines
1.1 KiB
C#
Raw Normal View History

using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading;
using AsbCloudApp.Data;
using AsbCloudApp.Services;
namespace AsbCloudInfrastructure.Services;
public class NotificationSendingQueueService : INotificationSendingQueueService
{
private readonly ManualResetEventSlim manualResetEventSlim = new();
private readonly ConcurrentQueue<NotificationDto> notificationsQueue = new();
public bool IsEmpty => notificationsQueue.IsEmpty;
public void Enqueue(NotificationDto notification)
{
notificationsQueue.Enqueue(notification);
manualResetEventSlim.Set();
}
public void EnqueueRange(IEnumerable<NotificationDto> notifications)
{
foreach (var notification in notifications)
{
notificationsQueue.Enqueue(notification);
}
manualResetEventSlim.Set();
}
public bool TryDequeue(out NotificationDto notification)
{
return notificationsQueue.TryDequeue(out notification!);
}
public void Wait(CancellationToken cancellationToken)
{
manualResetEventSlim.Wait(cancellationToken);
manualResetEventSlim.Reset();
}
}