From ac2614eac00c7f58fa64773910ef2f5bad0ab3a0 Mon Sep 17 00:00:00 2001 From: Loretta Date: Fri, 13 Dec 2024 20:20:30 +0100 Subject: [PATCH] improvements, fixes, etc... --- .../Hubs/AuctionHub.cs | 52 +++++++++++-------- .../Services/AuctionBackgroundService.cs | 45 +++++++++++++--- .../Services/ISessionItem.cs | 2 +- .../Services/ISessionService.cs | 2 +- .../Services/SessionItem.cs | 2 +- .../Services/SessionService.cs | 7 ++- 6 files changed, 79 insertions(+), 31 deletions(-) diff --git a/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs b/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs index bdce78e..51ef2fc 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Hubs/AuctionHub.cs @@ -15,19 +15,17 @@ using Nop.Core.Domain.Customers; using Nop.Services.Catalog; using Newtonsoft.Json.Linq; using DocumentFormat.OpenXml.Spreadsheet; +using Microsoft.IdentityModel.Tokens; namespace Nop.Plugin.Misc.AuctionPlugin.Hubs { public class AuctionHub(SessionService sessionService, ILockService lockService, ILogger logger, IProductService productService, AuctionService auctionService, IWorkContext workContext, ICustomerService customerService, ICategoryService categoryService) : Hub { - //private static readonly SemaphoreSlim _handleMessageMutex = new(1); - //private readonly Semaphore _handleMessageMutex = new(1, 1, "Nop.Plugin.Misc.AuctionPlugin.Hubs.AuctionHub"); - public override async Task OnConnectedAsync() { var connectionId = Context.ConnectionId; - + var customer = await workContext.GetCurrentCustomerAsync(); //if (sessionService.GetOrCreateSessionItem(connectionId) == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}"); //await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with id: {connectionId}"); @@ -37,34 +35,45 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs else { var sessionItem = sessionService.GetOrCreateSessionItem(httpContext.Session.Id); - + if (sessionItem == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}; sessionId: {httpContext.Session.Id}"); - else sessionItem.SignaRConnectionId = connectionId; + else + { + sessionItem.CustomerId = customer.Id; + sessionItem.SignaRConnectionId = connectionId; + } var userName = httpContext.Request.Query["ConnectionId"]; - if (!string.IsNullOrEmpty(userName)) + if (!userName.IsNullOrEmpty()) { await logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with name: {userName}; connectionId: {connectionId}"); } } - + await base.OnConnectedAsync(); await Clients.Caller.OnConnected(connectionId); } - public override Task OnDisconnectedAsync(Exception exception) + public override async Task OnDisconnectedAsync(Exception exception) { - sessionService.TryRemoveSessionItem(Context.ConnectionId, out _); + var httpContext = Context.GetHttpContext(); - return base.OnDisconnectedAsync(exception); + if (httpContext == null) await logger.ErrorAsync($"AuctionHub.OnDisconnectedAsync(); (httpContext == null); connectionId: {Context.ConnectionId}"); + else sessionService.TryRemoveSessionItem(httpContext.Session.Id, out _); + + await base.OnDisconnectedAsync(exception); } public async Task ReceiveMessageFromClient(MessageWrapper messageWrapper) { - var sessionItem = IncrementRequestCount(); + if (!TryGetCurrentSessionItem(out var sessionItem) || sessionItem == null) + { + await logger.ErrorAsync($"AuctionHub.ReceiveMessageFromClient(); (TryGetCurrentSessionItem(out var sessionItem) == false || sessionItem == null); connectionId: {Context.ConnectionId}"); + return; + } + sessionItem.RequestCount++; await HandleMessageAsync(messageWrapper, sessionItem, Context.ConnectionId); - //await SendMessageWrapperAsync(messageWrapper); } public async Task HandleMessageAsync(MessageWrapper messageWrapper, SessionItem sessionItem, string connectionId) @@ -224,7 +233,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs if (activeProductAuction is not { IsActiveItem: true } || (activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer))) { logger.Warning($"AuctionHub.HandleBidRequestAsync(); (activeProductAuction is not {{ IsActiveItem: true }} || activeProductAuction.WinnerCustomerId == customer.Id && !await customerService.IsAdminAsync(customer)); AuctionStatus: {activeProductAuction?.AuctionStatus}; WinnerCustomerId: {activeProductAuction?.WinnerCustomerId}", null, customer); - + await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller); return ResponseType.Error; } @@ -297,11 +306,12 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus)) { logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer); - + await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToCaller); return ResponseType.Error; } - else if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId); + + if (auctionProductStatusRequest.AuctionStatus == AuctionStatus.None) await ResetProductToAuction(productToAuction, auction.CategoryId); else { switch (auctionProductStatusRequest.AuctionStatus) @@ -353,7 +363,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToAllClients); //TODO: gond van ha az Admin valamit módosít és újrazár egy régi ProductToAuction-t!!!! AuctionRequestMode.Normal...- J. - if (/*AuctionRequestMode.Normal &&*/ auction.AuctionType == AuctionType.AutomaticNext && productToAuction.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold) + if ( /*AuctionRequestMode.Normal &&*/ auction.AuctionType == AuctionType.AutomaticNext && productToAuction.AuctionStatus is AuctionStatus.Sold or AuctionStatus.NotSold) await StepNextProductToAuctionAsync(statusChangedMessageWrapper, customer, productToAuction, auctionProductStatusRequest); return ResponseType.ToAllClients; @@ -581,14 +591,14 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs } } - private SessionItem IncrementRequestCount() + private bool TryGetCurrentSessionItem(out SessionItem sessionItem) { - SessionItem sessionItem = null; + sessionItem = null; var httpContext = Context.GetHttpContext(); - if (httpContext != null && sessionService.TryGetSessionItem(httpContext.Session.Id, out sessionItem)) sessionItem.RequestCount++; + if (httpContext == null) return false; - return sessionItem; + return !sessionService.TryGetSessionItem(httpContext.Session.Id, out sessionItem); } } } diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs index ea3dfe8..92478bd 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/AuctionBackgroundService.cs @@ -1,19 +1,52 @@ -using Mango.Nop.Services; +using AyCode.Utils.Extensions; +using Mango.Nop.Services; using Microsoft.AspNetCore.SignalR; +using Nop.Core; +using Nop.Core.Domain.Customers; +using Nop.Data; using Nop.Plugin.Misc.AuctionPlugin.Hubs; using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages; +using Nop.Services.Customers; using Nop.Services.Logging; namespace Nop.Plugin.Misc.AuctionPlugin.Services; -public class AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext auctionHubContext, AuctionService auctionService) : MgBackgroundServiceBase(logger, service) +public class AuctionBackgroundService : MgBackgroundServiceBase { + private readonly ILogger _logger; + private readonly IHubContext _auctionHubContext; + private readonly ILockService _lockService; + + private readonly Customer _auctionSystemCustomer; + + public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository customerService) : base(logger, service) + { + _logger = logger; + _auctionHubContext = auctionHubContext; + _lockService = lockService; + + _auctionSystemCustomer = customerService.Table.FirstOrDefault(x => x.Email == "builtin@background_task_auction.com"); + + if (_auctionSystemCustomer == null) + _logger.Error($"AuctionBackgroundService.AuctionBackgroundService(); _auctionSystemCustomer == null;", null, null); + } + protected override async Task OnExecuteAsync() { - //var auctionDto = auctionService.GetProductToAuctionDtosByAuctionId() - //await auctionHubContext.Clients.All.SendAsync("send", new MessageWrapper()); + await Task.Delay(15000); //Az elejére kell tenni! ha exception lenne, akkor ne kezdje el darálni... - J. - await auctionHubContext.Clients.All.SendAsync("OnDateTimeReceive", DateTime.Now.ToString("G")); - await Task.Delay(15000); + if (_auctionSystemCustomer == null) return; + + //await _logger.InformationAsync($"AuctionBackgroundService.OnExecuteAsync(); Before lock; ", null, _auctionSystemCustomer); + + using (await _lockService.SemaphoreSlim.UseWaitAsync()) + { + + //var auctionDto = auctionService.GetProductToAuctionDtosByAuctionId() + //await auctionHubContext.Clients.All.SendAsync("send", new MessageWrapper()); + + await _auctionHubContext.Clients.All.SendAsync("OnDateTimeReceive", DateTime.Now.ToString("G")); + + } } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionItem.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionItem.cs index 213a488..bb7083d 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionItem.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionItem.cs @@ -4,5 +4,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services; public interface ISessionItem : IMgSessionItem { - + int CustomerId { get; set; } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionService.cs index 3f72369..e5c92bb 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/ISessionService.cs @@ -4,5 +4,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services; public interface ISessionService : IMgSessionService { - + bool TryGetSessionItemByCustomerId(int customerId, out SessionItem sessionItem); } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/SessionItem.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/SessionItem.cs index 737cfbd..3db6b2c 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/SessionItem.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/SessionItem.cs @@ -4,5 +4,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services; public class SessionItem(string sessionKey) : MgSessionItem(sessionKey), ISessionItem { - + public int CustomerId { get; set; } } \ No newline at end of file diff --git a/Nop.Plugin.Misc.AuctionPlugin/Services/SessionService.cs b/Nop.Plugin.Misc.AuctionPlugin/Services/SessionService.cs index 374ed63..f73533c 100644 --- a/Nop.Plugin.Misc.AuctionPlugin/Services/SessionService.cs +++ b/Nop.Plugin.Misc.AuctionPlugin/Services/SessionService.cs @@ -1,8 +1,13 @@ using Mango.Nop.Services; +using Nop.Core.Domain.Customers; namespace Nop.Plugin.Misc.AuctionPlugin.Services; public class SessionService : MgSessionService, ISessionService { - + public bool TryGetSessionItemByCustomerId(int customerId, out SessionItem sessionItem) + { + sessionItem = Sessions.Values.FirstOrDefault(x => x.CustomerId == customerId); + return sessionItem != null; + } } \ No newline at end of file