klippa

example1

Sep 15th, 2020 (edited)
623
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ExponeaNotificationSender : IJobService
  2. {
  3.     private readonly ILogger<ExponeaNotificationSender> _logger;
  4.     private readonly IExponeaNotificationService _notificationService;
  5.  
  6.     public ExponeaNotificationSender(
  7.         ILogger<ExponeaNotificationSender> logger,
  8.         IExponeaNotificationService notificationService)
  9.     {
  10.         _logger = logger;
  11.         _notificationService = notificationService;
  12.     }
  13.  
  14.     private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim(1, 1);
  15.  
  16.    
  17.     public string Name => "Send Exponea Notifications";
  18.  
  19.     public async Task Run(CancellationToken cancellationToken)
  20.     {
  21.         await Semaphore.WaitAsync();
  22.         try
  23.         {
  24.             _logger.LogInformation("Start sending exponea notification");
  25.  
  26.             var notifications = (await _notificationService.GetNotSent()).ToList();
  27.            
  28.             _logger.LogInformation("Found {Count} notifications to send", notifications.Count);
  29.  
  30.             if (notifications.Count > 0)
  31.             {
  32.                 await _notificationService.Send(notifications);
  33.  
  34.                 _logger.LogInformation("Successfully sent {Count} notifications", GetSucceedCount(notifications));
  35.             }
  36.         }
  37.         finally
  38.         {
  39.             Semaphore.Release();
  40.         }
  41.     }
  42.  
  43.     private static int GetSucceedCount(List<ExponeaNotification> notifications)
  44.     {
  45.         return notifications.Count(x => x.SendState == NotificationSendState.Success);
  46.     }
  47. }
RAW Paste Data