Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ExponeaNotificationSender : IJobService
- {
- private readonly ILogger<ExponeaNotificationSender> _logger;
- private readonly IExponeaNotificationService _notificationService;
- public ExponeaNotificationSender(
- ILogger<ExponeaNotificationSender> logger,
- IExponeaNotificationService notificationService)
- {
- _logger = logger;
- _notificationService = notificationService;
- }
- private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1, 1);
- public string Name => "Send Exponea Notifications";
- public async Task Run(CancellationToken cancellationToken)
- {
- await Semaphore.WaitAsync();
- try
- {
- _logger.LogInformation("Start sending exponea notification");
- var notifications = (await _notificationService.GetNotSent()).ToList();
- _logger.LogInformation("Found {Count} notifications to send", notifications.Count);
- if (notifications.Count > 0)
- {
- await _notificationService.Send(notifications);
- _logger.LogInformation("Successfully sent {Count} notifications", GetSucceedCount(notifications));
- }
- }
- finally
- {
- Semaphore.Release();
- }
- }
- private static int GetSucceedCount(List<ExponeaNotification> notifications)
- {
- return notifications.Count(x => x.SendState == NotificationSendState.Success);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement