From 2232061f95f49c176bae5c8bba313b267ade3347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Tue, 11 Jul 2023 18:59:11 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20=D0=BB?= =?UTF-8?q?=D0=B8=D1=88=D0=BD=D0=B5=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/NotificationTransportDto.cs | 17 ---- .../INotificationSendingQueueService.cs | 41 ---------- AsbCloudDb/Model/NotificationTransport.cs | 20 ----- .../NotificationSendingQueueService.cs | 42 ---------- .../Services/NotificationService.cs | 71 ---------------- .../NotificationsOptionsSignalR.cs | 8 -- .../SignalRNotificationSender.cs | 82 ------------------- AsbCloudWebApi/appsettings.json | 4 - 8 files changed, 285 deletions(-) delete mode 100644 AsbCloudApp/Data/NotificationTransportDto.cs delete mode 100644 AsbCloudApp/Services/INotificationSendingQueueService.cs delete mode 100644 AsbCloudDb/Model/NotificationTransport.cs delete mode 100644 AsbCloudInfrastructure/Services/NotificationSendingQueueService.cs delete mode 100644 AsbCloudInfrastructure/Services/NotificationService.cs delete mode 100644 AsbCloudWebApi/Options/Notifications/NotificationsOptionsSignalR.cs delete mode 100644 AsbCloudWebApi/SignalR/BackgroundServices/SignalRNotificationSender.cs diff --git a/AsbCloudApp/Data/NotificationTransportDto.cs b/AsbCloudApp/Data/NotificationTransportDto.cs deleted file mode 100644 index 6f1d0a72..00000000 --- a/AsbCloudApp/Data/NotificationTransportDto.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace AsbCloudApp.Data; - -/// -/// DTO способа доставки уведомления -/// -public class NotificationTransportDto -{ - /// - /// Id способа доставки уведомления - /// - public int Id { get; set; } - - /// - /// Название способа доставки - /// - public string Name { get; set; } = null!; -} \ No newline at end of file diff --git a/AsbCloudApp/Services/INotificationSendingQueueService.cs b/AsbCloudApp/Services/INotificationSendingQueueService.cs deleted file mode 100644 index 95614e6f..00000000 --- a/AsbCloudApp/Services/INotificationSendingQueueService.cs +++ /dev/null @@ -1,41 +0,0 @@ -using System.Collections.Generic; -using System.Threading; -using AsbCloudApp.Data; - -namespace AsbCloudApp.Services; - -/// -/// Сервис для добавление уведомлений в очередь -/// -public interface INotificationSendingQueueService -{ - /// - /// Флаг для проверки пустая ли коллекция - /// - bool IsEmpty { get; } - - /// - /// Добавление одного уведомления в очередь - /// - /// - void Enqueue(NotificationDto notificationDto); - - /// - /// Добавление нескольких уведомлений в очередь - /// - /// - void EnqueueRange(IEnumerable notifications); - - /// - /// Извлечение элемента из очереди и его удаление - /// - /// - /// - bool TryDequeue(out NotificationDto notification); - - /// - /// Метод ожидания нового уведомления - /// - /// - void Wait(CancellationToken cancellationToken); -} \ No newline at end of file diff --git a/AsbCloudDb/Model/NotificationTransport.cs b/AsbCloudDb/Model/NotificationTransport.cs deleted file mode 100644 index 36ed528e..00000000 --- a/AsbCloudDb/Model/NotificationTransport.cs +++ /dev/null @@ -1,20 +0,0 @@ -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using Microsoft.EntityFrameworkCore; - -namespace AsbCloudDb.Model; - -[Table("t_notification_transport"), Comment("Способ доставки уведомлений")] -public class NotificationTransport : IId -{ - [Key] - [Column("id")] - public int Id { get; set; } - - [Column("name"), Comment("Название способа доставки уведомлений")] - public string Name { get; set; } = null!; - - [InverseProperty(nameof(Notification.NotificationTransport))] - public virtual ICollection Notifications { get; set; } = null!; -} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/NotificationSendingQueueService.cs b/AsbCloudInfrastructure/Services/NotificationSendingQueueService.cs deleted file mode 100644 index 23da999f..00000000 --- a/AsbCloudInfrastructure/Services/NotificationSendingQueueService.cs +++ /dev/null @@ -1,42 +0,0 @@ -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 notificationsQueue = new(); - - public bool IsEmpty => notificationsQueue.IsEmpty; - - public void Enqueue(NotificationDto notification) - { - notificationsQueue.Enqueue(notification); - manualResetEventSlim.Set(); - } - - public void EnqueueRange(IEnumerable 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(); - } -} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/NotificationService.cs b/AsbCloudInfrastructure/Services/NotificationService.cs deleted file mode 100644 index 3db22eba..00000000 --- a/AsbCloudInfrastructure/Services/NotificationService.cs +++ /dev/null @@ -1,71 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.Data; -using AsbCloudApp.Exceptions; -using AsbCloudApp.Repositories; -using AsbCloudApp.Services; - -namespace AsbCloudInfrastructure.Services; - -public class NotificationService : INotificationService -{ - private readonly INotificationSendingQueueService notificationSendingQueueService; - private readonly INotificationRepository notificationRepository; - - public NotificationService(INotificationSendingQueueService notificationSendingQueueService, - INotificationRepository notificationRepository) - { - this.notificationSendingQueueService = notificationSendingQueueService; - this.notificationRepository = notificationRepository; - } - - public async Task SendNotificationAsync(int idUser, - int idNotificationTransport, - int idNotificationCategory, - string title, - string subject, - TimeSpan timeToLife, - CancellationToken cancellationToken) - { - NotificationDto notification = new() - { - IdUser = idUser, - IdNotificationTransport = idNotificationTransport, - IdNotificationCategory = idNotificationCategory, - Title = title, - Subject = subject, - TimeToLife = timeToLife - }; - - await notificationRepository.InsertAsync(notification, - cancellationToken); - - notificationSendingQueueService.Enqueue(notification); - } - - public async Task UpdateNotificationAsync(int idNotification, - bool isRead, - CancellationToken cancellationToken) - { - var notification = await notificationRepository.GetOrDefaultAsync(idNotification, - cancellationToken) ?? throw new ArgumentInvalidException("Уведомление не найдено", - nameof(idNotification)); - - notification.IsRead = isRead; - - await notificationRepository.UpdateAsync(notification, - cancellationToken); - } - - public async Task ResendNotificationAsync(int idUser, - int idNotificationTransport, - CancellationToken cancellationToken) - { - var notifications = await notificationRepository.GetUnsentNotificationsAsync(idUser, - idNotificationTransport, - cancellationToken); - - notificationSendingQueueService.EnqueueRange(notifications); - } -} \ No newline at end of file diff --git a/AsbCloudWebApi/Options/Notifications/NotificationsOptionsSignalR.cs b/AsbCloudWebApi/Options/Notifications/NotificationsOptionsSignalR.cs deleted file mode 100644 index b955bd7a..00000000 --- a/AsbCloudWebApi/Options/Notifications/NotificationsOptionsSignalR.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace AsbCloudWebApi.Options.Notifications; - -public class NotificationsOptionsSignalR -{ - public string Method { get; set; } = null!; - - public int IdTransport { get; set; } -} \ No newline at end of file diff --git a/AsbCloudWebApi/SignalR/BackgroundServices/SignalRNotificationSender.cs b/AsbCloudWebApi/SignalR/BackgroundServices/SignalRNotificationSender.cs deleted file mode 100644 index c0d09014..00000000 --- a/AsbCloudWebApi/SignalR/BackgroundServices/SignalRNotificationSender.cs +++ /dev/null @@ -1,82 +0,0 @@ -using System; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.Repositories; -using AsbCloudApp.Services; -using AsbCloudWebApi.Options.Notifications; -using AsbCloudWebApi.SignalR.ConnectionManager; -using Microsoft.AspNetCore.SignalR; -using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Options; - -namespace AsbCloudWebApi.SignalR.BackgroundServices; - -public class SignalRNotificationSender : BackgroundService -{ - private readonly IConnectionManager connectionManager; - private readonly INotificationSendingQueueService notificationSendingQueueService; - private readonly IHubContext notificationHubContext; - private readonly NotificationsOptionsSignalR notificationsOptionsSignalR; - private readonly IServiceProvider serviceProvider; - - public SignalRNotificationSender(IConnectionManager connectionManager, - INotificationSendingQueueService notificationSendingQueueService, - IHubContext notificationHubContext, - IOptions notificationsOptionsSignalR, - IServiceProvider serviceProvider) - { - this.connectionManager = connectionManager; - this.notificationSendingQueueService = notificationSendingQueueService; - this.notificationHubContext = notificationHubContext; - this.notificationsOptionsSignalR = notificationsOptionsSignalR.Value; - this.serviceProvider = serviceProvider; - } - - protected override async Task ExecuteAsync(CancellationToken stoppingToken) - { - await Task.Run( async () => - { - await SendAsync(stoppingToken); - }, stoppingToken); - } - - private async Task SendAsync(CancellationToken cancellationToken) - { - while (!cancellationToken.IsCancellationRequested) - { - notificationSendingQueueService.Wait(cancellationToken); - - while (!notificationSendingQueueService.IsEmpty) - { - if (notificationSendingQueueService.TryDequeue(out var notification)) - { - string userId = notification.IdUser.ToString(); - - var connectionId = connectionManager.GetConnectionIdByUserId(userId); - - if (!string.IsNullOrWhiteSpace(connectionId)) - { - await notificationHubContext.Clients.Client(connectionId) - .SendAsync(notificationsOptionsSignalR.Method, - notification, - cancellationToken); - - notification.SentDateAtUtc = DateTime.UtcNow; - notification.IsRead = false; - } - - var scope = serviceProvider.CreateScope(); - - var notificationRepository = scope.ServiceProvider.GetService(); - - if (notificationRepository != null) - { - await notificationRepository.UpdateAsync(notification, - cancellationToken); - } - } - } - } - } -} \ No newline at end of file diff --git a/AsbCloudWebApi/appsettings.json b/AsbCloudWebApi/appsettings.json index 5014dbf2..683f7929 100644 --- a/AsbCloudWebApi/appsettings.json +++ b/AsbCloudWebApi/appsettings.json @@ -27,10 +27,6 @@ "supportMail": "support@digitaldrilling.ru" }, "DirectoryNameHelpPageFiles": "helpPages", - "NotificationsOptionsSignalR": { - "Method": "notifications", - "IdTransport": 1 - }, "Urls": "http://0.0.0.0:5000" //;https://0.0.0.0:5001" //, // See https man: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0 //"Kestrel": {