improvements, fixes, etc...

This commit is contained in:
Loretta 2024-12-13 20:20:30 +01:00
parent 49c9a4516a
commit ac2614eac0
6 changed files with 79 additions and 31 deletions

View File

@ -15,19 +15,17 @@ using Nop.Core.Domain.Customers;
using Nop.Services.Catalog; using Nop.Services.Catalog;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using DocumentFormat.OpenXml.Spreadsheet; using DocumentFormat.OpenXml.Spreadsheet;
using Microsoft.IdentityModel.Tokens;
namespace Nop.Plugin.Misc.AuctionPlugin.Hubs 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) public class AuctionHub(SessionService sessionService, ILockService lockService, ILogger logger, IProductService productService, AuctionService auctionService, IWorkContext workContext, ICustomerService customerService, ICategoryService categoryService)
: Hub<IAuctionHubClient> : Hub<IAuctionHubClient>
{ {
//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() public override async Task OnConnectedAsync()
{ {
var connectionId = Context.ConnectionId; var connectionId = Context.ConnectionId;
var customer = await workContext.GetCurrentCustomerAsync();
//if (sessionService.GetOrCreateSessionItem(connectionId) == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}"); //if (sessionService.GetOrCreateSessionItem(connectionId) == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}");
//await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with id: {connectionId}"); //await _logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with id: {connectionId}");
@ -37,34 +35,45 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
else else
{ {
var sessionItem = sessionService.GetOrCreateSessionItem(httpContext.Session.Id); var sessionItem = sessionService.GetOrCreateSessionItem(httpContext.Session.Id);
if (sessionItem == null) await logger.ErrorAsync($"AuctionHub.OnConnectedAsync(); (sessionItem == null); connectionId: {connectionId}; sessionId: {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"]; 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 logger.InformationAsync($"AuctionHub.OnConnectedAsync(); Caller connected with name: {userName}; connectionId: {connectionId}");
} }
} }
await base.OnConnectedAsync(); await base.OnConnectedAsync();
await Clients.Caller.OnConnected(connectionId); 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) 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 HandleMessageAsync(messageWrapper, sessionItem, Context.ConnectionId);
//await SendMessageWrapperAsync(messageWrapper);
} }
public async Task<ResponseType> HandleMessageAsync(MessageWrapper messageWrapper, SessionItem sessionItem, string connectionId) public async Task<ResponseType> 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))) 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); 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); await SendAuctionBidMessageAsync(messageWrapper, activeProductAuction, product, customer.Id, ResponseType.ToCaller);
return ResponseType.Error; return ResponseType.Error;
} }
@ -297,11 +306,12 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus)) if (!IsValidRequestAuctionStatus(auctionProductStatusRequest.AuctionStatus, productToAuction.AuctionStatus))
{ {
logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer); logger.Error($"AuctionHub.HandleProductToAuctionStatusChangedRequest(); RequestAuctionStatusIsValid() == false; newStatus: {auctionProductStatusRequest.AuctionStatus}; oldStatus: {productToAuction.AuctionStatus}", null, customer);
await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToCaller); await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToCaller);
return ResponseType.Error; 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 else
{ {
switch (auctionProductStatusRequest.AuctionStatus) switch (auctionProductStatusRequest.AuctionStatus)
@ -353,7 +363,7 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Hubs
await SendStatusChangedNotificationAsync(customer, statusChangedMessageWrapper, productToAuction, ResponseType.ToAllClients); 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. //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); await StepNextProductToAuctionAsync(statusChangedMessageWrapper, customer, productToAuction, auctionProductStatusRequest);
return ResponseType.ToAllClients; 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(); 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);
} }
} }
} }

View File

@ -1,19 +1,52 @@
using Mango.Nop.Services; using AyCode.Utils.Extensions;
using Mango.Nop.Services;
using Microsoft.AspNetCore.SignalR; 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;
using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages; using Nop.Plugin.Misc.AuctionPlugin.Hubs.Messages;
using Nop.Services.Customers;
using Nop.Services.Logging; using Nop.Services.Logging;
namespace Nop.Plugin.Misc.AuctionPlugin.Services; namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public class AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext<AuctionHub> auctionHubContext, AuctionService auctionService) : MgBackgroundServiceBase(logger, service) public class AuctionBackgroundService : MgBackgroundServiceBase
{ {
private readonly ILogger _logger;
private readonly IHubContext<AuctionHub> _auctionHubContext;
private readonly ILockService _lockService;
private readonly Customer _auctionSystemCustomer;
public AuctionBackgroundService(ILogger logger, IServiceProvider service, IHubContext<AuctionHub> auctionHubContext, AuctionService auctionService, ILockService lockService, IRepository<Customer> 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() protected override async Task OnExecuteAsync()
{ {
//var auctionDto = auctionService.GetProductToAuctionDtosByAuctionId() await Task.Delay(15000); //Az elejére kell tenni! ha exception lenne, akkor ne kezdje el darálni... - J.
//await auctionHubContext.Clients.All.SendAsync("send", new MessageWrapper());
await auctionHubContext.Clients.All.SendAsync("OnDateTimeReceive", DateTime.Now.ToString("G")); if (_auctionSystemCustomer == null) return;
await Task.Delay(15000);
//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"));
}
} }
} }

View File

@ -4,5 +4,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public interface ISessionItem : IMgSessionItem public interface ISessionItem : IMgSessionItem
{ {
int CustomerId { get; set; }
} }

View File

@ -4,5 +4,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public interface ISessionService : IMgSessionService<SessionItem> public interface ISessionService : IMgSessionService<SessionItem>
{ {
bool TryGetSessionItemByCustomerId(int customerId, out SessionItem sessionItem);
} }

View File

@ -4,5 +4,5 @@ namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public class SessionItem(string sessionKey) : MgSessionItem(sessionKey), ISessionItem public class SessionItem(string sessionKey) : MgSessionItem(sessionKey), ISessionItem
{ {
public int CustomerId { get; set; }
} }

View File

@ -1,8 +1,13 @@
using Mango.Nop.Services; using Mango.Nop.Services;
using Nop.Core.Domain.Customers;
namespace Nop.Plugin.Misc.AuctionPlugin.Services; namespace Nop.Plugin.Misc.AuctionPlugin.Services;
public class SessionService : MgSessionService<SessionItem>, ISessionService public class SessionService : MgSessionService<SessionItem>, ISessionService
{ {
public bool TryGetSessionItemByCustomerId(int customerId, out SessionItem sessionItem)
{
sessionItem = Sessions.Values.FirstOrDefault(x => x.CustomerId == customerId);
return sessionItem != null;
}
} }