forked from ddrilling/AsbCloudServer
Удалил лишнее
This commit is contained in:
parent
985c0489d0
commit
2232061f95
@ -1,17 +0,0 @@
|
|||||||
namespace AsbCloudApp.Data;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// DTO способа доставки уведомления
|
|
||||||
/// </summary>
|
|
||||||
public class NotificationTransportDto
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Id способа доставки уведомления
|
|
||||||
/// </summary>
|
|
||||||
public int Id { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Название способа доставки
|
|
||||||
/// </summary>
|
|
||||||
public string Name { get; set; } = null!;
|
|
||||||
}
|
|
@ -1,41 +0,0 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Threading;
|
|
||||||
using AsbCloudApp.Data;
|
|
||||||
|
|
||||||
namespace AsbCloudApp.Services;
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Сервис для добавление уведомлений в очередь
|
|
||||||
/// </summary>
|
|
||||||
public interface INotificationSendingQueueService
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Флаг для проверки пустая ли коллекция
|
|
||||||
/// </summary>
|
|
||||||
bool IsEmpty { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Добавление одного уведомления в очередь
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="notificationDto"></param>
|
|
||||||
void Enqueue(NotificationDto notificationDto);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Добавление нескольких уведомлений в очередь
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="notifications"></param>
|
|
||||||
void EnqueueRange(IEnumerable<NotificationDto> notifications);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Извлечение элемента из очереди и его удаление
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="notification"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
bool TryDequeue(out NotificationDto notification);
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Метод ожидания нового уведомления
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="cancellationToken"></param>
|
|
||||||
void Wait(CancellationToken cancellationToken);
|
|
||||||
}
|
|
@ -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<Notification> Notifications { get; set; } = 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<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();
|
|
||||||
}
|
|
||||||
}
|
|
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
namespace AsbCloudWebApi.Options.Notifications;
|
|
||||||
|
|
||||||
public class NotificationsOptionsSignalR
|
|
||||||
{
|
|
||||||
public string Method { get; set; } = null!;
|
|
||||||
|
|
||||||
public int IdTransport { get; set; }
|
|
||||||
}
|
|
@ -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<NotificationHub> notificationHubContext;
|
|
||||||
private readonly NotificationsOptionsSignalR notificationsOptionsSignalR;
|
|
||||||
private readonly IServiceProvider serviceProvider;
|
|
||||||
|
|
||||||
public SignalRNotificationSender(IConnectionManager connectionManager,
|
|
||||||
INotificationSendingQueueService notificationSendingQueueService,
|
|
||||||
IHubContext<NotificationHub> notificationHubContext,
|
|
||||||
IOptions<NotificationsOptionsSignalR> 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<INotificationRepository>();
|
|
||||||
|
|
||||||
if (notificationRepository != null)
|
|
||||||
{
|
|
||||||
await notificationRepository.UpdateAsync(notification,
|
|
||||||
cancellationToken);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -27,10 +27,6 @@
|
|||||||
"supportMail": "support@digitaldrilling.ru"
|
"supportMail": "support@digitaldrilling.ru"
|
||||||
},
|
},
|
||||||
"DirectoryNameHelpPageFiles": "helpPages",
|
"DirectoryNameHelpPageFiles": "helpPages",
|
||||||
"NotificationsOptionsSignalR": {
|
|
||||||
"Method": "notifications",
|
|
||||||
"IdTransport": 1
|
|
||||||
},
|
|
||||||
"Urls": "http://0.0.0.0:5000" //;https://0.0.0.0:5001" //,
|
"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
|
// See https man: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/endpoints?view=aspnetcore-6.0
|
||||||
//"Kestrel": {
|
//"Kestrel": {
|
||||||
|
Loading…
Reference in New Issue
Block a user